Day 11: Maintaining etcd
Goal: Read the state of etcd, understand why the database grows and does not shrink by itself, defragment members without an outage, and create snapshots automatically · Duration: approx. 75 min · Prerequisite: Day 10
Briefing
- etcd stores every change as a new revision. Old revisions remain until they are compacted. The API server does that by itself every five minutes.
- Compaction frees the space inside the file but does not make the file smaller. Only a defragmentation fills the gaps. It blocks the affected member for a short time.
- The file has an upper limit, the quota (default 2
GiB). When it is reached, etcd raises the
NOSPACEalarm and stops accepting writes. The cluster then stands still until someone cleans up. - And: a snapshot only before upgrades (Day 5 and 9) is not enough. It belongs on a schedule.
| Metric | Meaning |
|---|---|
DB SIZE |
Size of the file on disk |
IN USE |
The part of it actually in use |
REVISION |
Counter of all changes since the cluster was created |
| Quota | Upper limit for DB SIZE, then the NOSPACE
alarm |
Drill 1: The state
From today on, environment variables save you typing.
sudo -E passes them on to etcdctl:
mkdir -p ~/day2/tag11 && cd ~/day2/tag11
export ETCDCTL_ENDPOINTS=https://172.16.0.126:2379,https://172.16.0.228:2379,https://172.16.0.83:2379
export ETCDCTL_CACERT=/etc/kubernetes/pki/etcd/ca.crt
export ETCDCTL_CERT=/etc/kubernetes/pki/etcd/server.crt
export ETCDCTL_KEY=/etc/kubernetes/pki/etcd/server.key
sudo -E etcdctl endpoint health -w table
sudo -E etcdctl endpoint status -w json | jq -r '.[] | [.Endpoint, .Status.header.revision,
.Status.dbSize, .Status.dbSizeInUse, (.Status.leader == .Status.header.member_id)] | @tsv'Expected:
+---------------------------+--------+-------------+-------+
| ENDPOINT | HEALTH | TOOK | ERROR |
+---------------------------+--------+-------------+-------+
| https://172.16.0.126:2379 | true | 30.692486ms | |
| https://172.16.0.228:2379 | true | 35.783719ms | |
| https://172.16.0.83:2379 | true | 45.843641ms | |
+---------------------------+--------+-------------+-------+
https://172.16.0.126:2379 200582 18907136 4501504 false
https://172.16.0.228:2379 200582 18952192 4493312 false
https://172.16.0.83:2379 200582 18956288 4485120 true
Revision 200,582 after one day of the course: every status, every
event, every lease renewal counts. The file is 18.9 MB in size, 4.5 MB
of it in use. A good three quarters are gaps, created by the upgrades
and the restore of the last few days. The leader is currently
cp3.
Save your progress? Sign up for free
Drill 2: Who compacts
Revision 1 is long gone:
sudo -E etcdctl get /registry/namespaces/default --rev=1 2>&1 | tail -1Expected:
Error: etcdserver: mvcc: required revision has been compacted
That was the API server, not etcd. It compacts every five minutes
(flag --etcd-compaction-interval, not set by kubeadm, so
the default applies). kubeadm does not set the quota either; the default
of 2 GiB applies. You can see both in the metrics that etcd offers
locally on port 2381:
curl -s http://127.0.0.1:2381/metrics | grep -E \
'^etcd_server_quota_backend_bytes|^etcd_mvcc_db_total_size_in(_use_in)?_bytes'Expected:
etcd_mvcc_db_total_size_in_bytes 1.8907136e+07
etcd_mvcc_db_total_size_in_use_in_bytes 4.54656e+06
etcd_server_quota_backend_bytes 2.147483648e+09
These three numbers belong in monitoring. What matters is the ratio of file size to quota, not the part in use.
Save your progress? Sign up for free
Drill 3: Let the database grow
600 ConfigMaps of 53 KB each, like a program that stores too much:
kubectl create namespace tag11
head -c 40000 /dev/urandom | base64 -w0 > blob.txt
date +%T
for i in $(seq 1 600); do
kubectl -n tag11 create configmap last-$i --from-file=daten=blob.txt >/dev/null
done
date +%T
sudo -E etcdctl endpoint status -w table | cut -d'|' -f2,6,7,8,10 | grep -v '^+'Expected:
16:01:12
16:03:49
ENDPOINT | DB SIZE | IN USE | PERCENTAGE NOT IN USE | IS LEADER
https://172.16.0.126:2379 | 43 MB | 38 MB | 13% | false
https://172.16.0.228:2379 | 43 MB | 38 MB | 13% | false
https://172.16.0.83:2379 | 43 MB | 38 MB | 12% | true
And get rid of it again:
kubectl delete namespace tag11
sudo -E etcdctl endpoint status -w table | cut -d'|' -f2,6,7,8 | grep -v '^+'Expected:
namespace "tag11" deleted
ENDPOINT | DB SIZE | IN USE | PERCENTAGE NOT IN USE
https://172.16.0.126:2379 | 43 MB | 38 MB | 13%
https://172.16.0.228:2379 | 43 MB | 38 MB | 13%
https://172.16.0.83:2379 | 43 MB | 38 MB | 12%
Deleted, but still in use: a deletion is just another new revision. The old values remain until compaction.
Save your progress? Sign up for free
Drill 4: Wait two cycles
After a good five minutes:
sleep 330
sudo -E etcdctl endpoint status -w table | cut -d'|' -f2,6,7,8 | grep -v '^+'Expected:
ENDPOINT | DB SIZE | IN USE | PERCENTAGE NOT IN USE
https://172.16.0.126:2379 | 52 MB | 38 MB | 28%
https://172.16.0.228:2379 | 52 MB | 38 MB | 27%
https://172.16.0.83:2379 | 51 MB | 38 MB | 27%
The file has even grown, and 38 MB are still in use. The API server always compacts up to the revision it noted in the previous cycle. What was deleted is gone in the cycle after next at the earliest. Another four minutes:
sleep 240
sudo -E etcdctl endpoint status -w table | cut -d'|' -f2,6,7,8 | grep -v '^+'Expected:
ENDPOINT | DB SIZE | IN USE | PERCENTAGE NOT IN USE
https://172.16.0.126:2379 | 52 MB | 4.3 MB | 92%
https://172.16.0.228:2379 | 52 MB | 4.3 MB | 92%
https://172.16.0.83:2379 | 51 MB | 4.3 MB | 92%
4.3 MB are in use again, but the file stays at 52 MB. 92 % empty space that counts against the quota. This is exactly where defragmentation comes in.
Save your progress? Sign up for free
Drill 5: Defragment under load
The measurements run as on Day 6: last against
shop, plus the API probe on cp2.
kubectl -n demo delete pod last
sed "s#http://web.demo#http://shop.demo#" ~/day2/tag00/last.yaml | kubectl apply -f -
kubectl -n demo wait --for=condition=Ready pod/last# on cp2
mkdir -p ~/day2/tag11
nohup sh -c 'while true; do echo "$(date +%T) $(curl -sk -m 2 -o /dev/null \
-w %{http_code} https://172.16.0.100:6443/readyz)"; sleep 1; done' \
> ~/day2/tag11/api.log 2>&1 &Warning: A defragmentation blocks the member until it is finished. Only one member at a time, the followers first, the leader last.
You specify a single member with --endpoints. With the
environment variable set, there is a surprise:
sudo -E etcdctl --endpoints=https://172.16.0.126:2379 defrag 2>&1 | cut -c1-120Expected:
{"level":"fatal","ts":"2026-11-22T16:15:50.486772Z","caller":"flags/flag.go:86","msg":"conflicting environment variable
etcdctl refuses when an environment variable and a flag set the same thing. So override the variable for this one call:
for ep in 172.16.0.126 172.16.0.228; do
sudo -E ETCDCTL_ENDPOINTS=https://$ep:2379 etcdctl defrag
done
sudo -E etcdctl endpoint status -w table | cut -d'|' -f2,3,6,7,10 | grep -v '^+'Expected:
Finished defragmenting etcd member[https://172.16.0.126:2379]. took 237.481964ms
Finished defragmenting etcd member[https://172.16.0.228:2379]. took 372.928816ms
ENDPOINT | ID | DB SIZE | IN USE | IS LEADER
https://172.16.0.126:2379 | 33764b6962314e3f | 4.2 MB | 4.2 MB | false
https://172.16.0.228:2379 | 6292e5989e4c860b | 4.2 MB | 4.2 MB | false
https://172.16.0.83:2379 | cf664b54c8c1c3e0 | 51 MB | 4.5 MB | true
From 52 to 4.2 MB in a quarter of a second. With databases of several gigabytes it takes longer.
Save your progress? Sign up for free
Drill 6: The leader last
Instead of blocking the leader, you hand over the leadership first. The ID of cp1 is in the table above:
sudo -E ETCDCTL_ENDPOINTS=https://172.16.0.83:2379 etcdctl move-leader 33764b6962314e3f
sudo -E ETCDCTL_ENDPOINTS=https://172.16.0.83:2379 etcdctl defrag
sudo -E etcdctl endpoint status -w table | cut -d'|' -f2,6,7,10 | grep -v '^+'Expected:
Leadership transferred from cf664b54c8c1c3e0 to 33764b6962314e3f
Finished defragmenting etcd member[https://172.16.0.83:2379]. took 330.360473ms
ENDPOINT | DB SIZE | IN USE | IS LEADER
https://172.16.0.126:2379 | 4.3 MB | 4.3 MB | true
https://172.16.0.228:2379 | 4.3 MB | 4.3 MB | false
https://172.16.0.83:2379 | 4.3 MB | 4.3 MB | false
The results:
kubectl -n demo logs last | wc -l
kubectl -n demo logs last | awk '$2 != 200' | wc -l# on cp2
pkill -f readyz
wc -l < ~/day2/tag11/api.log
awk '$2 != 200' ~/day2/tag11/api.log | wc -lExpected:
122
0
63
0
0 errors for workload and API, three members defragmented, file down from 52 to 4.3 MB.
Save your progress? Sign up for free
Drill 7: Snapshots on a schedule
A systemd timer creates a snapshot every six hours and deletes snapshots older than seven days:
sudo tee /etc/systemd/system/etcd-snapshot.service >/dev/null <<"EOF"
[Unit]
Description=etcd-Snapshot nach /var/backups/k8s/etcd
[Service]
Type=oneshot
Environment=ETCDCTL_ENDPOINTS=https://127.0.0.1:2379
Environment=ETCDCTL_CACERT=/etc/kubernetes/pki/etcd/ca.crt
Environment=ETCDCTL_CERT=/etc/kubernetes/pki/etcd/server.crt
Environment=ETCDCTL_KEY=/etc/kubernetes/pki/etcd/server.key
ExecStartPre=/usr/bin/mkdir -p /var/backups/k8s/etcd
ExecStart=/bin/sh -c "/usr/local/bin/etcdctl snapshot save /var/backups/k8s/etcd/etcd-$(date +%%Y%%m%%d-%%H%%M).db"
ExecStartPost=/usr/bin/find /var/backups/k8s/etcd -name "etcd-*.db" -mtime +7 -delete
EOF
sudo tee /etc/systemd/system/etcd-snapshot.timer >/dev/null <<"EOF"
[Unit]
Description=etcd-Snapshot alle 6 Stunden
[Timer]
OnCalendar=*-*-* 00/6:00:00
RandomizedDelaySec=10m
Persistent=true
[Install]
WantedBy=timers.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now etcd-snapshot.timer
sudo systemctl start etcd-snapshot.service
sudo ls -l /var/backups/k8s/etcd/Expected:
Created symlink /etc/systemd/system/timers.target.wants/etcd-snapshot.timer → /etc/systemd/system/etcd-snapshot.timer.
total 4568
-rw------- 1 root root 4673568 Nov 22 16:26 etcd-20261122-1626.db
%% instead of %: systemd would read a
single % as a specifier of its own.
Persistent=true catches up on a missed run if the node was
off at the scheduled time. Is the snapshot usable?
sudo etcdutl snapshot status /var/backups/k8s/etcd/$(sudo ls /var/backups/k8s/etcd/ | tail -1) -w table
systemctl list-timers etcd-snapshot.timer --no-pager | head -2 | cut -c1-90Expected:
+----------+----------+------------+------------+---------+
| HASH | REVISION | TOTAL KEYS | TOTAL SIZE | VERSION |
+----------+----------+------------+------------+---------+
| a9f5b5f4 | 206744 | 460 | 4.7 MB | 3.7.0 |
+----------+----------+------------+------------+---------+
NEXT LEFT LAST PASSED UNIT ACTIVATES
Sun 2026-11-22 18:05:43 UTC 1h 39min - - etcd-snapshot.timer etcd-snapshot.service
4.7 MB instead of 52: a snapshot contains only the data in use. That is another way to see how much empty space the file holds.
Save your progress? Sign up for free
Exercise: Speed Drills
- Show the file size, the space in use and the leader of all members. Target time: 1 min
- Show whether an alarm is active. Target time: 30 s
- Show when the next snapshot runs. Target time: 30 s
Solution
sudo -E etcdctl endpoint status -w table | cut -d'|' -f2,6,7,10 | grep -v '^+'
sudo -E etcdctl alarm list
systemctl list-timers etcd-snapshot.timer --no-pageralarm list prints nothing if no alarm is active.
Save your progress? Sign up for free
Checkpoint
sudo -E etcdctl endpoint status -w table | cut -d'|' -f6,7 | grep -v '^+'Expected: For all three members,
DB SIZE and IN USE are close to each
other.
systemctl is-active etcd-snapshot.timerExpected: active
Challenge
A snapshot only on cp1 is worthless if cp1 fails. Set up the same timer on cp2 and cp3, and check that snapshots are created there.
Hint: After the first run, check the status of the service, not just whether the timer is active.
Solution
Copy the two files to cp2 and cp3 (content as in Drill 7), then:
# on cp2, then on cp3
sudo systemctl daemon-reload
sudo systemctl enable --now etcd-snapshot.timer
sudo systemctl start etcd-snapshot.serviceExpected:
Created symlink /etc/systemd/system/timers.target.wants/etcd-snapshot.timer → /etc/systemd/system/etcd-snapshot.timer.
Job for etcd-snapshot.service failed because the control process exited with error code.
See "systemctl status etcd-snapshot.service" and "journalctl -xeu etcd-snapshot.service" for details.
# on cp2
sudo journalctl -u etcd-snapshot.service --no-pager -n 4 | cut -c1-90Expected:
Nov 22 16:27:01 cp2 systemd[1]: Starting etcd-snapshot.service - etcd-Snapshot nach /var/bac
Nov 22 16:27:01 cp2 sh[43591]: /bin/sh: 1: /usr/local/bin/etcdctl: not found
Nov 22 16:27:01 cp2 systemd[1]: etcd-snapshot.service: Main process exited, code=exited, sta
Nov 22 16:27:01 cp2 systemd[1]: etcd-snapshot.service: Failed with result 'exit-code'.
On Day 9, only etcdutl for the restore was installed on
cp2 and cp3. The timer itself is still active and would
fail silently every six hours. So install etcdctl as well:
# on cp2, then on cp3
ARCH=$(dpkg --print-architecture)
curl -sL https://github.com/etcd-io/etcd/releases/download/v3.6.6/etcd-v3.6.6-linux-$ARCH.tar.gz \
| tar xz -C /tmp
sudo install /tmp/etcd-v3.6.6-linux-$ARCH/etcdctl /usr/local/bin/
sudo systemctl start etcd-snapshot.service
sudo ls /var/backups/k8s/etcd/Expected:
etcd-20261122-1627.db
Save your progress? Sign up for free
Cleanup
rm -f ~/day2/tag11/blob.txtThe timers stay. From now on, they are part of operations.
Interview Note
- What is the difference between compaction and defragmentation? Compaction deletes old revisions and frees space inside the file. Defragmentation rewrites the file and makes it smaller. The API server does the first; the second is up to you.
- What happens when etcd reaches the quota? etcd
raises the
NOSPACEalarm and only accepts reads and deletes. The fix: compact, defragment, clear the alarm withetcdctl alarm disarm, find the cause. - Why defragment the leader last? Because a
defragmentation briefly blocks the member. If it is the leader, the
whole cluster stalls. With
move-leaderyou hand over the leadership beforehand.
Enterprise Note
- Many teams defragment on a schedule, for example weekly and member by member, or use an operator for it. An alert on “file size above 50 % of the quota” buys time.
- Snapshots belong off the node: on a backup server or in S3 storage, encrypted, with regular restore tests. The planned course “Backup and disaster recovery” covers the full strategy.
- When etcd fills up, the cause is almost always a program that writes
too many or too large objects: events without expiry, huge ConfigMaps,
an operator in a loop. The
apiserver_storage_objectsmetric shows which resource type is growing.