Sub topic 3
Taking an etcd snapshot with etcdctl
This chapter assumes you can SSH to a kubeadm control plane node, run kubectl, and know that etcd holds the cluster state. You will save a point-in-time snapshot of etcd with etcdctl.
What a snapshot contains
A snapshot is a single file holding the full etcd keyspace at one revision. It includes every Kubernetes object: Deployments, Secrets, ConfigMaps, RBAC rules and more. It does not include the contents of persistent volumes. Those need a separate backup, covered later with Velero.
Because the file contains Secrets in plain form, treat it as sensitive.
Find the connection details
etcd on a kubeadm cluster listens with TLS and requires client certificates. The default locations are:
| Setting | Default kubeadm value |
|---|---|
| Endpoint | `https://127.0.0.1:2379` |
| CA certificate | `/etc/kubernetes/pki/etcd/ca.crt` |
| Client certificate | `/etc/kubernetes/pki/etcd/server.crt` |
| Client key | `/etc/kubernetes/pki/etcd/server.key` |
If your cluster differs, check the flags in /etc/kubernetes/manifests/etcd.yaml. Look at --listen-client-urls, --trusted-ca-file, --cert-file and --key-file.
Save the snapshot
Run the command on a control plane node. Create the target directory first if it does not exist.
ETCDCTL_API=3 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/backups/etcd-snapshot.dbETCDCTL_API=3 selects the v3 API. Recent etcdctl versions default to v3, so the variable is harmless there but needed on old ones. The snapshot save subcommand streams a consistent copy from the member you connect to. It does not stop etcd or block writes.
Common errors
- Certificate errors or "context deadline exceeded": the endpoint, CA, certificate or key path is wrong. Compare against the etcd manifest.
- "permission denied": the key files are readable only by root. Run the command with
sudo, keeping theETCDCTL_API=3variable (sudo ETCDCTL_API=3 etcdctl ...). - "command not found": install
etcdctl, or run it inside the etcd Pod withkubectl exec -n kube-system. Then copy the file out, because the Pod's filesystem is not a safe place to keep it. - Snapshot lands on the same disk as etcd: a disk failure then loses both. Copy the file off the node as soon as it is saved.
Exercise
Your snapshot command fails with "context deadline exceeded" on a kubeadm cluster. Name two things to check, and say where to find the correct values.
Show answer
Check that the --endpoints address and port match what etcd listens on, and that the --cacert, --cert and --key paths point to the right files. The correct values are in the etcd static Pod manifest at /etc/kubernetes/manifests/etcd.yaml, in flags such as --listen-client-urls, --trusted-ca-file, --cert-file and --key-file.
Exercise
Does a snapshot taken with etcdctl snapshot save back up the data stored inside your PersistentVolumes?
Show answer
No. It captures only the Kubernetes objects stored in etcd, such as the PersistentVolumeClaim definitions. The data on the volumes needs a separate backup, for example with Velero.