Bird
Raised Fist0
Kubernetesdevops~5 mins

Desired state vs actual state reconciliation in Kubernetes - CLI Comparison

Choose your learning style10 modes available
Introduction
When you tell Kubernetes how you want your app to run, it keeps checking if the real situation matches your instructions. If something changes or breaks, Kubernetes fixes it automatically to match what you asked for.
When you want your app to always run with a certain number of copies, even if some copies crash.
When you update your app and want Kubernetes to replace old versions with new ones smoothly.
When you want to recover automatically if a server or container stops working.
When you want to make sure your app configuration stays the same even if someone changes it by mistake.
When you want to scale your app up or down and have Kubernetes handle the changes.
Config File - deployment.yaml
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-deployment
  labels:
    app: example-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: example-app
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
      - name: example-container
        image: nginx:1.23.3
        ports:
        - containerPort: 80

This file tells Kubernetes you want 3 copies of an Nginx app running. Kubernetes will keep checking and fixing the number of copies to always be 3.

replicas: number of copies you want.

selector: how Kubernetes finds the pods it manages.

template: the pod setup including container image and ports.

Commands
This command tells Kubernetes to create or update the deployment based on the file. Kubernetes will start 3 pods as requested.
Terminal
kubectl apply -f deployment.yaml
Expected OutputExpected
deployment.apps/example-deployment created
This command lists all pods with the label app=example-app to check if 3 pods are running as desired.
Terminal
kubectl get pods -l app=example-app
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-deployment-6f7c9d7f5f-abc12 1/1 Running 0 10s example-deployment-6f7c9d7f5f-def34 1/1 Running 0 10s example-deployment-6f7c9d7f5f-ghi56 1/1 Running 0 10s
→
-l - Filter pods by label
This command deletes one pod manually to simulate a failure. Kubernetes will notice and create a new pod to keep 3 running.
Terminal
kubectl delete pod example-deployment-6f7c9d7f5f-abc12
Expected OutputExpected
pod "example-deployment-6f7c9d7f5f-abc12" deleted
Check again to see Kubernetes created a new pod to replace the deleted one, keeping the desired state.
Terminal
kubectl get pods -l app=example-app
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-deployment-6f7c9d7f5f-def34 1/1 Running 0 30s example-deployment-6f7c9d7f5f-ghi56 1/1 Running 0 30s example-deployment-6f7c9d7f5f-jkl78 1/1 Running 0 10s
→
-l - Filter pods by label
Key Concept

Kubernetes constantly compares the desired state you set with the actual state and fixes differences automatically.

Common Mistakes
Not using labels correctly in the deployment selector and pod template.
Kubernetes cannot match pods to the deployment, so it won't manage them properly.
Ensure the selector labels exactly match the pod template labels.
Manually deleting pods and expecting the deployment to stop creating new ones.
Kubernetes will recreate pods to maintain the desired number, so manual deletion only causes temporary removal.
Scale down the deployment replicas if you want fewer pods.
Summary
Use a deployment YAML file to tell Kubernetes how many copies of your app you want.
Apply the deployment with kubectl apply to create or update the app pods.
Kubernetes watches the actual pods and fixes any difference from the desired state automatically.

Practice

(1/5)
1. What does Kubernetes do when the actual state of a pod differs from the desired state?
easy
A. It changes the actual state to match the desired state automatically.
B. It deletes the pod without recreating it.
C. It ignores the difference and does nothing.
D. It sends an alert but does not change anything.

Solution

  1. 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.
  2. Step 2: Kubernetes reconciliation process

    Kubernetes continuously compares actual state to desired state and makes changes to fix any differences automatically.
  3. Final Answer:

    It changes the actual state to match the desired state automatically. -> Option A
  4. Quick Check:

    Reconciliation means fixing actual state to desired state [OK]
Hint: Kubernetes fixes differences automatically [OK]
Common Mistakes:
  • Thinking Kubernetes ignores differences
  • Believing Kubernetes only alerts without fixing
  • Assuming Kubernetes deletes pods without recreating
2. Which Kubernetes component is responsible for ensuring the actual state matches the desired state?
easy
A. kube-apiserver
B. kube-scheduler
C. etcd
D. kube-controller-manager

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    kube-controller-manager -> Option D
  4. Quick Check:

    Controllers fix state differences [OK]
Hint: Controllers fix state differences [OK]
Common Mistakes:
  • Confusing scheduler with controller
  • Thinking apiserver reconciles state
  • Assuming etcd manages reconciliation
3. Given this YAML snippet for a Deployment:
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?
medium
A. Create a new pod to maintain 3 replicas.
B. Do nothing and keep 2 pods running.
C. Delete all pods and restart the deployment.
D. Scale down the deployment to 2 replicas.

Solution

  1. Step 1: Understand the desired replicas

    The Deployment specifies 3 replicas, so Kubernetes wants 3 pods running.
  2. 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.
  3. Final Answer:

    Create a new pod to maintain 3 replicas. -> Option A
  4. Quick Check:

    Pods count fixed to desired replicas [OK]
Hint: Pods auto-recreated to match replicas [OK]
Common Mistakes:
  • Thinking Kubernetes ignores crashed pods
  • Assuming deployment restarts all pods
  • Believing deployment scales down automatically
4. You notice a Deployment is stuck with fewer pods than desired. Which of these is a likely cause preventing reconciliation?
medium
A. The kube-apiserver is running normally.
B. The image name in the container spec is correct.
C. The Deployment's selector labels do not match pod labels.
D. The desired replicas count is set to 3.

Solution

  1. 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.
  2. Step 2: Understand other options

    Correct image name, running apiserver, and desired replicas do not prevent reconciliation by themselves.
  3. Final Answer:

    The Deployment's selector labels do not match pod labels. -> Option C
  4. Quick Check:

    Label mismatch blocks reconciliation [OK]
Hint: Selector and pod labels must match [OK]
Common Mistakes:
  • Ignoring label selector importance
  • Blaming image name for pod count issues
  • Assuming apiserver status causes pod count
5. You want to update a Deployment's container image from 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?
hard
A. Kubernetes ignores image changes unless manually restarted.
B. Kubernetes gradually updates pods to match desired state using rolling updates.
C. Kubernetes cannot update pods without deleting the Deployment.
D. Kubernetes immediately deletes all old pods and creates new ones.

Solution

  1. Step 1: Understand rolling update strategy

    Kubernetes uses rolling updates to gradually replace old pods with new ones to avoid downtime.
  2. 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.
  3. Final Answer:

    Kubernetes gradually updates pods to match desired state using rolling updates. -> Option B
  4. Quick Check:

    Rolling update means gradual pod replacement [OK]
Hint: Rolling updates replace pods gradually [OK]
Common Mistakes:
  • Expecting instant pod replacement
  • Thinking Deployment must be deleted to update
  • Believing Kubernetes ignores image changes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard