etcd

On this page 7

etcd is an open-source, distributed and strongly consistent key-value store that serves as the single source of truth for a Kubernetes cluster. Every object the Kubernetes API accepts, from Pods and Deployments to ConfigMaps and Secrets, is persisted in etcd, and every controller learns about changes by watching keys through the API server.

The name combines the Unix configuration directory /etc with d for distributed. Originally developed at CoreOS in 2013, etcd is now a graduated project of the Cloud Native Computing Foundation and uses the Raft consensus algorithm to keep replicas identical even when individual members fail.

How the store keeps replicas consistent

The store guarantees linearizable reads and writes by running the Raft protocol among an odd number of members. One member is elected leader; it accepts every write, appends it to a replicated log and commits the entry only after a majority of members, the quorum, has persisted it to disk.

If the leader stops sending heartbeats, followers start an election after a randomized election timeout, by default 1,000 milliseconds, with a heartbeat interval of 100 milliseconds. Each committed entry increments a cluster-wide revision number, which gives every key a version history that clients can read or watch from a specific point.

The table shows how cluster size determines fault tolerance.

MembersQuorum sizeTolerated failuresTypical use
110Development and testing only
321Most production control planes
532Large or critical clusters
743Rare; write latency increases
Cluster size and failure tolerance

Even cluster sizes add no tolerance: four members still survive only one failure, yet need three acknowledgments per write. Odd member counts are therefore the rule.

The role of the store in a Kubernetes control plane

In Kubernetes, only the API server talks to the store directly. The scheduler, the controller manager, kubelets and kubectl all read and write cluster state through the API server, which validates requests, applies admission control and then serializes objects into keys under the /registry prefix.

The watch mechanism is what makes the reconciliation model work. Controllers open long-lived watches through the API server, receive every change with its revision, and react by moving actual state toward desired state. The API server keeps a watch cache so that thousands of clients do not each load the database.

Two topologies are common, and the choice affects resilience and hardware cost.

TopologyWhere members runAdvantageDrawback
StackedOn the same nodes as the API serverFewer machines, simpler setupLosing a node removes both roles
ExternalOn dedicated hostsIsolated failure domains and resourcesMore machines to operate
Managed serviceHidden by the providerNo operational burdenNo direct access or tuning
Control plane topologies

Managed offerings such as Amazon EKS and Google Kubernetes Engine run and back up the store for the customer, who never sees the members directly.

Key features and operational parameters

The version 3 API exposes keys in a flat binary key space over gRPC, with range queries, transactions, leases and watches. Data lives in a B+ tree database called bbolt, while the write-ahead log and periodic snapshots allow recovery after a crash.

ParameterDefault or guidanceWhat it controls
–quota-backend-bytes2 GB default, 8 GB suggested maximumDatabase size before alarms block writes
Request size limit1.5 MB per requestLargest single object that can be stored
–auto-compaction-retentionKubernetes compacts every 5 minutesHow much revision history is kept
–heartbeat-interval100 msLeader liveness signal
–election-timeout1,000 msDelay before a new leader is chosen
–snapshot-count100,000 entriesWhen the log is snapshotted
Parameters that most affect stability

Compaction and defragmentation work together: compaction discards old revisions logically, while defragmentation returns freed space to the file system. Without both, the database grows until it hits the quota and the cluster enters a read-only alarm state.

Backup, restore and security

Because the store holds the entire cluster definition, regular snapshots are the most important disaster recovery control for self-managed Kubernetes. A snapshot taken with etcdctl snapshot save captures a consistent copy that can rebuild a new cluster.

  • Encrypt traffic with mutual TLS between members and between the API server and the store.
  • Enable encryption at rest for Secrets in the API server, since values are otherwise only base64 encoded.
  • Restrict network access to the client port 2379 and peer port 2380.
  • Test restores regularly, because an untested snapshot is not a reliable backup.

Anyone with direct access to the store can read every Secret and rewrite any object, bypassing Kubernetes RBAC entirely, so access to the members equals full cluster control.

Performance limits and common mistakes

The store is sensitive to disk and network latency because every write waits for an fsync on a quorum of members. Guidance calls for SSD storage with 99th percentile WAL fsync latency under 10 milliseconds; slower disks cause leader elections and API timeouts.

SymptomLikely causeRemedy
Frequent leader changesSlow disks or overloaded hostsMove to dedicated SSDs, isolate members
Database space exceeded alarmMissing compaction or defragmentationCompact, defragment, then disarm the alarm
Slow kubectl responsesLarge lists of objects or eventsReduce object churn, paginate requests
Cluster unavailable after outageQuorum lostRestore from snapshot or recover members
Members spread across regionsHigh round-trip latencyKeep members within one region
Symptoms, causes and remedies

A frequent misconception is that the store can serve as a general-purpose database. It is designed for small, critical coordination data measured in gigabytes, not for application payloads or high-volume analytics.

A non-technical view

Think of a Kubernetes cluster as a large, busy workshop. Somewhere there must be one official notebook that records what should be running, where and how. That notebook is etcd.

To make sure the notebook is never lost or contradictory, several identical copies are kept on different machines. A change counts only when most copies agree, so the workshop keeps working even if one copy is destroyed.

A concrete example

A logistics company operates a self-managed Kubernetes cluster with about 300 worker nodes running its tracking services. The control plane uses three external etcd members on dedicated virtual machines in three availability zones of one region.

One morning, engineers see API requests slowing and logs reporting repeated leader elections. Metrics show WAL fsync latency of 40 milliseconds on one member, caused by a noisy neighbor sharing its storage volume. The team moves that member to a dedicated NVMe-backed volume, and fsync latency drops below 5 milliseconds.

Months later, an operator accidentally deletes a critical namespace. Because the team takes automated snapshots every 30 minutes and rehearses restores quarterly, they rebuild the control plane state on a staging cluster, extract the lost objects and reapply them in under an hour.