Skip to content

Automated deployment of lightweight Kubernetes (K3s) clusters on azure cloud VMs using Terraform and Ansible(fully automated)

License

x0rw/k3s-terraform-ansible-cluster

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

k3s-terraform-ansible-cluster

Spin up flexible, scalable, and compliant K3s clusters in Azure Cloud(for now) from a single configuration file.

Features

  • Modular azure cluster creation
  • remote terraform state support(Terraform Cloud)
  • Managed ssh keys through azure key vault
  • Ansible to fully setup and config kubernetes in the clusters
  • Modular ansible with playbooks and roles
  • Use certs with TTL instead of ssh keys(for security and compliance -- rotation)
  • Ansible roles to setup helm, Prometheus/grafana, k3s dashboard...
  • [ ]

Prerequisits

  • You need an azure account(auth through the cli az login, or create a principal and save the tokens)
  • create a vault called wasp-vault in your az account
  • optional: terraform cloud account for remote state managment

How to setup

1. create terraform.tfvars in terraform/ with your desired configs:

clusters = {
  france = {
    cluster_name   = "france-cluster-1"
    region         = "francecentral"
    admin_username = "azureuser"
    workers = [
      {
        name          = "worker-group-1"
        instance_size = "Standard_B1ms"
        disk_size     = 30
        count         = 13  
        tags          = { role = "ms1" }
      },
      {
        name          = "worker-group-2"
        instance_size = "Standard_B1ms"
        disk_size     = 50
        count         = 5
        tags          = { role = "dbs" }
      }
    ]
  }

  spain = {
    cluster_name   = "spain-cent-test"
    region         = "spaincentral"
    admin_username = "azureuser"
    workers = [
      {
        name          = "wkr-1"
        instance_size = "Standard_B1ms"
        disk_size     = 30
        count         = 13  
        tags          = { role = "dev" }
      },
      {
        name          = "wkr-2"
        instance_size = "Standard_B1ms"
        disk_size     = 50
        count         = 5
        tags          = { role = "test" }
      }
    ]
  }

}

2. Provision infrastructure with Terraform:

bash ./scripts/run_tf.sh

3. Generate Ansible inventory from Terraform state:

bash ./scripts/generate_ansible_inv.sh

4. Run Ansible playbooks to setup K3s on master and worker nodes:

bash ./scripts/run_ansible_inv.sh

Workflow Diagram

flowchart LR
    %% Subgraph: Configuration
    subgraph Config[Configuration]
        A[terraform.tfvars - cluster config]
    end

    %% Subgraph: Provisioning
    subgraph Provisioning[Provisioning]
        B[Terraform - provision VMs]
        B1[Terraform Cloud - remote state]
    end

    %% Subgraph: Secrets
    subgraph Secrets[Secrets Management]
        KV[Azure Key Vault - secrets and SSH keys]
    end

    %% Subgraph: Inventory & Setup
    subgraph Setup[Cluster Setup]
        C[Generate Ansible Inventory - from TF state]
        D[Ansible - setup K3s and roles/playbooks]
    end

    %% Subgraph: Running Cluster
    subgraph Running[Running Cluster]
        E[Running K3s Cluster - masters and workers]
    end

    %% Flow connections
    A --> B
    B --> B1
    B --> KV
    B1 --> C
    KV --> D
    C --> D
    D --> E
Loading

About

Automated deployment of lightweight Kubernetes (K3s) clusters on azure cloud VMs using Terraform and Ansible(fully automated)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published