Lesson 8 · Section 2: Core Concepts
Lesson 8: etcd in Kubernetes: where the cluster keeps its state
What Kubernetes stores in etcd and why only the API server talks to it, how kubeadm runs it as a static Pod, stacked and external topologies, and how to inspect it quickly in the exam. · 18 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 Kubernetes needs etcd
The control plane is a set of programs that can crash, restart or move to another machine at any moment: the API server, the scheduler, the controller manager. None of them keeps anything important on its own disk. That only works because all of them agree on one place where the truth is written down.
That place is etcd, a distributed key-value store. Every object you have ever seen in kubectl get output is a
key in etcd: Pods, Deployments, Secrets, nodes, RBAC rules, and the leases that say which controller manager is the
leader. etcd holds both the desired state (the spec you applied) and the observed state (the status
that components report back).
This is why an administrator treats etcd differently from every other component. A broken scheduler is an outage you fix by restarting it. A lost etcd with no backup is a lost cluster: the containers keep running on the nodes for a while, but Kubernetes no longer knows that they exist.
What Kubernetes keeps in etcd
Parts:
- kubectl (you)
- kube-scheduler
- Controller manager
- kube-apiserver (the only etcd client)
- kubelet (on every node)
- etcd (all cluster state)
Connections:
- kubectl to kube-apiserver
- kube-scheduler to kube-apiserver
- Controller manager to kube-apiserver
- kubelet to kube-apiserver
- kube-apiserver and etcd: TLS :2379
The scheduler, the controller manager, the kubelet and kubectl all get their view of the cluster from the API
server. Not one of them has the address of etcd or a certificate to open a connection to it. The API server is the
single client, and it talks to etcd over mutual TLS on port 2379.
The API server stores each object as one key under the /registry prefix. The layout is predictable:
| Object | Key in etcd |
|---|---|
Pod web in namespace shop | /registry/pods/shop/web |
Deployment web in shop | /registry/deployments/shop/web |
Secret db-password in shop | /registry/secrets/shop/db-password |
Namespace shop | /registry/namespaces/shop |
ClusterRole view (no namespace) | /registry/clusterroles/view |
Node node01 | /registry/minions/node01 |
| A custom resource | /registry/GROUP/PLURAL/NAMESPACE/NAME |
Nodes live under minions because that is what Kubernetes called them in its first releases, and renaming the keys
would have broken every existing cluster.
Just as useful is knowing what is not in etcd:
- Container logs and metrics. Logs are files on each node, and the metrics server keeps its numbers in memory.
- The data in your volumes. etcd stores the PersistentVolume and the claim, which are descriptions. The bytes live on the storage system.
- Container images. They sit in registries and in each node's image cache.
- Files on the nodes. The certificates in
/etc/kubernetes/pki, the kubeconfig files and the static Pod manifests are plain files. An etcd backup does not contain them.
How the API server uses etcd
Parts:
- kubectl (create deployment)
- kube-apiserver (check, then store)
- etcd (revision 1042)
- One key (deployments/shop/web)
- Controllers (watching the API)
Connections:
- kubectl to kube-apiserver: 1. POST
- kube-apiserver to etcd: 2. write
- etcd to One key: keeps
- kube-apiserver to Controllers: 3. event
Follow one kubectl create deployment web --image=nginx -n shop:
kubectlsends a POST request to the API server.- The API server authenticates the caller, checks RBAC, runs admission and validates the object.
- It writes the object to etcd at
/registry/deployments/shop/web. etcd replicates the write to a majority of its members and assigns it a new revision, a counter that increases with every change to the store. - Only now does the API server answer
deployment.apps/web created. If etcd cannot commit the write, you get an error and nothing was created. - The Deployment controller, which has a watch open on the API server, receives an event and creates a ReplicaSet. That is another write to etcd, another event, and so on down to the kubelet starting containers.
Three things you already use every day come straight from this design:
resourceVersionin an object's metadata is the etcd revision of its last change.kubectl get pods --watchworks because etcd can stream every change after a given revision, and the API server passes that stream on. Controllers never poll; they watch.- Conflict errors. When two clients update the same object, the API server tells etcd "write this only if the key is still at the revision I read". The second writer loses and sees the object has been modified; please apply your changes to the latest version and try again. No locks are needed.
Under the hood · Details that explain limits you will meet
- Values are binary. Built-in objects are stored as protobuf, not as the YAML or JSON you see in
kubectl. If you read a key withetcdctlthe value looks like noise with a few readable strings in it. Custom resources are stored as JSON. - Secrets are not encrypted by default. A Secret's data is only base64 in the API, and in etcd it is stored as it is unless the API server was started with an encryption configuration. Anyone who can read etcd, or a snapshot file, can read every Secret. Treat backups accordingly.
- Objects have a size limit. etcd rejects requests larger than about 1.5 MiB, which is why a ConfigMap or Secret cannot hold more than 1 MiB of data.
- The database has a size limit too. etcd stops accepting writes when its database reaches its quota (2 GiB
unless configured otherwise) and raises a
NOSPACEalarm. The cluster then looks read-only. - History is trimmed. etcd keeps old revisions of every key so that watches can catch up. The API server asks etcd to compact that history every five minutes. A watcher that falls too far behind gets a "resource version too old" error and has to list everything again.
- Events expire. The API server attaches a one hour lease to each Event, and etcd deletes it when the lease
runs out. That is why
kubectl get eventsonly shows the recent past.
How etcd is deployed
There are two ways you will meet etcd in a cluster, and the first question in any etcd task is which one you are looking at.
| kubeadm cluster | Installed by hand | |
|---|---|---|
| Runs as | A static Pod named etcd-NODENAME in kube-system | A systemd service on the host |
| Configured in | /etc/kubernetes/manifests/etcd.yaml | The unit file, usually /etc/systemd/system/etcd.service |
| Started by | The kubelet | systemd |
| See it with | kubectl -n kube-system get pods, crictl ps | systemctl status etcd |
| Logs | kubectl logs, crictl logs | journalctl -u etcd |
The exam clusters are built with kubeadm, so the static Pod is the one to know well.
Parts:
- Static Pod manifest (manifests/etcd.yaml)
- kubelet (watches the folder)
- etcd-controlplane (static Pod, :2379)
- kube-apiserver (--etcd-servers)
- pki/etcd (CA, server, peer)
- /var/lib/etcd (hostPath: the data)
- Client certificate (apiserver-etcd-client)
Connections:
- Static Pod manifest to kubelet: reads
- kubelet to etcd-controlplane: starts
- kube-apiserver to etcd-controlplane: :2379
- etcd-controlplane to /var/lib/etcd: writes
- pki/etcd to etcd-controlplane: mounted
- Client certificate to kube-apiserver: presents
A static Pod is started by the kubelet directly from a manifest file in /etc/kubernetes/manifests. This solves
a chicken and egg problem: the API server cannot start without etcd, so etcd cannot be something the API server
schedules. The Pod you see in kubectl -n kube-system get pods is only a mirror that the kubelet publishes. You
cannot change etcd by editing that Pod. You edit the file, and the kubelet restarts etcd when the file changes.
The flags in the manifest answer almost every question you can be asked:
spec:
containers:
- name: etcd
image: registry.k8s.io/etcd:3.6.5-0
command:
- etcd
- --name=controlplane
- --advertise-client-urls=https://192.168.56.10:2379
- --listen-client-urls=https://127.0.0.1:2379,https://192.168.56.10:2379
- --listen-peer-urls=https://192.168.56.10:2380
- --initial-cluster=controlplane=https://192.168.56.10:2380
- --data-dir=/var/lib/etcd
- --cert-file=/etc/kubernetes/pki/etcd/server.crt
- --key-file=/etc/kubernetes/pki/etcd/server.key
- --trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt
- --client-cert-auth=true
- --peer-cert-file=/etc/kubernetes/pki/etcd/peer.crt
- --peer-key-file=/etc/kubernetes/pki/etcd/peer.key
- --peer-trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt
- --listen-metrics-urls=http://127.0.0.1:2381
volumeMounts:
- { name: etcd-data, mountPath: /var/lib/etcd }
- { name: etcd-certs, mountPath: /etc/kubernetes/pki/etcd }
hostNetwork: true
volumes:
- name: etcd-data
hostPath: { path: /var/lib/etcd, type: DirectoryOrCreate }
- name: etcd-certs
hostPath: { path: /etc/kubernetes/pki/etcd, type: DirectoryOrCreate }
| Flag | What it tells you |
|---|---|
--listen-client-urls | Where clients connect: the API server and etcdctl. Port 2379 |
--listen-peer-urls | Where other etcd members connect to replicate. Port 2380 |
--data-dir | The folder that holds the database, and the thing a backup protects |
--cert-file, --key-file | The certificate etcd shows to clients |
--trusted-ca-file | The CA that client certificates must be signed by |
--client-cert-auth=true | Clients must present a certificate: there are no anonymous connections |
--initial-cluster | The members this one expects to find: one entry on a single node cluster |
--listen-metrics-urls | A plain HTTP health and metrics port on localhost, 2381, used by the Pod's probes |
The other half of the connection is in the API server's own manifest,
/etc/kubernetes/manifests/kube-apiserver.yaml:
- --etcd-servers=https://127.0.0.1:2379
- --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt
- --etcd-certfile=/etc/kubernetes/pki/apiserver-etcd-client.crt
- --etcd-keyfile=/etc/kubernetes/pki/apiserver-etcd-client.key
Topologies: stacked and external
A single etcd member is fine for practice and is what most exam clusters have. In production you run several members so that the cluster survives a machine failure. kubeadm supports two layouts.
Parts:
- kube-apiserver 1, in Control plane 1
- kube-apiserver 2, in Control plane 2
- kube-apiserver 3, in Control plane 3
- etcd member 1 (127.0.0.1:2379), in Control plane 1
- etcd member 2 (127.0.0.1:2379), in Control plane 2
- etcd member 3 (127.0.0.1:2379), in Control plane 3
Connections:
- kube-apiserver 1 to etcd member 1
- kube-apiserver 2 to etcd member 2
- kube-apiserver 3 to etcd member 3
- etcd member 1 and etcd member 2: :2380
- etcd member 2 and etcd member 3: :2380
In the stacked topology, the default, every control plane node runs an etcd member next to its API server.
kubeadm join --control-plane adds the new member for you. Each API server points at https://127.0.0.1:2379, its
local member. It is simple and needs no extra machines, but a node failure costs you an API server and an etcd
member at the same moment.
Parts:
- kube-apiserver 1, in Control plane nodes
- kube-apiserver 2, in Control plane nodes
- kube-apiserver 3, in Control plane nodes
- etcd member 1 (10.0.1.11:2379), in Dedicated etcd hosts
- etcd member 2 (10.0.1.12:2379), in Dedicated etcd hosts
- etcd member 3 (10.0.1.13:2379), in Dedicated etcd hosts
Connections:
- kube-apiserver 1 to etcd member 1
- kube-apiserver 1 to etcd member 2
- kube-apiserver 1 to etcd member 3
- kube-apiserver 2 to etcd member 1
- kube-apiserver 2 to etcd member 2
- kube-apiserver 2 to etcd member 3
- kube-apiserver 3 to etcd member 1
- kube-apiserver 3 to etcd member 2
- kube-apiserver 3 to etcd member 3
In the external topology the members run on their own hosts, and you tell kubeadm about them in its configuration file. Every API server then lists all of them:
- --etcd-servers=https://10.0.1.11:2379,https://10.0.1.12:2379,https://10.0.1.13:2379
| Stacked | External | |
|---|---|---|
| Machines for a highly available control plane | 3 | 6 (3 control plane, 3 etcd) |
etcd Pods in kube-system | Yes, one per control plane node | None |
--etcd-servers on the API server | https://127.0.0.1:2379 | A list of other hosts |
| A control plane node fails | Lose one API server and one member | Lose one API server only |
Quorum
etcd members elect a leader and accept a write only when a majority of members, the quorum, has stored it. That rule is what keeps two halves of a split cluster from both accepting different writes. It also decides how many failures you can afford:
| Members | Quorum | Failures tolerated |
|---|---|---|
| 1 | 1 | 0 |
| 2 | 2 | 0 |
| 3 | 2 | 1 |
| 4 | 3 | 1 |
| 5 | 3 | 2 |
When quorum is lost, or the single member is down, the API server cannot read or write. kubectl returns errors or
times out, nothing new is scheduled and nothing heals. The Pods that are already running keep running, because
the kubelet and the container runtime do not need etcd to keep a container alive. The cluster is frozen, not gone.
Using it
Example · Find out how etcd runs on this cluster
A task asks for the etcd version, its data directory, or the certificate it serves. Start by finding out which kind of etcd you have:
kubectl -n kube-system get pods -l component=etcd -o wide
A Pod named etcd-controlplane means a kubeadm static Pod, and stacked etcd. Now read the facts instead of
remembering them. On the control plane node:
sudo grep -E 'image:|data-dir|listen-client|-file' /etc/kubernetes/manifests/etcd.yaml
sudo grep etcd /etc/kubernetes/manifests/kube-apiserver.yaml
If you are not on the node, kubectl -n kube-system describe pod etcd-controlplane shows the same command line.
No etcd Pod at all? Look at --etcd-servers in the API server manifest. Addresses of other machines mean external
etcd, and on those machines systemctl status etcd and systemctl cat etcd show the service and its flags.
Example · See the keys Kubernetes wrote
The etcd image ships with etcdctl, so you can run it inside the Pod, where the certificates are already mounted.
List the namespaces as etcd sees them:
kubectl -n kube-system exec etcd-controlplane -- etcdctl \
--cacert=/etc/kubernetes/pki/etcd/ca.crt \
--cert=/etc/kubernetes/pki/etcd/server.crt \
--key=/etc/kubernetes/pki/etcd/server.key \
get /registry/namespaces --prefix --keys-only
/registry/namespaces/default
/registry/namespaces/kube-node-lease
/registry/namespaces/kube-public
/registry/namespaces/kube-system
/registry/namespaces/shop
The three files are the same ones the etcd flags name: --trusted-ca-file becomes --cacert, --cert-file becomes
--cert and --key-file becomes --key. etcdctl connects to 127.0.0.1:2379 unless you pass --endpoints,
which is right inside the Pod. Leave out --keys-only and you get the protobuf values as well.
Reading is safe. Never write or delete keys by hand: the API server is the only writer that keeps objects valid.
Example · Check etcd when the cluster misbehaves
While the API server still answers, ask it about its etcd connection:
kubectl get --raw='/readyz/etcd'
kubectl -n kube-system logs etcd-controlplane --tail=20
When kubectl itself fails with "connection refused" or a timeout, go to the control plane node. The static Pods
do not need the API server, and neither does the container runtime's own tool:
sudo crictl ps -a --name etcd # running, or exited and restarting?
sudo crictl logs CONTAINER_ID # why it stopped
sudo journalctl -u kubelet --since "10 min ago" | grep -i etcd
Typical causes: a typo in a flag in etcd.yaml, a certificate path that does not exist, a --data-dir that
does not match the volume, or a full disk. Fix the manifest and the kubelet restarts the Pod within a minute.
Tricks
Common mistakes
- Thinking other components use etcd. Only the API server does. A scheduler that cannot schedule is never a scheduler to etcd connection problem.
- Mixing up the ports. 2379 is for clients, 2380 is for members talking to each other.
etcdctlon 2380 fails with a TLS or deadline error. - Using the wrong CA or certificate. The etcd files are under
/etc/kubernetes/pki/etcd/. The API server's client pair,apiserver-etcd-client, sits one folder up. - Editing the mirror Pod.
kubectl edit pod etcd-controlplanechanges nothing that lasts. Edit the manifest file. - Leaving a copy in the manifests folder. The kubelet runs every file it finds there, so
etcd.yaml.baknext toetcd.yamlstarts a second, conflicting etcd. Keep backups of manifests somewhere else. - Expecting a backup of etcd to hold everything. It holds every API object, but not volume data and not the certificates and manifests on the node.
Recap
- etcd holds all cluster state, one key per object under
/registry; lose it without a backup and the cluster is gone. - Only the API server talks to etcd, on port 2379 with its own client certificate.
- On kubeadm clusters etcd is a static Pod:
/etc/kubernetes/manifests/etcd.yaml, data in/var/lib/etcd, certificates in/etc/kubernetes/pki/etcd. - Stacked puts a member on every control plane node; external uses separate hosts. Either way, writes need quorum, so run three or five members.
- When
kubectlfails, usecrictlon the control plane node, and read every path from the manifest.
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
A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.
Talk to the cluster's etcd with etcdctl: identify the member, find the key behind an object, read what etcd really stores for a Secret, and ask the server about itself.
Investigate · Level 2 · standard · target 20 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.