Sub topic 3
The API Server and etcd as the Shared Source of Truth
This chapter assumes you know basic Kubernetes objects, can use kubectl get and kubectl describe, can read YAML, and understand REST and JSON at a basic level.
One place where state lives
Controllers do not talk to each other, and they do not keep their own copy of the cluster's truth. They all read and write the same records through one component, the kube-apiserver. The API server stores every object in etcd, a consistent, distributed key-value store. This gives the whole system a single source of truth.
Only the API server talks to etcd. Controllers, the scheduler, the kubelet and kubectl are all clients of the API server.
What the API server does
For each request, the API server does the following:
- Authenticates the caller and checks that it is authorized.
- Runs admission control, which can mutate or reject the object.
- Validates the object against its schema.
- Persists the result to etcd.
- Notifies anyone watching that resource.
Because every write passes through this path, no component can bypass validation or access rules. The next chapters cover how watches deliver the notifications.
Objects are resources with a version
Each object is addressed by a REST path. A Deployment named web in the default namespace lives at /apis/apps/v1/namespaces/default/deployments/web. You can read it directly:
kubectl get --raw /apis/apps/v1/namespaces/default/deployments/webEach object carries metadata.resourceVersion, an opaque string derived from etcd's revision counter. It changes on every write. The API server uses it to detect conflicting updates, which a later chapter explains. Treat it as opaque. Do not parse it or compare it numerically.
Why etcd matters
etcd uses the Raft consensus algorithm. A write is committed only when a majority of etcd members accept it. This means:
- Reads of committed data are consistent. Two controllers asking the same question get answers from one agreed history.
- The cluster can lose a minority of etcd members and keep working.
- If etcd loses its majority, the API server cannot write. Running Pods usually keep running, but nothing can change the desired state until etcd recovers.
Common mistakes
- Storing important state only in a controller's memory. After a restart, that state is gone. Put it in the object's
statusor in another API object. - Reading and writing etcd directly. This skips validation, admission and watch notifications, and it can corrupt the cluster.
- Assuming a successful write means the work is done. The API server only records your intent. Controllers act on it later.
Exercise
A controller crashes and restarts on a new node. Why does the cluster not lose track of the objects it was managing?
Show answer
The objects and their state are stored in etcd through the API server, not in the controller. The restarted controller lists the current objects from the API server and resumes work from there.
Exercise
Why should a controller send writes through the API server rather than write to etcd directly?
Show answer
Writes through the API server go through authentication, authorization, admission and validation, and they trigger watch notifications to other components. Writing to etcd directly skips all of these.