Sub topic 8
Controllers: making reality match the spec
This chapter assumes you know what a Pod and a Deployment are, can read YAML, and have seen the reconciliation idea from the earlier chapter on desired state. Here you will see how controllers do that work and which component runs them.
What a controller is
A controller is a loop that watches the state of some objects in the cluster and acts to move the actual state toward the desired state. Each controller owns one kind of job. The Deployment controller manages ReplicaSets. The ReplicaSet controller manages Pods. The Node controller notices when nodes stop reporting.
A controller does not talk to containers directly. It reads and writes objects through the API server, and other components such as the scheduler and kubelet react to those changes.
The reconcile loop
Every controller repeats the same cycle:
- Receive a notification that an object it cares about has changed (a watch event).
- Read the desired state from the object's
spec. - Observe the actual state, for example by counting the Pods that match a selector.
- Make the smallest change that closes the gap, such as creating or deleting a Pod.
- Write what it saw to the object's
status.
The loop is level-based, not edge-based. A controller does not need to know what happened. It only compares what should exist with what does exist. If a controller restarts and misses events, it still converges on the next pass.
kube-controller-manager
Most built-in controllers are compiled into one binary, kube-controller-manager, which runs on the control plane. It bundles many controllers as separate loops in a single process. Cloud-specific logic, such as creating load balancers, runs in the separate cloud-controller-manager.
You can usually see it as a Pod in the kube-system namespace:
kubectl get pods -n kube-systemOn managed clusters the control plane may be hidden from you, so this Pod may not appear.
Watching a controller work
Apply the Deployment from earlier chapters, then delete one Pod:
kubectl apply -f deployment.yaml
kubectl get pods -l app=web
kubectl delete pod <pod-name>
kubectl get pods -l app=webThe second listing shows three Pods again. The ReplicaSet controller saw two matching Pods where the spec says three, and it created a new one. The Pod name is new, because the controller creates a replacement and does not restart the deleted Pod.
Events record the activity:
kubectl get events --sort-by=.metadata.creationTimestampCommon errors
- Editing a Pod by hand and expecting it to stay. A controller will revert or replace it to match the spec.
- Expecting instant results. Reconciliation is asynchronous, so check
kubectl getagain or usekubectl describe. - Confusing
specandstatus. You writespec. Controllers writestatus. - Overlapping selectors on two controllers. Both may claim the same Pods and fight over them.
Exercise
You run kubectl scale deployment web --replicas=5, and a few seconds later five Pods exist. Which component created the Pods, and which part of the Deployment object did you change?
Show answer
The ReplicaSet controller, running in kube-controller-manager, created them. First, the Deployment controller updated the ReplicaSet's replica count. You changed spec.replicas, which is the desired state. The controllers then reconciled actual state to match it.