Sub topic 2
Controllers and the control loop
This chapter assumes you know Pods, Deployments, and ReplicaSets, can use kubectl, and understand the spec/status split. Here you will see how Kubernetes turns a declared desired state into running objects. Every operator you build later uses the same mechanism.
The loop
A controller is a process that watches objects of one or more types and acts to make the real world match what those objects declare. It does not run a script once. It runs a loop:
- Observe the desired state in
specand the actual state of the cluster. - Compare them.
- Act to reduce the difference.
- Repeat whenever something changes.
This is called reconciliation. The built-in Deployment controller works this way. You declare replicas: 3. If a Pod dies, the controller sees two Pods, compares that with the desired three, and creates one more.
Where controllers run
Controllers talk to the API server like any other client. They do not touch etcd directly. The built-in ones run inside kube-controller-manager. A controller you write runs as an ordinary Pod, or even on your laptop, using a kubeconfig.
A controller does not poll the API server for everything on a timer. It opens a watch, which is a long-lived request that streams changes. Most implementations also keep a local cache of objects, so reads are cheap. When an object changes, its key (namespace and name) is put on a work queue. A worker takes a key off the queue and runs the reconcile logic for that one object.
Properties that matter
- Idempotent. Running the logic twice on the same state gives the same result. Retries and duplicate events are normal, so this is required.
- Level-triggered. The logic reads current state and does not depend on which event woke it up.
- Eventually consistent. The cluster is not fixed instantly. Each pass moves it closer to the desired state, and failures are retried with backoff.
- Single owner of its types. Each kind of object is managed by one controller. Two controllers fighting over the same field produce endless churn.
Watching it work
You can observe the loop with a Deployment.
kubectl create deployment web --image=nginx:1.27 --replicas=3
kubectl get pods -l app=web
kubectl delete pod -l app=web --wait=false
kubectl get pods -l app=web -wNew Pods appear within seconds. You deleted the actual state, and the ReplicaSet controller restored it. Nothing was told to "create a Pod". The desired count was simply still 3.
Common mistakes
- Treating events as commands, such as "a Pod was deleted, so create a Pod". Compare counts or fields instead.
- Keeping state in controller memory. After a restart it is gone. Store state in the API objects or in the external system.
- Writing logic that fails when an object already exists. Check first, or use create-or-update.
Exercise
A controller crashes for ten minutes while three users change the desired replicas of an object several times each. When it restarts, how does it know what to do, and why does it not need the missed events?
Show answer
It reads the object's current spec and the actual state, compares them, and acts on the difference. Because controllers are level-triggered, only the latest desired state matters, so the intermediate changes do not need to be replayed.