Back up and restore etcd
In this task you'll save the cluster's state and bring it back after a mistake.
Every object in a cluster lives in etcd, a small key-value database on the control plane. A snapshot of etcd is a backup of the whole cluster's state (not of the data in volumes). On clusters you build yourself, you take snapshots with etcdctl using the etcd certificates, store them off the node, and restore by pointing etcd at a restored data folder.
Managed services such as AKS run etcd for you and don't let you restore it, so there you back up what you deploy instead: keep manifests in Git (GitOps), and use a tool such as Velero or AKS Backup for objects and volumes. Practising an etcd restore on kind teaches how the control plane hangs together.
Example. Fernhill Cloud's self-managed training cluster takes an etcd snapshot every six hours to a storage account. When a script deleted the wrong namespace, they restored the morning snapshot and lost only two hours of changes.
Before you start
You need: Docker Desktop, kind and kubectl on your computer (all free), and a cluster from Run a practice cluster on your computer. Time: 35 minutes.
Where to look
- Terminal
- kubectl exec
- etcdctl
Steps
- Find the etcd pod and its certificate paths:
kubectl get pods -n kube-system -l component=etcd, then look for cert-file, key-file and trusted-ca-file in /etc/kubernetes/manifests/etcd.yaml on the control plane (docker exec lab-control-plane cat /etc/kubernetes/manifests/etcd.yaml).On kind, etcdctl lives inside the etcd pod, not on the node. - Create a namespace
keepmewith a ConfigMap in it, so you have something to lose. - Take a snapshot with etcdctl inside the etcd pod (see Commands), saving it under /var/lib/etcd so it lands on the node's disk, and check it with
etcdutl snapshot status. - Delete the namespace
keepme.This is the mistake to undo. - Restore the snapshot to a new folder with
etcdutl snapshot restore --data-dir /var/lib/etcd/restored, then point the etcd manifest's data volume at that folder; the kubelet restarts etcd by itself. - After a few seconds, check the namespace and its ConfigMap are back, then note what was lost: anything created after the snapshot.
Commands
# kind: etcdctl and etcdutl run inside the etcd pod
E="kubectl exec -n kube-system etcd-lab-control-plane --"
$E etcdctl --endpoints=https://127.0.0.1:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key snapshot save /var/lib/etcd/backup.db
$E etcdutl snapshot status /var/lib/etcd/backup.db -w table
# Restore to a new folder, then point the manifest's etcd-data hostPath at it
$E etcdutl snapshot restore /var/lib/etcd/backup.db --data-dir /var/lib/etcd/restored
docker exec lab-control-plane sed -i 's#path: /var/lib/etcd$#path: /var/lib/etcd/restored#' /etc/kubernetes/manifests/etcd.yaml
Watch out for
- Cost
- Free on kind.
- Security
- A snapshot holds every Secret in the cluster: encrypt it and store it as carefully as a password.
- Common mistakes
- Saving snapshots only on the control plane node, never testing a restore, and assuming a snapshot includes the data in persistent volumes.
Clean up
Delete the kind cluster and recreate it if the restore left it unhealthy: kind delete cluster --name lab.
Words to know
- etcd
- The database that stores every Kubernetes object.
- Snapshot
- A point-in-time copy of etcd's data.
- etcdctl
- The command-line tool for etcd: health, snapshots and more.
- Velero
- A tool that backs up Kubernetes objects and volumes to storage.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands OnKeep going
- Kubernetes roadmap: the skills behind the CKA and CKADExplained
- Keeping a cluster current and recoverable: upgrades and backupsExplained
- Add startup, readiness and liveness probesPrevious in Kubernetes skills
- Plan a cluster version upgradeNext in Kubernetes skills
- Containers and KubernetesAll pages in this topic