Desired state vs actual state reconciliation in Kubernetes - Performance Comparison
We want to understand how the work done by Kubernetes grows as it checks and fixes the cluster state.
How does the time to match the desired state with the actual state change when the cluster size grows?
Analyze the time complexity of the reconciliation loop in Kubernetes.
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: app
image: example-image
# Controller watches all Pods and compares desired vs actual state
for each pod in cluster {
if pod.actualState != pod.desiredState {
update pod to desiredState
}
}
This code watches all pods and updates any that do not match the desired state.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all pods in the cluster to check their state.
- How many times: Once per reconciliation cycle, the loop runs once for each pod.
As the number of pods increases, the controller must check each one, so the work grows with the number of pods.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks and possible updates |
| 100 | 100 checks and possible updates |
| 1000 | 1000 checks and possible updates |
Pattern observation: The work grows directly with the number of pods; doubling pods doubles the work.
Time Complexity: O(n)
This means the time to reconcile grows linearly with the number of pods in the cluster.
[X] Wrong: "The reconciliation time stays the same no matter how many pods there are."
[OK] Correct: The controller must check each pod, so more pods mean more work and more time.
Understanding how Kubernetes controllers scale their work helps you explain system behavior and design efficient solutions.
"What if the controller only checked pods that changed instead of all pods? How would the time complexity change?"
Practice
actual state of a pod differs from the desired state?Solution
Step 1: Understand desired vs actual state
The desired state is what you want Kubernetes to have running, and the actual state is what is currently running.Step 2: Kubernetes reconciliation process
Kubernetes continuously compares actual state to desired state and makes changes to fix any differences automatically.Final Answer:
It changes the actual state to match the desired state automatically. -> Option AQuick Check:
Reconciliation means fixing actual state to desired state [OK]
- Thinking Kubernetes ignores differences
- Believing Kubernetes only alerts without fixing
- Assuming Kubernetes deletes pods without recreating
Solution
Step 1: Identify the role of kube-controller-manager
The kube-controller-manager runs controllers that watch the desired state and make changes to match the actual state.Step 2: Differentiate other components
kube-apiserver handles API requests, kube-scheduler assigns pods to nodes, and etcd stores cluster data but does not reconcile states.Final Answer:
kube-controller-manager -> Option DQuick Check:
Controllers fix state differences [OK]
- Confusing scheduler with controller
- Thinking apiserver reconciles state
- Assuming etcd manages reconciliation
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: web
image: nginx:1.19
If one pod crashes and stops running, what will Kubernetes do?Solution
Step 1: Understand the desired replicas
The Deployment specifies 3 replicas, so Kubernetes wants 3 pods running.Step 2: Reconciliation after pod crash
If one pod crashes, Kubernetes detects the actual state has only 2 pods and creates a new pod to restore 3 replicas.Final Answer:
Create a new pod to maintain 3 replicas. -> Option AQuick Check:
Pods count fixed to desired replicas [OK]
- Thinking Kubernetes ignores crashed pods
- Assuming deployment restarts all pods
- Believing deployment scales down automatically
Solution
Step 1: Check label selectors for matching pods
If the Deployment's selector labels do not match pod labels, Kubernetes cannot track pods correctly, so reconciliation fails.Step 2: Understand other options
Correct image name, running apiserver, and desired replicas do not prevent reconciliation by themselves.Final Answer:
The Deployment's selector labels do not match pod labels. -> Option CQuick Check:
Label mismatch blocks reconciliation [OK]
- Ignoring label selector importance
- Blaming image name for pod count issues
- Assuming apiserver status causes pod count
nginx:1.19 to nginx:1.21. After applying the change, you see some pods still run the old image. What explains this behavior in terms of desired vs actual state reconciliation?Solution
Step 1: Understand rolling update strategy
Kubernetes uses rolling updates to gradually replace old pods with new ones to avoid downtime.Step 2: Explain why old pods remain temporarily
During rolling update, some pods run old image until replaced, so actual state temporarily differs from desired state.Final Answer:
Kubernetes gradually updates pods to match desired state using rolling updates. -> Option BQuick Check:
Rolling update means gradual pod replacement [OK]
- Expecting instant pod replacement
- Thinking Deployment must be deleted to update
- Believing Kubernetes ignores image changes
Ready to prove it?
Pass the test and this pattern moves unknown
