Terraform and Ansible code to deploy an HA K3s cluster on Proxmox
README.md

proxmox-k3s #

Terraform + Ansible to deploy a production-grade k3s HA cluster on Proxmox VE.

What you get:

  • 3 control plane nodes with embedded etcd (HA quorum)
  • 2+ worker nodes
  • kube-vip for the API server floating VIP (ARP mode)
  • MetalLB for LoadBalancer services (L2/ARP mode)
  • cert-manager + OpenBao (Vault-compatible) for internal TLS - Work in progress

Prerequisites #

  • Proxmox VE 8.x with 2+ nodes
  • A subnet with several free static IPs (VMs, VIP, MetalLB pool)
  • mise installed locally
  • Terraform ≥ 1.3 (installed automatically by mise)
  • Ansible installed locally (pip install ansible or your package manager)
  • SSH key pair for VM access

Quick Start #

1. Clone and configure #

git clone https://github.com/YOUR_USER/proxmox-k3s.git
cd proxmox-k3s

# Terraform credentials and VM layout
cp terraform/terraform.tfvars.example terraform/terraform.tfvars
$EDITOR terraform/terraform.tfvars

# Ansible inventory (VM IPs, VIP, MetalLB pool)
cp ansible/inventory.yml.example ansible/inventory.yml
$EDITOR ansible/inventory.yml

2. Create VM templates on each Proxmox node #

Each Proxmox node needs a local Ubuntu 24.04 cloud image template. Run once per node:

export PROXMOX_NODE_1=192.168.X.10   # your first Proxmox node IP
export PROXMOX_NODE_2=192.168.X.20   # your second Proxmox node IP
mise run make-templates

The script SSH's into each node as root and runs wget + qm create + qm template. Override STORAGE or BRIDGE env vars if your Proxmox uses non-default names.

3. Provision VMs with Terraform #

mise run tf-init
mise run tf-plan    # review what will be created
mise run tf-apply

4. Deploy k3s #

mise run ansible-ping    # verify SSH works to all 5 VMs
mise run ansible-deploy  # installs k3s across all nodes (~5-10 min)

5. Access the cluster #

export KUBECONFIG=~/kubeconfig-k3s.yaml
kubectl get nodes -o wide

Add-ons #

MetalLB (LoadBalancer services) #

Set metallb_ip_range in ansible/inventory.yml, then:

mise run install-metallb

cert-manager #

mise run install-cert-manager

OpenBao (internal CA) #

# 1. Install (3-replica Raft HA)
mise run install-openbao

# 2. Initialize (run once, save your unseal keys and root token securely)
kubectl exec -n openbao openbao-0 -- bao operator init -key-shares=5 -key-threshold=3

# 3. Unseal all 3 pods (3 of 5 keys each)
kubectl exec -n openbao openbao-0 -- bao operator unseal <key>
# ... repeat for openbao-1 and openbao-2

# 4. Configure PKI + cert-manager ClusterIssuer (prompts for root token)
mise run configure-openbao

Configuration Reference #

terraform/terraform.tfvars #

Variable Description
proxmox_endpoint Proxmox API URL, e.g. https://192.168.1.10:8006/
proxmox_api_token USER@REALM!TOKEN_ID=SECRET
vm_gateway Subnet gateway
vm_dns_servers DNS servers for cloud-init
template_vm_ids Map of Proxmox node name → template VM ID
control_planes Map of hostname → { node, vm_id, ip } (exactly 3)
workers Map of hostname → { node, vm_id, ip }

ansible/inventory.yml #

Variable Description
k3s_version k3s version to install
k3s_vip Floating API server VIP (kube-vip)
kube_vip_version kube-vip version
metallb_ip_range IP range for MetalLB pool (e.g. 192.168.1.200-192.168.1.220)

Architecture #

                        ┌─────────────────────────────────────────┐
                        │            Proxmox Cluster               │
                        │                                          │
                        │  ┌──────────┐      ┌──────────────────┐ │
                        │  │  node1   │      │      node2       │ │
                        │  │          │      │                  │ │
                        │  │ k3s-cp-1 │      │    k3s-cp-3      │ │
                        │  │ k3s-cp-2 │      │  k3s-worker-2    │ │
                        │  │ k3s-wkr-1│      │                  │ │
                        │  └──────────┘      └──────────────────┘ │
                        └─────────────────────────────────────────┘
                                         │
                              VIP (kube-vip, ARP)
                                  :6443 API

Storage: Each Proxmox node uses local-lvm (non-shared). VM templates must be created on each node separately, which is why template_vm_ids is a per-node map.

kube-vip: Runs as a DaemonSet on control-plane nodes only. Deployed via k3s auto-manifest directory so it starts before CP2/CP3 attempt to join.

etcd: Embedded in k3s. Requires exactly 3 control-plane nodes for quorum. Losing 2 of 3 control planes makes the cluster read-only.