In Part 1 (opens in a new tab) I explained why jithendar.com is leaving mk4, my Hetzner cluster. This part is the build. Contabo gives you cheap servers and little around them: no load balancer, an API with gaps, and a Terraform provider that can't list the servers in your account. I still wanted the whole cluster to come from one GitHub Actions workflow, with nothing run from my laptop.

Below: Terraform that finds the servers and puts them on a private network, Ansible that locks down SSH and installs k3s, HAProxy in front of the Istio gateway, TLS, and the errors from the runs that failed.

This is part 2 of a six-part series on moving jithendar.com's k3s cluster from Hetzner to Contabo.

How the Contabo k3s cluster is built, in four bands. Top, GitHub Actions: the Create Contabo k3s workflow, run by hand on master and never cancelled mid-apply, runs a control plane job (plan, guards, then apply; firewall, k3s server), then one worker job per worker in parallel (k3s agent), then a check job (the inventory equals the plan's nodes, every node Ready), then the infrastructure job (the Helm stack with its own state, then the HAProxy edge). Second, Terraform in contabo-k3s/terraform with its state in S3 reached through GitHub OIDC: discover-nodes.sh lists the servers through the Contabo API; contabo_instance takes a server over and reinstalls it, and destroying one only cancels it; a private network, one per data center, where a server gets its card after a restart; Cloudflare DNS with one proxied A record per worker and no health checks; ansible_host resources keep the inventory in the state, so there is no hosts file. Third, Ansible in contabo-k3s/ansible, run by reconcile.sh with the inventory read from the Terraform state: prepare.yml (ufw denies by default, SSH keys only), private-network.yml (restart through the API, netplan the address), flannel-backend.yml (WireGuard to VXLAN, drops flannel-wg), k3s-ansible pinned to commit f6b040a installing k3s v1.37.2+k3s1, and edge.yml (HAProxy on the workers, 80 and 443 to NodePorts). Bottom, a request today with load_balancer set to dns: visitors reach Cloudflare (proxy and CDN on, SSL Full strict), then HAProxy on port 443 of one worker, which sends the PROXY protocol v2 header to NodePort 31443 of istio-ingressgateway, where kube-proxy picks a gateway pod; TLS ends on the Istio gateway with cert-manager certificates from a DNS-01 challenge; then the applications. A note: Cloudflare picks one worker's record per request and tries another when a worker does not answer, with no health checks; master-1, a Cloud VPS 4, runs only the control plane with the CriticalAddonsOnly taint, and its API port 6443 answers the admin addresses only; the three workers are Cloud VPS 6 NVMe.

What I'm working with

Four servers in Contabo's EU region: a Cloud VPS 4 for the control plane and three Cloud VPS 6 NVMe workers, each ready within a couple of minutes of ordering. I buy servers in the control panel and Terraform takes them over. The one file I edit is contabo-k3s/terraform/nodes.auto.tfvars in k8s-deployments (opens in a new tab) (comments removed):

HCL
plans = {
  V153 = "server" # Cloud VPS 4: the k3s control plane (exactly one)
  V166 = "agent"  # Cloud VPS 6 NVMe: the workers
}
private_network = true
load_balancer = "dns"
region = "EU"

Everything is pinned: Terraform 1.16.6, providers contabo/contabo ~> 0.1.44 and cloudflare/cloudflare ~> 5.29, ansible-core 2.18.20, and k3s v1.37.2+k3s1, as on mk4.

Terraform: finding the servers

The Contabo provider has no data source that lists servers; every one wants an id. So Terraform asks the API through a script (contabo-k3s/terraform/nodes.tf, postconditions left out):

HCL
data "external" "servers" {
  program = ["bash", "${path.module}/../discover-nodes.sh"]
  query = {
    cluster = var.cluster_name
    region  = var.region
    plans   = jsonencode(var.plans)
  }
}

discover-nodes.sh pages through /v1/compute/instances with curl and jq. A server is a node when it's in the region, isn't cancelled, has one of the plans, and its display name is empty, the default, or starts with jithendar-. To take a server out, I rename it in the panel.

On Contabo, changing a server's image, SSH keys or default user reinstalls it, and destroying one only cancels the contract: it keeps running, and billing, until the term ends. So the resource ignores everything that would wipe a server it already has (trimmed):

HCL
resource "contabo_instance" "node" {
  for_each             = local.nodes
  display_name         = "${var.cluster_name}-${each.key}"
  existing_instance_id = each.value.instance_id

  lifecycle {
    ignore_changes = [image_id, ssh_keys, default_user, product_id, region, period, existing_instance_id]
  }
}

ansible/drain-removed.yml uninstalls the k3s agent from a removed worker first, or the still-running server would register itself again.

The workflow, .github/workflows/create-contabo-k3s.yml, stops an apply unless I tick allow_new_servers, allow_cancel or allow_dns_takeover, and never cancels a running apply. The repository and its logs are public, so all output goes through a sed program in env.REDACT that masks addresses and ids. The S3 state is reached through GitHub OIDC, limited to this cluster's state object and lock.

The region mistake and the private network

My first control plane ended up in Contabo's India region, by a manual mistake. The first successful run, on 30 November, built the cluster anyway, with flannel over WireGuard between India and the EU. It took 16 minutes 25 seconds; the task "Enable and start K3s agent" alone took 7 minutes 53 seconds.

After checking, I moved the control plane to the EU and put all four servers on a Contabo private network (terraform/private_network.tf). A server only gets its card after a restart, so ansible/private-network.yml restarts each one without a card through the Contabo API, compares /proc/sys/kernel/random/boot_id to be sure it rebooted, and writes the address to /etc/netplan/60-contabo-private.yaml.

The k3s arguments are built per node in terraform/inventory.tf. For the control plane they come out like this (addresses replaced):

--node-name=master-1 --node-external-ip=<public> --node-ip=<private>
--tls-san=<public> --tls-san=<private>
--node-taint=CriticalAddonsOnly=true:NoExecute
--disable=traefik --disable=servicelb --disable=local-storage

No --flannel-backend any more: flannel uses its default VXLAN over the private network, like mk4, and ansible/flannel-backend.yml deletes the old flannel-wg interface. Traefik, ServiceLB and local storage are off because the platform stack brings its own. The next run took 8 minutes 33 seconds, and starting the agents took 12 seconds instead of almost eight minutes.

Ansible reads its inventory from the state

There's no hosts file: Terraform writes ansible_host resources into its state, and the cloud.terraform.terraform_provider inventory plugin reads them. k3s comes from k3s-ansible (opens in a new tab), pinned in ansible/requirements.yml to commit f6b040a (main, 17 November), because the workflow runs it with the SSH key and the join secret. manage_firewall: false is set because its own rules would open the API, kubelet and flannel ports to everyone. ansible/prepare.yml owns the firewall: SSH with keys only, ufw denying by default, 6443 open only to the ADMIN_CIDRS secret, everything between the nodes, 80/443 on the workers.

What's open today: SSH from anywhere, because GitHub's runners change addresses (keys only), and 80/443 on the workers, where the Istio gateway itself only lets Cloudflare's addresses through. Turning on Contabo's firewall is next. [ASK JITHENDAR: what will Contabo's firewall allow: 80/443 from Cloudflare's ranges only, and 22 from where, given GitHub's runners change addresses?]

Every Ansible task is an SSH round trip from the runner, so ansible/ansible.cfg cuts them down:

INI
[defaults]
forks = 50

[ssh_connection]
pipelining = True
ssh_args = -C -o ControlMaster=auto -o ControlPersist=30m

With that, plus installing the control plane once and checking the cards in parallel, a run took 3 minutes 14 seconds. One job per worker made it 4 minutes 52 seconds, but I kept it: a failed worker no longer stops the others.

The edge, without a load balancer

Contabo doesn't sell a load balancer. I first tried Cloudflare Load Balancing (it failed, below), a paid add-on I decided wasn't worth it for this site. So the cluster uses round-robin DNS, one proxied A record per worker (terraform/cloudflare.tf, trimmed):

HCL
resource "cloudflare_dns_record" "web" {
  for_each = toset(local.cf_record)
  name     = var.cloudflare.hostname
  type     = "A"
  content  = local.ipv4[each.key]
  proxied  = true
}

Cloudflare picks a record per request and retries another if a worker doesn't answer; there are no health checks.

On mk4, Hetzner's load balancer sent the PROXY protocol header the Istio gateway requires. Here ansible/edge.yml puts HAProxy on every worker to do that (templates/edge-haproxy.cfg.j2):

frontend edge_{{ port }}
    bind :{{ port }}
    default_backend gateway_{{ port }}

backend gateway_{{ port }}
    server gateway {{ private_ip | default(ansible_host, true) }}:{{ node_port }} send-proxy-v2 check inter 5s

Port 80 goes to NodePort 31080 and 443 to 31443 (deploy/minikube/istio/ingressgateway-nodeport.yaml). TLS ends on the gateway, with Let's Encrypt certificates from a DNS-01 challenge at Cloudflare (cert-manager, ClusterIssuer letsencrypt-dns). Cloudflare runs with the proxy and CDN on and SSL mode Full (strict), so it checks that certificate on every connection to a worker.

Problems I hit

Cloudflare Load Balancing refused, twice. The first apply, run 46362651229, had already ordered the control plane and taken over the workers when Cloudflare said no:

POST ".../accounts/<hidden>/load_balancers/monitors": 403 Forbidden
{"success":false,"errors":[{"code":10000,"message":"Authentication error", ...}]}

The token lacked the Load Balancing permissions; the plan step now tests the token and names a missing permission before anything is ordered. With a fixed token, run 46367233390 failed differently:

400 Bad Request
"code": 1002, "message": "interval is not in range [1, 1]: validation failed"

That's the answer when the Load Balancing add-on isn't active, and where I switched to round-robin DNS.

The drain couldn't reach the moved control plane. Run 46375578388, right after the move:

fatal: [master-1]: UNREACHABLE! => {..., "msg": "Failed to connect to the host via ssh: ssh: connect to host x.x.x.x port 22: Connection timed out", ...}

The drain runs before terraform apply, so the inventory still held the old address. reconcile.sh now passes the drain each server's current address from the plan's refreshed state.

worker-3 never got its network card. It came back from the restart without one (run 46382115311). Its status in the private network was "reinstall", and the playbook now stops with that advice (run 46384765022):

Contabo says worker-3 must be REINSTALLED to join the private network (status "reinstall"); a restart does not add its card.

I reinstalled it in the panel with the same key. Its first boot then held the apt lock (Could not get lock /var/lib/dpkg/lock-frontend, run 46385920163), so apt now waits up to 600 seconds.

k3s's own StorageClass blocked Helm. Run 46401785352, the first install of the platform stack:

Unable to continue with install: StorageClass "local-path" in namespace ""
exists and cannot be imported into the current release: invalid ownership metadata; ...

I had run scope "infrastructure only", so the cluster stage that adds --disable=local-storage never ran. The job now checks for k3s's add-ons first and says which scope to run.

Gatekeeper's webhook blocked every namespace. Run 46405790646:

Internal error occurred: failed calling webhook "check-ignore-label.gatekeeper.sh": ...
no endpoints available for service "gatekeeper-webhook-service"

On an empty cluster the webhook existed before any Gatekeeper pod was ready, and it fails closed. Gatekeeper now goes first, and the job waits for its endpoint.

Verify it works

Copy the kubeconfig (outside ADMIN_CIDRS, add --tunnel <port>):

Bash
contabo-k3s/download-kubeconfig.sh -i ~/.ssh/<key> --merge
KUBECONFIG=~/.kube/contabo-k3s.yaml kubectl get nodes -o wide -L kubernetes.io/arch

On 1 December, trimmed:

NAME       STATUS   ROLES           VERSION        OS-IMAGE             KERNEL-VERSION      ARCH
master-1   Ready    control-plane   v1.37.2+k3s1   Ubuntu 24.04.5 LTS   6.8.0-147-generic   amd64
worker-1   Ready    <none>          v1.37.2+k3s1   Ubuntu 24.04.5 LTS   6.8.0-151-generic   amd64
worker-2   Ready    <none>          v1.37.2+k3s1   Ubuntu 24.04.5 LTS   6.8.0-151-generic   amd64
worker-3   Ready    <none>          v1.37.2+k3s1   Ubuntu 24.04.5 LTS   6.8.0-147-generic   amd64

The certificates (two of four rows):

Bash
kubectl get certificate -A
NAMESPACE       NAME                     READY   SECRET                       AGE
istio-ingress   jithendar-com            True    jithendar-com-gateway-tls    15h
jithendar       jithendar-com-wildcard   True    jithendar-com-wildcard-tls   16h

The workflow checks on every apply too: the check job (ansible/verify.yml) compares the inventory with the plan's nodes and waits for each to be Ready (run 46394503040 printed ok: [master-1] => (item=worker-3) and so on for all four). The infrastructure job then calls curl --resolve "jithendar.com:443:$worker" https://jithendar.com/grafana/api/health through one worker's HAProxy. From a runner the gateway answers 403, refusing an address that isn't Cloudflare's, which proves the path works. Run 46418346829 printed edge -> 403.

Key takeaways

  • When the provider can't list servers, a data "external" script over the API works, with a postcondition checking what it found.
  • On Contabo, treat image, keys and user as create-only, and drain before you destroy: destroy only cancels.
  • Put the nodes on a private network before installing k3s: one agent start went from almost eight minutes to 12 seconds.
  • No load balancer is workable: round-robin DNS at Cloudflare, HAProxy on each worker sending PROXY v2 to a NodePort gateway.
  • SSH and 80/443 are still open at the host firewall; Contabo's firewall is next.

Previous: Part 1: Why I moved k3s from Hetzner to Contabo, and the plan (opens in a new tab) · Next: Part 3: Rebuilding the images for arm64 and amd64 (opens in a new tab)