Lesson 67 · Section 6: Cluster Maintenance
Lesson 67: Working with etcdctl and etcdutl: the right tool for each backup and restore step
How to choose between etcdctl and etcdutl, find where etcd runs (stacked or external), and carry a restore through on a static Pod, on a systemd service and on a cluster with several etcd members. · 16 min read · 4 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why two tools get a lesson of their own
In etcd commands you met both tools and ran a backup and a restore on a kubeadm control plane. That is the happy path. In practice, and in exam tasks, three things get in the way:
- The command you remember belongs to the other tool. Tutorials written before etcd 3.6 restore with
etcdctl. Kubernetes 1.34 and later ship etcd 3.6, where that command no longer exists. - etcd is not where you expect it. Some clusters run etcd as a static Pod next to the API server; others run it on separate hosts as a systemd service. The restore command is the same, but everything around it changes.
- The cluster has more than one etcd member, and a restore has to be done for each of them.
This lesson is about getting through those cases without guessing.
Online and offline
Parts:
- You (shell on the etcd host)
- etcdctl (online: needs TLS)
- etcdutl (offline: no flags)
- Running etcd (port 2379)
- Data directory (/var/lib/etcd)
- Snapshot file (/opt/backup/etcd.db)
Connections:
- You to etcdctl
- You to etcdutl
- etcdctl to Running etcd: API call
- Running etcd to Data directory: owns
- etcdutl to Snapshot file: opens
- etcdutl to Data directory: only when etcd is stopped
etcdctl is a network client: every command is a request to a running etcd server, which answers from its data
directory. etcdutl is a file utility: it opens a snapshot file or a data directory on the local disk and never
makes a connection. That is why it was split out of etcdctl in etcd 3.5: disaster recovery is exactly the moment
when no server is running, and a tool for it should not carry a network client's flags.
Several jobs exist in both forms. Pick by whether etcd is up:
| Job | Online, with etcdctl | Offline, with etcdutl |
|---|---|---|
| Make a backup | etcdctl snapshot save <file> | None: only the server can produce a consistent copy |
| Inspect a backup | None since 3.6 | etcdutl snapshot status <file> -w table |
| Restore a backup | None since 3.6 | etcdutl snapshot restore <file> --data-dir <dir> |
| Reclaim disk space | etcdctl defrag (the member pauses while it runs) | etcdutl defrag --data-dir <dir> (etcd must be stopped) |
| Size and revision | etcdctl endpoint status -w table | etcdutl snapshot status <file> -w table |
| Tool version | etcdctl version | etcdutl version |
| Health, members, keys | etcdctl endpoint health, member list, get | None: these are questions for a live server |
What moved, by etcd version:
| etcd version | Shipped with Kubernetes | etcdctl snapshot restore and snapshot status | etcdutl |
|---|---|---|---|
| 3.4 and older | up to 1.21 | The only way | Does not exist |
| 3.5 | 1.22 to 1.33 | Still work, with a deprecation warning | Available, preferred |
| 3.6 and newer | 1.34 and later | Removed: unknown command | The only way |
etcdctl snapshot save never moved: taking a snapshot needs the server, so it stays with the network client.
Under the hood · What snapshot status tells you, and what it does not
etcdutl snapshot status <file> -w table prints four values: the file's hash, the revision (how many
changes the keyspace had seen when the snapshot was taken), the total keys and the total size. A file that
is truncated or is not a snapshot at all fails here, which is the point of running it.
It does not tell you when the snapshot was taken or which cluster it came from. Put the date and the cluster in
the file name, and compare TOTAL KEYS with what you expect: a kubeadm cluster with nothing deployed already has
several hundred keys, so a snapshot with a handful of keys came from the wrong etcd.
Find out where etcd runs
Parts:
- kube-apiserver (--etcd-servers), in Control plane node
- etcd static Pod (127.0.0.1:2379), in Control plane node
- etcd.yaml (flags and hostPath), in Control plane node
- etcd service (10.0.0.21:2379), in Separate etcd host
- etcd.service (flags in ExecStart), in Separate etcd host
Connections:
- kube-apiserver to etcd static Pod: loopback
- kube-apiserver to etcd service: another host
- etcd.yaml to etcd static Pod: starts
- etcd.service to etcd service: starts
kubeadm supports two layouts. In the stacked layout, each control plane node runs its own etcd member as a
static Pod, and the API server talks to the one on its own node. In the external layout, etcd runs on separate
hosts and the control plane nodes only hold a client certificate for it. An external etcd is usually a plain
systemd service, so there is no etcd Pod and no etcd.yaml.
| Stacked etcd | External etcd | |
|---|---|---|
--etcd-servers on the API server | https://127.0.0.1:2379 | One or more other hosts |
etcd Pod in kube-system | Yes, etcd-<node-name> | No |
| What starts etcd | The kubelet, from /etc/kubernetes/manifests/etcd.yaml | systemd, from etcd.service |
| Where the flags are | The command list in the manifest | ExecStart in the unit (systemctl cat etcd) |
| Certificates | /etc/kubernetes/pki/etcd/ on the node | Wherever the unit says, on the etcd host |
| Data directory | A hostPath volume, /var/lib/etcd | --data-dir in the unit |
| After a restore | Change the hostPath; the kubelet restarts the Pod | chown, edit the unit, systemctl restart etcd |
| Where you run the tools | On the control plane node | On the etcd host, over SSH |
The second half of the question is where the tools are. etcdutl works on local files, so it must run on the
machine that holds the data directory. etcdctl can run anywhere that reaches port 2379 and has a certificate, but
snapshot save writes the file to the disk of the machine it runs on.
Under the hood · How many etcd members are there?
etcdctl member list -w table is the direct answer, and etcdctl endpoint status --cluster -w table adds which
member is the leader. Without etcdctl, count the control plane nodes of a stacked cluster
(kubectl get nodes -l node-role.kubernetes.io/control-plane) or the addresses in --etcd-servers of an external
one. In a stacked cluster, each etcd's own --initial-cluster flag only lists the members that existed when that
node joined, so it is not a reliable count.
etcdctl snapshot save talks to exactly one endpoint. Give it several and it stops with
snapshot must be requested to one selected node, not multiple. Every member holds the same data, so any healthy
one will do.
What a restore needs around it
etcdutl snapshot restore only writes a directory. Making the cluster use it takes the same four moves on every
layout; only the handles differ.
- Stop the API server
- Restore into a new directory
- Point etcd at it and restart etcd
- Start the API server and the other components
- Stop the API servers. The Kubernetes documentation asks for this: an API server that keeps running holds
connections and caches from the old data. On a kubeadm node, move
kube-apiserver.yamlout of/etc/kubernetes/manifestsand the kubelet stops the Pod. - Restore into a directory that does not exist yet.
etcdutlrefuses a directory that already has data. - Point etcd at the new directory and restart it: the hostPath in
etcd.yaml, or--data-dirin the systemd unit. - Start the API servers again, and restart kube-scheduler, kube-controller-manager and the kubelet so that nothing keeps acting on state from before the restore.
Parts:
- Snapshot file (/opt/backup/etcd.db)
- New data dir (/var/lib/etcd-restore)
- etcd.service (--data-dir=...)
- chown etcd:etcd (the service's user)
- etcd service (systemctl restart)
- kube-apiserver (restart last)
Connections:
- Snapshot file to New data dir: restore
- chown etcd:etcd to New data dir: owns
- etcd.service to New data dir: points to
- etcd.service to etcd service: starts
- kube-apiserver to etcd service: reconnects
On a systemd service there are two things a static Pod never asks of you. The restored directory is created by the
user who ran etcdutl, normally root, with permissions that only its owner can use. If the service runs as a
dedicated etcd user, it cannot open the directory until you chown it. And systemd does not watch the unit
file: after editing it, systemctl daemon-reload comes before systemctl restart etcd.
The flags of snapshot restore
| Flag | What it sets | When you need it |
|---|---|---|
--data-dir | Where the new data directory is written | Always. Without it the data goes to ./<name>.etcd, in the current folder |
--wal-dir | A separate directory for the write-ahead log | Only if etcd itself runs with --wal-dir |
--name | The member's name | Several members: must match that member's --name |
--initial-cluster | The full member list, name=peer-url,... | Several members: the same value on every member |
--initial-cluster-token | A token that marks the new cluster | Several members: the same value on every member |
--initial-advertise-peer-urls | This member's peer URL (port 2380) | Several members: that member's own URL |
--skip-hash-check | Skips the integrity check | Only for a db file copied out of a data directory, never for a snapshot save file |
--bump-revision, --mark-compacted | Push the revision forward after the restore | When clients must never see revisions go backwards (see below) |
For a single etcd member, which is every single control plane kubeadm cluster, --data-dir is the only flag.
Parts:
- Snapshot file (the same on each host)
- etcd-1 (--name etcd-1)
- etcd-2 (--name etcd-2)
- etcd-3 (--name etcd-3)
- New etcd cluster (new ids, one token)
Connections:
- Snapshot file to etcd-1
- Snapshot file to etcd-2: restore
- Snapshot file to etcd-3
- etcd-1 to New etcd cluster
- etcd-2 to New etcd cluster: start
- etcd-3 to New etcd cluster
With several members, the snapshot file is copied to every etcd host and restored there once, with that member's
own --name and --initial-advertise-peer-urls, and the same --initial-cluster and --initial-cluster-token
everywhere. The values must match what each etcd is started with. All members then start on their new directories.
Restoring only one member and leaving the others on old data does not work: the restored member has a new cluster
id and the others will refuse to talk to it.
Under the hood · Why a restore can confuse controllers, and what --bump-revision does
Every write to etcd raises a counter, the revision, and Kubernetes exposes it as resourceVersion. Controllers
and the kubelet keep local caches and tell the API server "send me what changed since revision N". A restore takes
etcd back to an older revision, so those clients may hold an N from a future that no longer exists, and can miss
changes or act on objects that are gone.
Restarting the components clears their caches, which is why the documentation recommends it. For clusters where
that is not enough, etcdutl snapshot restore can add a large number to the revision with --bump-revision
and mark everything up to it as compacted with --mark-compacted (the two go together). Every old watch is then
answered with "too old, list again", which forces a clean reload:
etcdutl snapshot restore /opt/backup/etcd.db --data-dir /var/lib/etcd-restore \
--bump-revision 1000000000 --mark-compacted
The etcd documentation suggests one billion as a bump that covers a snapshot up to a week old. You are unlikely to need this on a practice cluster, but it explains the odd behaviour you may see after a restore without restarts.
Using it
Example · Work out the layout and the tools in a minute
Run these on the control plane node before you type any backup or restore command.
grep etcd-servers /etc/kubernetes/manifests/kube-apiserver.yaml # 127.0.0.1 = stacked, anything else = external
kubectl -n kube-system get pods -o wide | grep etcd # an etcd Pod confirms stacked
ls /etc/kubernetes/manifests/ # is there an etcd.yaml?
etcdctl version # which tools are here, and how old?
etcdutl version
grep image: /etc/kubernetes/manifests/etcd.yaml # the version of the etcd server itself
For an external etcd, the same questions are answered on the etcd host:
ssh etcd-1
systemctl cat etcd # the unit file: data directory, certificates, user
ps -ef | grep [e]tcd # the same flags, from the running process
Prefer an etcdutl of the same minor version as the etcd server that will start on the restored directory.
Example · Back up and restore an external etcd that runs under systemd
The task: save a snapshot of the etcd on host etcd-1 to /opt/backup/etcd.db, then restore it to
/var/lib/etcd-restore. On etcd-1, read the certificate paths from the unit file and take the snapshot:
systemctl cat etcd | grep -E 'listen-client-urls|trusted-ca-file|cert-file|key-file|data-dir|User'
etcdctl --endpoints=https://10.0.0.21:2379 \
--cacert=/etc/etcd/pki/ca.crt \
--cert=/etc/etcd/pki/server.crt \
--key=/etc/etcd/pki/server.key \
snapshot save /opt/backup/etcd.db
etcdutl snapshot status /opt/backup/etcd.db -w table
Stop the API server on the control plane node, then restore on etcd-1:
# on the control plane node
mv /etc/kubernetes/manifests/kube-apiserver.yaml /tmp/
# on etcd-1
etcdutl snapshot restore /opt/backup/etcd.db --data-dir /var/lib/etcd-restore
chown -R etcd:etcd /var/lib/etcd-restore # the user from the unit's User= line
vi /etc/systemd/system/etcd.service # --data-dir=/var/lib/etcd-restore
systemctl daemon-reload
systemctl restart etcd
systemctl status etcd --no-pager # active (running)
Bring the control plane back and check:
# on the control plane node
mv /tmp/kube-apiserver.yaml /etc/kubernetes/manifests/
systemctl restart kubelet
kubectl get nodes
kubectl get pods -A # the objects from the snapshot
If the unit has no User= line, etcd runs as root and the chown is not needed. If the service fails to start,
journalctl -u etcd --no-pager | tail -20 shows why; permission denied on the data directory is the usual
answer.
Example · Restore a stacked etcd by the book
The lesson on etcd commands changed the hostPath and let the kubelet restart etcd. This version adds the stop and restart steps the Kubernetes documentation recommends, which avoids stale state afterwards.
cd /etc/kubernetes/manifests
mv kube-apiserver.yaml kube-controller-manager.yaml kube-scheduler.yaml /tmp/ # stop the clients of etcd
etcdutl snapshot restore /opt/backup/etcd.db --data-dir /var/lib/etcd-restore
vi etcd.yaml # volumes: hostPath path /var/lib/etcd -> /var/lib/etcd-restore
watch crictl ps --name etcd # wait until a new etcd container is Running
mv /tmp/kube-*.yaml /etc/kubernetes/manifests/
systemctl restart kubelet
kubectl get pods -A
While the API server is stopped kubectl answers connection refused. That is expected: use crictl ps to watch
the containers until the manifests are back.
Example · Restore a three-member etcd cluster
Copy the same snapshot to all three hosts. On each one, restore with that member's name and peer URL. On etcd-1:
etcdutl snapshot restore /opt/backup/etcd.db \
--name etcd-1 \
--data-dir /var/lib/etcd-restore \
--initial-cluster etcd-1=https://10.0.0.21:2380,etcd-2=https://10.0.0.22:2380,etcd-3=https://10.0.0.23:2380 \
--initial-cluster-token restored-2026-11 \
--initial-advertise-peer-urls https://10.0.0.21:2380
On etcd-2 and etcd-3, only --name and --initial-advertise-peer-urls change. Then, on every host, fix the
owner, point etcd at the new directory, and start all three. Check from any of them:
etcdctl endpoint status --cluster -w table # three members, one leader, the same revision
Read the right values from each member's own flags (--name, --initial-advertise-peer-urls) rather than
inventing them: a name that does not match is the most common reason a restored member does not start.
Tricks
Common mistakes
- Using
etcdutlto save. There is noetcdutl snapshot save. A backup always comes from a running server, throughetcdctl. - Passing connection flags to
etcdutl.unknown flag: --cacertor--endpoints: remove them all. - Forgetting
--data-dir. The restore succeeds, into./default.etcdin the current folder, and etcd never sees it. - Forgetting the owner on a systemd service. etcd fails to start with
permission denied;chown -Rthe new directory to the service's user. - Editing the unit without
systemctl daemon-reload. systemd restarts etcd with the old flags, on the old data. - Running
etcdutlon the wrong machine. With an external etcd, a restore on the control plane node writes a directory that no etcd will ever read. Restore on the etcd host. - Looking for
etcd.yamlon a cluster with external etcd. There is none. Check--etcd-serversfirst. - Restoring one member of several. Each member needs its own restore from the same snapshot, with matching
--initial-clusterand token. - Using
etcdutl defrag --data-diron a live directory. The offline form is for a stopped etcd only; on a running member useetcdctl defrag.
Recap
etcdctlfor anything that needs a running etcd (snapshot save, health, members);etcdutlfor anything that only needs files (snapshot status,snapshot restore), with no connection flags.- From etcd 3.6, shipped with Kubernetes 1.34,
etcdctlcan no longer restore. --etcd-serverson the API server tells you whether etcd is stacked (static Pod,etcd.yaml) or external (systemd,etcd.service).- Static Pod: change the hostPath. Service:
chown, edit--data-dir,daemon-reload,systemctl restart etcd. - Stop the API server before a restore, restart the control plane after it, and restore every member when there are several.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
2 hands-on labs for this lesson. You get goals, not steps: any solution that reaches them passes.
A namespace was deleted and only an etcd snapshot remembers it. Save the present first, then restore the whole cluster from the snapshot and get the control plane running on the restored data.
Fix · Level 3 · challenge · target 25 min
Section capstone on a kubeadm cluster. Empty a node for repair without hurting a protected app, save an etcd snapshot, unpack an older snapshot next to the live data, and upgrade the control plane node to the next Kubernetes version.
Build · Level 3 · challenge · target 45 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.