Defragging etcd took it from 1.45 GiB to 80 MiB
3 min read
kubernetes · etcd · homelab
← all postsEach of my three etcd pods used 1.65 GiB of memory to hold 45 MiB of live keys. The memory was not a leak. The database file had grown to 1.45 GiB, and etcd keeps an index of it in memory.
Four numbers that disagreed
- Memory per etcd pod: 1.65 GiB.
db_total_size_in_bytes: 1.45 GiB.db_total_size_in_use_in_bytes: 45 MiB, roughly 3% of the file.- A panel header on the same dashboard: 560 MiB used at 73%, read from an older quota assumption that no longer applied.
The first two agreed with each other. The last one came from an assumption about the quota that no longer held, which is why the dashboard looked healthy while the file grew.
Why a file grows while the data does not
Every write appends to the Bolt file, and compaction marks old revisions for removal without returning their pages to the filesystem. Free pages stay in the file, the index still covers them, and the RSS follows the file. A control plane with steady churn accumulates gigabytes of pages that hold nothing.
Quota alarms are the part that bites. etcd_server_quota_backend_bytes counts the file, not the live data, so a 45 MiB dataset can fill a 2 GiB backend and put the cluster into alarm at 3% real usage.
etcdctl defrag blocks the member
etcdctl endpoint defrag rewrites the file and blocks that member's writes while it runs. On a 3-member control plane that is survivable one member at a time, but it is a hand-run operation against a live quorum, and I did not want my hands on it.
The CronJob
ahrtr/etcd-defrag v0.26.0 wraps the same call and decides per member whether a rewrite is worth it. The job runs on a control-plane node, uses the host network to reach etcd on loopback, and fires weekdays at 09:14:
args:
- --endpoints=https://127.0.0.1:2379
- --cacert=/ca.pem
- --cert=/member-node-1.pem
- --key=/member-node-1-key.pem
- --cluster
- --defrag-rule
- "dbQuotaUsage > 0.8 || dbSize - dbSizeInUse > 200*1024*1024"
The rule is the whole decision: defrag a member above 80% of its quota, or one sitting on more than 200 MiB of reclaimable pages. Below that the job starts, checks, and exits.
The result
Memory went from 1.65 GiB to 250 MiB per pod. db_total_size_in_bytes went from 1.45 GiB to 80 MiB, with the live data still around 40 MiB. The dashboard now reads 4% of 2 GiB.
What it costs
A run rewrites the database file on the disk that also holds the WAL: IO on the control plane, where I do not want it. The rule is what keeps that off a healthy member: with the 200 MiB threshold, most weekday runs do nothing.
The threshold also decides how much of the quota I spend on free pages. Two hundred reclaimable megabytes inside a 2 GiB backend is a tenth of the budget; at 8 GiB of quota I would want the threshold to scale with it instead of staying at 200 MiB.
What I would do differently
The job fixes the symptom, and I set it up before I had a panel that shows the symptom. The metric worth watching is db_total_size - db_total_size_in_use, alerted at the same 200 MiB, so the alert and the rule cannot drift apart.
What I still cannot answer is what wrote enough churn to produce 1.4 GiB of free pages. Compaction handles the revisions; something in that cluster produced pages that stayed free, and no panel I have tells me which object it was.