This repository contains Terraform config to automatically provision a lightweight Kubernetes cluster using k3s on Proxmox VE. It creates a cluster with one master node and two worker nodes, all running Ubuntu Server.
My primary goal is reproducability. I try to achieve this by:
- Infrastructure as Code (IaC) using Terraform to create and manage VMs in Proxmox
- Configuration management with Ansible to consistently deploy and configure K3s across nodes
The infrastructure consists of:
- 1 k3s master node (VM ID: 200)
- 2 k3s worker nodes (VM IDs: 201, 202)
- All nodes are created from an Ubuntu cloud image template
- Nodes are configured with static IP addresses
- All VMs are placed in a dedicated Proxmox resource pool named "k3s"
- Proxmox VE server (tested with version 8.x)
- SSH key pair for VM access
- Ubuntu cloud image template in Proxmox
- Terraform installed locally
- Proxmox API token with appropriate permissions
- Ansible installed locally (for k3s setup)
-
Clone this repository
-
Copy
terraform.tfvars.example
toterraform.tfvars
:cp terraform.tfvars.example terraform.tfvars
-
Edit
terraform.tfvars
with your specific configuration:- Proxmox API credentials
- Network settings
- SSH public key
- VM resource allocations
-
Initialize and apply Terraform:
terraform init terraform plan terraform apply
-
Set up k3s using Ansible:
cd ansible ansible-playbook -i hosts setup-k3s.yml
First, create the Ubuntu cloud image template in Proxmox:
# Download Ubuntu Cloud Image
wget https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img
# Install libguestfs-tools
apt-get install -y libguestfs-tools
# Create VM template (ID 9000)
qm create 9000 --name ubuntu-cloud-init-template --memory 2048 --cores 2 --net0 virtio,bridge=vmbr0
qm importdisk 9000 focal-server-cloudimg-amd64.img local-zfs
qm set 9000 --scsihw virtio-scsi-pci --scsi0 local-zfs:vm-9000-disk-0
qm set 9000 --ide2 local-zfs:cloudinit
qm set 9000 --boot c --bootdisk scsi0
qm set 9000 --agent 1
qm template 9000
Edit terraform.tfvars
with your specific settings
Run Terraform to create the infrastructure:
terraform init
terraform plan
terraform apply
The Ansible playbooks are located in the ansible
directory. The setup process is automated and will:
-
Configure all nodes with:
- System updates
- Required packages
- Disabled swap
- Required kernel modules and parameters
-
Set up the master node with:
- k3s server installation
- Systemd service configuration
- Automatic kubeconfig generation
-
Configure worker nodes with:
- k3s agent installation
- Systemd service configuration
- Automatic cluster joining
To run the setup:
cd ansible
ansible-playbook -i hosts setup-k3s.yml
The playbook will automatically:
- Use the correct IP addresses from your Terraform configuration
- Generate and save the kubeconfig file locally
- Configure all necessary system requirements
- Set up the complete k3s cluster
After the infrastructure and k3s are set up:
-
Access the cluster:
# The kubeconfig file will be automatically saved in the ansible directory export KUBECONFIG=./ansible/k3s.yaml kubectl get nodes
-
Verify the cluster:
kubectl get nodes kubectl get pods -A