Sub topic 3
Desired State and Reconciliation Loops
This chapter assumes you know what containers and images are, can read YAML, and have seen the Deployment manifest from earlier in this tutorial. It explains the idea that most Kubernetes behavior follows from: you declare what you want, and controllers keep working until the cluster matches.
Imperative versus declarative
An imperative command says how to do something: "start a container on host A." A declarative object says what should be true: "three copies of this Pod should be running." Kubernetes favors the second style.
Every object has two parts:
specis the desired state. You write it.statusis the observed state. Kubernetes components write it.
In the Deployment from earlier, replicas: 3 lives in spec. The number of Pods that are actually ready appears in status.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80When you run kubectl apply -f deployment.yaml, the API server stores this spec. Nothing is started at that moment. Controllers notice the stored object and begin acting on it.
The reconciliation loop
A controller is a process that runs a loop for one kind of object:
- Observe the current state.
- Compare it with the desired state in
spec. - Act to reduce the difference.
- Repeat.
Controllers do not poll blindly. They watch the API server and receive a notification when a relevant object changes. They also re-check periodically, so a missed event does not leave the cluster wrong forever.
Why this model is useful
Suppose one of your three web Pods dies because its node fails. No one issues a "restart" command. The ReplicaSet controller sees two Pods where spec asks for three, and it creates a new one. The same loop handles scaling, since changing replicas to 5 simply changes the desired state.
Reconciliation is also level-based, not edge-based. The controller reacts to the current difference, not to the specific event that caused it. Because of this, a controller that restarts or misses a notification still reaches the right outcome.
The loop is idempotent: running it again when the state already matches does nothing. This is why kubectl apply is safe to repeat.
Common errors
- Expecting instant results. Reconciliation is eventual. Right after
apply,kubectl get podsmay show fewer Pods thanreplicas. - Fighting a controller. If you delete a Pod owned by a Deployment, a replacement appears. Change the Deployment's
specinstead. - Reading
specas fact. A spec says what you want, not what exists. Checkstatus, for example withkubectl get deployment web. - Unfixable desired state. A typo such as an image tag that does not exist is accepted by the API server. The controller keeps trying, and the Pods stay in a failing state such as
ImagePullBackOff.
Exercise
You run kubectl delete pod on one of the three Pods managed by the web Deployment. Explain why the Pod count returns to three, naming the desired state and the mechanism.
Show answer
The Deployment (through its ReplicaSet) has replicas: 3 as desired state. A controller watching Pods sees only two. It compares that with the spec, creates a new Pod to remove the difference, and keeps looping so the observed state matches the desired state.