0
0
Kubernetesdevops~30 mins

Desired state vs actual state reconciliation in Kubernetes - Hands-On Comparison

Choose your learning style9 modes available
Desired State vs Actual State Reconciliation in Kubernetes
📖 Scenario: You are managing a small web application running on Kubernetes. You want to ensure that the number of running pods always matches the number you specify in your deployment configuration. This is called reconciliation, where Kubernetes compares the desired state you set with the actual state running in the cluster and makes changes to match them.
🎯 Goal: Learn how to define a Kubernetes Deployment manifest with a specific number of replicas (desired state), check the actual number of running pods, and understand how Kubernetes reconciles the difference automatically.
📋 What You'll Learn
Create a Kubernetes Deployment manifest with exactly 3 replicas
Add a label selector to the Deployment to identify pods
Use kubectl commands to check the actual number of running pods
Observe how Kubernetes reconciles the actual state to match the desired state
💡 Why This Matters
🌍 Real World
Kubernetes operators and DevOps engineers use reconciliation to keep applications running as expected, automatically fixing differences between desired and actual states.
💼 Career
Understanding reconciliation is essential for managing Kubernetes workloads reliably and troubleshooting deployment issues in real-world cloud environments.
Progress0 / 4 steps
1
Create a Deployment manifest with 3 replicas
Create a YAML file named deployment.yaml with a Deployment resource. Set replicas to 3. Use app: mywebapp as the label under metadata.labels and spec.selector.matchLabels. Use the container image nginx:latest.
Kubernetes
Need a hint?

Make sure the replicas field is set to 3 and the labels match exactly app: mywebapp.

2
Add a label selector to the Deployment
Ensure the Deployment manifest has a spec.selector.matchLabels field with app: mywebapp to select pods correctly. This helps Kubernetes know which pods belong to this Deployment.
Kubernetes
Need a hint?

The selector.matchLabels must exactly match the pod template labels.

3
Check the actual number of running pods
After applying the Deployment with kubectl apply -f deployment.yaml, run the command kubectl get pods -l app=mywebapp to list pods with the label app=mywebapp. Count how many pods are in the Running state.
Kubernetes
Need a hint?

Use the label selector -l app=mywebapp to filter pods belonging to your Deployment.

4
Observe reconciliation by scaling replicas
Run kubectl scale deployment mywebapp-deployment --replicas=5 to change the desired state to 5 replicas. Then run kubectl get pods -l app=mywebapp --no-headers | wc -l to count how many pods are running. Kubernetes will create new pods to match the desired state.
Kubernetes
Need a hint?

Scaling changes the desired state. Kubernetes will create or delete pods to match it.