Back Up and Restore etcd on a kubeadm Cluster

In progress Kubernetes Linux

Goal

Take scheduled etcd snapshots on a kubeadm control plane, keep them on the node, on a NAS and on a workstation, prove they restore without touching the live cluster, and know the steps of a real restore. This is the manual version of what playbook 10-etcd-backup.yaml does in my homelab repo.

Prerequisites

  • A kubeadm control plane with stacked etcd (the static pod in /etc/kubernetes/manifests/etcd.yaml).
  • An NFS share for backups: a dedicated dataset, exported only to the control-plane IPs, with root mapped to root so a root job can write (see Provide NFS Storage to Kubernetes).
  • nfs-common, zstd and tar on the node.

Steps

Install etcdctl and etcdutl at the Running Version

kubeadm runs etcd from an image, so the node has no etcd tools. Use the release that matches the image tag:

grep 'image:' /etc/kubernetes/manifests/etcd.yaml        # e.g. registry.k8s.io/etcd:3.6.8-0 -> 3.6.8
V=3.6.8
cd /tmp && curl -LO https://github.com/etcd-io/etcd/releases/download/v$V/etcd-v$V-linux-amd64.tar.gz
curl -LO https://github.com/etcd-io/etcd/releases/download/v$V/SHA256SUMS
sha256sum --ignore-missing -c SHA256SUMS                  # must print OK
sudo mkdir -p /opt/etcd-v$V && sudo tar -xzf etcd-v$V-linux-amd64.tar.gz -C /opt/etcd-v$V --strip-components=1
sudo ln -sfn /opt/etcd-v$V /opt/etcd
sudo ln -sf /opt/etcd/etcdctl /opt/etcd/etcdutl /usr/local/bin/

The etcd server binary stays out of PATH: only the restore drill uses it.

Take a Snapshot and Bundle It with the PKI

  1. Snapshot through the local member with the read-only healthcheck-client certificate kubeadm creates:
    P=/etc/kubernetes/pki/etcd
    sudo etcdctl --endpoints=https://127.0.0.1:2379 --cacert=$P/ca.crt \
      --cert=$P/healthcheck-client.crt --key=$P/healthcheck-client.key snapshot save /tmp/snapshot.db
    sudo etcdutl snapshot status /tmp/snapshot.db -w table     # hash, revision, total keys
    
  2. Bundle the snapshot with /etc/kubernetes/pki and the manifests: a restore on a rebuilt node needs the CA keys too (see An etcd Snapshot Alone Can't Rebuild a kubeadm Control Plane, So Back Up the PKI with It). Keep it root-only:
    (umask 077; sudo tar --zstd -cf etcd-$(hostname -s)-$(date -u +%Y%m%dT%H%M%SZ).tar.zst \
      -C /tmp snapshot.db -C / etc/kubernetes/pki etc/kubernetes/manifests)
    
  3. Write a .sha256 next to each bundle and check it again after copying it anywhere.

Mount the NAS Only When Needed

A systemd automount, with soft NFS options, so a NAS outage makes the copy fail instead of hanging boot or the job. The unit name must match the path (/mnt/backups → mnt-backups):

# /etc/systemd/system/mnt-backups.mount
[Mount]
What=<nas>:/mnt/<pool>/backups
Where=/mnt/backups
Type=nfs
Options=soft,timeo=100,retrans=3,noatime,_netdev

# /etc/systemd/system/mnt-backups.automount
[Automount]
Where=/mnt/backups
TimeoutIdleSec=600
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload && sudo systemctl enable --now mnt-backups.automount

Schedule It

Put the snapshot, bundle, copy and pruning in one script (/usr/local/sbin/etcd-backup): write locally first, copy to the NAS under a temporary name, rename, re-check the checksum there, keep the newest N in each place. Exit with a distinct status when only the NAS copy failed. Then a oneshot service and a timer:

# /etc/systemd/system/etcd-backup.service
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/etcd-backup
Nice=10
IOSchedulingClass=idle

# /etc/systemd/system/etcd-backup.timer
[Timer]
OnCalendar=*-*-* 00/6:00:00
RandomizedDelaySec=15min
Persistent=true
[Install]
WantedBy=timers.target

Persistent=true runs a missed backup at the next boot. Keep a third copy off the NAS (a workstation pulling over SSH) until the pool is mirrored.

Prove It: Restore Drill

Restore the bundle into a throwaway etcd on loopback, read it, compare it with the live one. The live etcd is only read:

D=$(sudo mktemp -d) && sudo tar --zstd -xf <bundle> -C $D
sudo etcdutl snapshot restore $D/snapshot.db --data-dir $D/data --name drill \
  --initial-cluster drill=http://127.0.0.1:12380 --initial-advertise-peer-urls http://127.0.0.1:12380
sudo /opt/etcd/etcd --name drill --data-dir $D/data \
  --listen-client-urls http://127.0.0.1:12379 --advertise-client-urls http://127.0.0.1:12379 \
  --listen-peer-urls http://127.0.0.1:12380 --initial-advertise-peer-urls http://127.0.0.1:12380 \
  --initial-cluster drill=http://127.0.0.1:12380 &
etcdctl --endpoints=http://127.0.0.1:12379 get /registry/namespaces/ --prefix --keys-only
# stop it (kill %1) and remove $D

Run it monthly and after every etcd upgrade.

Restore for Real (One Control Plane)

Everything written after the snapshot is lost.

  1. Move kube-apiserver.yaml and etcd.yaml out of /etc/kubernetes/manifests; the kubelet stops both pods. Wait until crictl ps no longer lists them.
  2. Move /var/lib/etcd aside, then restore with the member name and peer URL from the bundled etcd.yaml:
    sudo etcdutl snapshot restore snapshot.db --data-dir /var/lib/etcd \
      --name <node> --initial-cluster <node>=https://<node-ip>:2380 \
      --initial-advertise-peer-urls https://<node-ip>:2380 \
      --bump-revision 1000000000 --mark-compacted
    
    The two last flags keep revisions moving forward and end every watch, so controllers re-list instead of trusting caches newer than the snapshot.
  3. Put the manifests back, check kubectl get nodes and get pods -A, then restart the CNI agents, CoreDNS and any controller that misbehaves.

On a rebuilt machine, restore /etc/kubernetes/pki from the bundle before kubeadm init, with the same hostname and IP. With several control planes, stop all of them and restore the same snapshot on each, with its own name and the full --initial-cluster list.

Verify

  • systemctl list-timers etcd-backup.timer shows the next run.
  • journalctl -u etcd-backup.service shows the key count, the local file and the NAS copy with a matching checksum.
  • A copy exists off the NAS, with a checked sha256.
  • The restore drill passes, with key counts close to the live ones (events and leases change between the two).

Troubleshooting

  • The local copy works, the NAS copy doesn't, and mnt-backups.mount is failed: the share isn't exported to this node (showmount -e <nas>), or root is squashed. Fix the share, then sudo systemctl reset-failed mnt-backups.mount before retrying.
  • journalctl -u etcd-backup.service prints -- No entries --: a regular user only sees their own journal. Use sudo.
  • etcdctl gets x509: certificate signed by unknown authority: the etcd CA (pki/etcd/ca.crt) is a different CA from the cluster's (pki/ca.crt). Use the etcd one.

References