Files
homelab-infrastructure-as-code/docs/runbooks/full-cluster-rebuild.md
T
kasun ffe976fb16
Deploy k8s Bootstrap / Bootstrap k3s Cluster (pull_request) Skipped
Deploy k8s Bootstrap / Pulumi Preview (pull_request) Successful in 48s
Secret Scan / Betterleaks (pull_request) Successful in 8s
docs: add full cluster rebuild runbook
A normal pulumi up can't rebuild the cluster: bootstrap commands run once, the later stacks' state describes the old cluster, and monitoring's import options fail on a fresh one.
2026-11-23 02:32:39 +01:00

5.8 KiB
Raw Blame History

Full cluster rebuild

Use this to rebuild the k3s cluster from new VMs. It destroys everything stored in the cluster. To change a running cluster, don't use this runbook.

This procedure has not been run end to end. Stop at any checkpoint that doesn't match, and don't work around it with --force or by clicking through a preview.

Why a normal pulumi up isn't enough

  • 02-k8s-bootstrap commands run once. They are recorded as done in state, so after new VMs appear, pulumi up would report them unchanged and install nothing.
  • 03–05 state describes the old cluster. The fixed clusterIdentifier tells Pulumi it is still the same cluster, so it would try to update resources that no longer exist.
  • 05-monitoring adopts existing resources. Its two import options fail on a fresh cluster, where those resources don't exist yet.

Before you start

  1. Save your data. Longhorn has no backup target configured, so its volumes are lost with the VMs. That includes heimdall-config and the Prometheus metrics. Grafana's database is external and is not affected.

  2. Stop automatic deploys. In Gitea's Actions tab, disable the 01, 03, 04 and 05 workflows, and don't merge anything until the last step.

  3. Back up all state. Run this from the repo root:

    mkdir -p ~/state-backups/rebuild
    for S in 01-proxmox-infra 02-k8s-bootstrap 03-k8s-infra 04-k8s-apps 05-monitoring; do
      pulumi stack export --stack dev --cwd "$S" --file ~/state-backups/rebuild/$S.json
    done
    

    These backups describe the old cluster. Once its VMs are deleted, don't restore them.

  4. Check what the rebuild depends on:

    • pfSense is running, with the control-plane VIP and HAProxy configured. They are not managed by Pulumi. pfSense is VM 200 on the Optiplex, so if you reinstall that host, restore pfSense from a config backup first.
    • The TrueNAS NFS share exists.
  5. If you reinstalled any Proxmox host:

    • Replace its CA in 02-k8s-bootstrap/certs/ (pve1 is B550, pve2 is Optiplex, pve3 is Esprimo). Get it with cat /etc/pve/pve-root-ca.pem, and never copy anything from /etc/pve/priv/.
    • Check that the host's certificate lists its current IP. /etc/hosts must have that IP before the certificate is generated.
    • Recreate the API token, then update the token and endpoint in 01-proxmox-infra config. After changing local config, update the matching <STACK>_PULUMI_DEV_YAML secret in Gitea.
    • The VM template on that host is gone too, so expect step 1 to recreate it.

1. Recreate the VMs (01-proxmox-infra)

  1. In Proxmox, delete the six k3s-* VMs. Keep the k3s-ubuntu-noble-template VMs.

  2. In pfSense, delete the six k3s-* DHCP static mappings. New VMs get new MAC addresses.

  3. Sync state and preview:

    pulumi refresh --stack dev --cwd 01-proxmox-infra
    pulumi preview --stack dev --cwd 01-proxmox-infra
    

    Checkpoint: the preview creates six VMs and six DHCP mappings, plus a template for any reinstalled host. It must show no replace or delete. These resources are protected, so if one is still in state after the refresh, find its URN with pulumi stack --show-urns and remove only that entry:

    pulumi state delete --force --stack dev --cwd 01-proxmox-infra '<urn>'
    
  4. Deploy. The VMs stay stopped until step 3.

    pulumi up --stack dev --cwd 01-proxmox-infra
    

2. Clear the state of 02–05

Only do this after the old VMs are gone. It removes Pulumi's records and doesn't touch real resources or Pulumi.dev.yaml.

for S in 05-monitoring 04-k8s-apps 03-k8s-infra 02-k8s-bootstrap; do
  pulumi state delete --all --yes --stack dev --cwd "$S"
done

Previews of 03–05 fail until step 3 is done, because 02 has no kubeconfig output yet.

3. Bootstrap k3s (02-k8s-bootstrap)

pulumi preview --stack dev --cwd 02-k8s-bootstrap
pulumi up --stack dev --cwd 02-k8s-bootstrap

Checkpoint: the preview only creates resources. The deploy starts the VMs, installs k3s on master-1, waits for the VIP, then joins the other nodes through it. If a step fails, fix the cause and run pulumi up again.

Check the new cluster with its own kubeconfig:

(umask 077; pulumi stack output kubeconfig --show-secrets --stack dev --cwd 02-k8s-bootstrap > ~/k3s-rebuilt.yaml)
KUBECONFIG=~/k3s-rebuilt.yaml kubectl get nodes

Checkpoint: all six nodes are Ready. Then replace your normal kubeconfig with this file, because the old one no longer works.

4. Deploy 03, 04 and 05

Deploy these through Gitea, because the runners have the helm CLI that 03 needs. Re-enable each workflow as you reach it, and wait for each run to finish.

  1. Run the Deploy k8s Infra workflow manually.
  2. Run the Deploy k8s Apps workflow manually.
  3. Open a PR that removes both import options from 05-monitoring/index.ts, unless they are already gone. Re-enable the Deploy Monitoring workflow and merge the PR, which deploys 05. If the options were already gone, run the workflow manually instead.

5. Verify

kubectl get nodes
kubectl get pods -A --field-selector=status.phase!=Running,status.phase!=Succeeded
kubectl get pvc -A
kubectl get nodes.longhorn.io -n longhorn-system
kubectl get certificates -A

Checkpoint: nodes are Ready, no pods are unhealthy, PVCs are Bound, and certificates become Ready. New certificates come from Let's Encrypt, which rate-limits repeated requests for the same hostnames, so avoid repeated rebuilds in a short period.

Run pulumi preview --stack dev --cwd <stack> for all five stacks. None should show changes. Re-enable the 01 workflow, restore any data you saved, and delete ~/k3s-rebuilt.yaml and ~/state-backups/rebuild.