0
0
Kubernetesdevops~10 mins

Desired state vs actual state reconciliation in Kubernetes - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Process Flow - Desired state vs actual state reconciliation
User defines desired state
Kubernetes stores desired state
Controller checks actual state
Compare actual state with desired state
Do nothing
Update actual state
Loop back to check actual state
This flow shows how Kubernetes controllers continuously compare the desired state with the actual state and make changes to keep the system as the user wants.
Execution Sample
Kubernetes
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: nginx
    image: nginx:latest
This YAML defines the desired state of a Pod named 'my-pod' running an nginx container.
Process Table
StepActionDesired StateActual StateComparison ResultController Action
1User applies Pod YAMLPod 'my-pod' with nginx containerNo Pod existsNo matchCreate Pod 'my-pod'
2Controller checks clusterPod 'my-pod' with nginx containerPod 'my-pod' startingPartial matchWait for Pod to be ready
3Pod starts runningPod 'my-pod' with nginx containerPod 'my-pod' runningMatchNo action needed
4Pod crashes unexpectedlyPod 'my-pod' with nginx containerPod 'my-pod' crashedNo matchRestart Pod
5Pod restarts and runsPod 'my-pod' with nginx containerPod 'my-pod' runningMatchNo action needed
6User deletes Pod YAMLNo Pod desiredPod 'my-pod' runningNo matchDelete Pod 'my-pod'
7Pod deletedNo Pod desiredNo Pod existsMatchNo action needed
💡 Desired state matches actual state; reconciliation complete.
Status Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 6Final
Desired StateNo PodPod 'my-pod' with nginxPod 'my-pod' with nginxPod 'my-pod' with nginxNo PodNo Pod
Actual StateNo PodPod 'my-pod' startingPod 'my-pod' runningPod 'my-pod' crashedPod 'my-pod' runningNo Pod
Key Moments - 3 Insights
Why does the controller restart the Pod after it crashes instead of creating a new one?
The controller sees the Pod exists but is not in the desired running state (Step 4). It tries to fix the actual state by restarting the existing Pod to match the desired state, not by creating a new Pod.
What happens if the desired state is deleted but the Pod still runs?
The controller detects the mismatch (Step 6) and deletes the running Pod to match the desired state of no Pod.
Why does the controller sometimes wait instead of acting immediately?
When the Pod is starting (Step 2), the actual state partially matches the desired state, so the controller waits for the Pod to become ready before taking further action.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the actual state at Step 3?
ANo Pod exists
BPod 'my-pod' crashed
CPod 'my-pod' running
DPod 'my-pod' starting
💡 Hint
Check the 'Actual State' column for Step 3 in the execution table.
At which step does the controller delete the Pod to match the desired state?
AStep 4
BStep 6
CStep 2
DStep 1
💡 Hint
Look for the step where desired state is 'No Pod' but actual state is 'Pod running'.
If the Pod never crashes, which steps would be skipped?
ASteps 4 and 5
BSteps 2 and 3
CSteps 6 and 7
DSteps 1 and 2
💡 Hint
Check the steps related to Pod crashing and restarting in the execution table.
Concept Snapshot
Desired state is what you want the system to be.
Actual state is what is currently running.
Kubernetes controllers compare both.
If they differ, controllers act to fix actual state.
This loop keeps your system stable and as you defined.
Full Transcript
In Kubernetes, you tell the system what you want by defining the desired state, like a Pod running nginx. The system checks what is actually running, called the actual state. If the actual state does not match the desired state, Kubernetes controllers take action to fix it. For example, if the Pod crashes, the controller restarts it. If you delete the Pod definition, the controller deletes the running Pod. This continuous check and fix process is called reconciliation, and it keeps your cluster running as you want.