Skip to content
LF Cert Trainer

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

1/11etcdctl and etcdutl
0:00 / 4:03

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

What each tool touchesAPI callownsopensonly when etcd is stoppedYoushell on the etcd hostetcdctlonline: needs TLSetcdutloffline: no flagsRunning etcdport 2379Data directory/var/lib/etcdSnapshot file/opt/backup/etcd.db
What each tool touches. etcdctl reaches the data through the running server. etcdutl never talks to a server: it opens files on the disk of the machine it runs on.

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:

JobOnline, with etcdctlOffline, with etcdutl
Make a backupetcdctl snapshot save <file>None: only the server can produce a consistent copy
Inspect a backupNone since 3.6etcdutl snapshot status <file> -w table
Restore a backupNone since 3.6etcdutl snapshot restore <file> --data-dir <dir>
Reclaim disk spaceetcdctl defrag (the member pauses while it runs)etcdutl defrag --data-dir <dir> (etcd must be stopped)
Size and revisionetcdctl endpoint status -w tableetcdutl snapshot status <file> -w table
Tool versionetcdctl versionetcdutl version
Health, members, keysetcdctl endpoint health, member list, getNone: these are questions for a live server

What moved, by etcd version:

etcd versionShipped with Kubernetesetcdctl snapshot restore and snapshot statusetcdutl
3.4 and olderup to 1.21The only wayDoes not exist
3.51.22 to 1.33Still work, with a deprecation warningAvailable, preferred
3.6 and newer1.34 and laterRemoved: unknown commandThe 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

Stacked or external: where is etcd?Control plane nodeSeparate etcd hostloopbackanother hoststartsstartskube-apiserver--etcd-serversetcd static Pod127.0.0.1:2379etcd.yamlflags and hostPathetcd service10.0.0.21:2379etcd.serviceflags in ExecStart
Stacked or external: where is etcd?. The API server's --etcd-servers flag tells you. A loopback address means etcd is a static Pod on the same node; another address means etcd runs elsewhere, usually as a systemd service.

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 etcdExternal etcd
--etcd-servers on the API serverhttps://127.0.0.1:2379One or more other hosts
etcd Pod in kube-systemYes, etcd-<node-name>No
What starts etcdThe kubelet, from /etc/kubernetes/manifests/etcd.yamlsystemd, from etcd.service
Where the flags areThe command list in the manifestExecStart in the unit (systemctl cat etcd)
Certificates/etc/kubernetes/pki/etcd/ on the nodeWherever the unit says, on the etcd host
Data directoryA hostPath volume, /var/lib/etcd--data-dir in the unit
After a restoreChange the hostPath; the kubelet restarts the Podchown, edit the unit, systemctl restart etcd
Where you run the toolsOn the control plane nodeOn 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.

  1. Stop the API server
  2. Restore into a new directory
  3. Point etcd at it and restart etcd
  4. Start the API server and the other components
A restore, on any layout
  1. 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.yaml out of /etc/kubernetes/manifests and the kubelet stops the Pod.
  2. Restore into a directory that does not exist yet. etcdutl refuses a directory that already has data.
  3. Point etcd at the new directory and restart it: the hostPath in etcd.yaml, or --data-dir in the systemd unit.
  4. 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.
Restoring an etcd that runs as a systemd servicerestoreownspoints tostartsreconnectsSnapshot file/opt/backup/etcd.dbNew data dir/var/lib/etcd-restoreetcd.service--data-dir=...chown etcd:etcdthe service's useretcd servicesystemctl restartkube-apiserverrestart last
Restoring an etcd that runs as a systemd service. The same idea as on a static Pod, with different handles: the directory needs the service's owner, the unit file holds the path, and systemctl does the restart.

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

FlagWhat it setsWhen you need it
--data-dirWhere the new data directory is writtenAlways. Without it the data goes to ./<name>.etcd, in the current folder
--wal-dirA separate directory for the write-ahead logOnly if etcd itself runs with --wal-dir
--nameThe member's nameSeveral members: must match that member's --name
--initial-clusterThe full member list, name=peer-url,...Several members: the same value on every member
--initial-cluster-tokenA token that marks the new clusterSeveral members: the same value on every member
--initial-advertise-peer-urlsThis member's peer URL (port 2380)Several members: that member's own URL
--skip-hash-checkSkips the integrity checkOnly for a db file copied out of a data directory, never for a snapshot save file
--bump-revision, --mark-compactedPush the revision forward after the restoreWhen 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.

One snapshot, one new directory per memberrestorestartSnapshot filethe same on each hostetcd-1--name etcd-1etcd-2--name etcd-2etcd-3--name etcd-3New etcd clusternew ids, one token
One snapshot, one new directory per member. Every member is restored from the same file with its own name and peer URL, and all of them share one member list and one token. Together they form a new cluster.

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 etcdutl to save. There is no etcdutl snapshot save. A backup always comes from a running server, through etcdctl.
  • Passing connection flags to etcdutl. unknown flag: --cacert or --endpoints: remove them all.
  • Forgetting --data-dir. The restore succeeds, into ./default.etcd in the current folder, and etcd never sees it.
  • Forgetting the owner on a systemd service. etcd fails to start with permission denied; chown -R the 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 etcdutl on 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.yaml on a cluster with external etcd. There is none. Check --etcd-servers first.
  • Restoring one member of several. Each member needs its own restore from the same snapshot, with matching --initial-cluster and token.
  • Using etcdutl defrag --data-dir on a live directory. The offline form is for a stopped etcd only; on a running member use etcdctl defrag.

Recap

  • etcdctl for anything that needs a running etcd (snapshot save, health, members); etcdutl for anything that only needs files (snapshot status, snapshot restore), with no connection flags.
  • From etcd 3.6, shipped with Kubernetes 1.34, etcdctl can no longer restore.
  • --etcd-servers on 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.