Sub topic 2
Controllers and the Reconciliation Loop
This chapter assumes you know what a Pod, a node and a cluster are, and that you can read YAML and run kubectl get, describe and apply. It also assumes you have seen labels and selectors, which the previous chapter covered.
Desired state and observed state
Kubernetes stores two kinds of facts about each object. The desired state is what you asked for, written in the object's spec. The observed state is what is true right now, reported in its status.
A controller is a process that watches objects of one kind and acts to make observed state match desired state. The ReplicaSet controller and the Deployment controller both run inside kube-controller-manager, a component of the control plane.
The reconciliation loop
Every controller repeats the same cycle, called reconciliation:
- Read the desired state from the API server.
- Read the observed state, for example the Pods that match a selector.
- Compare them.
- If they differ, create, update or delete objects to close the gap.
- Wait for the next change and start again.
Controllers do not run a script once and exit. They are level-based: they react to the current difference, not to a remembered event. If a controller restarts and misses an event, it still finds the gap on its next pass.
A worked example
Suppose a ReplicaSet has replicas: 3 and three matching Pods exist. Someone deletes one Pod with kubectl delete pod. The controller sees a change in the Pod set and runs a pass:
- Desired: 3 Pods.
- Observed: 2 Pods.
- Action: create 1 Pod from the Pod template.
If instead the count were 4, the action would be to delete one Pod. The controller never edits a running Pod to repair it. It only adds or removes whole Pods.
Controllers talk only to the API server
A controller does not start containers. It creates a Pod object through the API server. The scheduler then picks a node, and the kubelet on that node starts the containers. Each component does one job and reads or writes shared state through the API server.
This design explains several behaviors you will see later:
- Changes are asynchronous.
kubectl applyreturns before the cluster has finished converging. - Controllers retry. A failed step is attempted again on the next pass.
- Objects chain together. A Deployment controller manages ReplicaSets, and a ReplicaSet controller manages Pods.
Common errors
- Expecting instant results. After a change,
statuslagsspec. Checkkubectl getagain or usekubectl describeand read the Events section. - Fighting the controller. If you create an extra Pod by hand with labels that match a ReplicaSet's selector, the controller counts it and may delete one Pod to reach the target.
- Editing a Pod to fix it. Changes to a managed Pod do not change the template. A replacement Pod comes from the old template.
Exercise
A ReplicaSet has replicas: 3 and three running Pods. You delete one Pod. Describe what the controller observes, what it compares, and what it does.
Show answer
It observes 2 Pods matching its selector, compares that with the desired 3, finds a gap of 1, and creates one new Pod from its Pod template through the API server. The scheduler and kubelet then place and start it.