What if your app could fix itself when it's too busy or too quiet, without you doing anything?
Desired replicas vs actual replicas in Kubernetes - When to Use Which
Imagine you have a website that suddenly gets a lot of visitors. You try to start more copies of your website manually on different servers to handle the traffic.
You have to check each server one by one to see if the website is running, and start or stop copies as needed.
This manual way is slow and tiring. You might forget to start enough copies or accidentally start too many, wasting resources.
Also, if a copy crashes, you might not notice quickly, causing your website to slow down or stop.
Kubernetes solves this by letting you say how many copies (replicas) you want, called the desired replicas.
It then watches the actual replicas running and automatically starts or stops copies to match your desired number.
This keeps your website running smoothly without you checking all the time.
ssh server1 start website ssh server2 start website
kubectl scale deployment mysite --replicas=3You can focus on your app while Kubernetes keeps the right number of copies running automatically.
During a big sale, your online store needs more copies to handle many buyers. Kubernetes adjusts the copies up and down without you lifting a finger.
Manually managing copies is slow and error-prone.
Desired replicas tell Kubernetes how many copies you want.
Kubernetes keeps actual replicas matching desired replicas automatically.
Practice
desired replicas value represent in a Kubernetes Deployment?Solution
Step 1: Understand the role of desired replicas
Desired replicas specify how many copies of a pod Kubernetes should maintain to run your app.Step 2: Differentiate from actual replicas
Actual replicas show how many pods are currently running, which may differ temporarily.Final Answer:
The number of pod copies Kubernetes should run -> Option DQuick Check:
Desired replicas = target pod count [OK]
- Confusing desired replicas with actual running pods
- Thinking desired replicas count containers, not pods
- Mixing desired replicas with cluster node count
Solution
Step 1: Identify the correct field name in Deployment spec
The correct field to set desired replicas isreplicasunder the spec section.Step 2: Verify syntax correctness
Usingreplicas: 3is valid YAML and recognized by Kubernetes.Final Answer:
replicas: 3 -> Option CQuick Check:
Use 'replicas' to set desired pod count [OK]
- Using incorrect field names like desiredReplicas or replicaCount
- Placing replicas outside the spec section
- Using plural 'pods' instead of 'replicas'
kubectl get deployment myapp and see:DESIRED=4, CURRENT=3, UP-TO-DATE=3, AVAILABLE=3What does this mean?
Solution
Step 1: Compare desired and current replicas
Desired is 4 but current is 3, so Kubernetes aims for 4 pods but only 3 are running now.Step 2: Interpret the status fields
Up-to-date and available are 3, meaning 3 pods are ready and updated; the 4th pod is likely starting.Final Answer:
Kubernetes is still creating the 4th pod -> Option BQuick Check:
Desired > Current means pods are still launching [OK]
- Assuming pods crashed without checking events
- Thinking deployment scaled down when desired is higher
- Confusing available with desired replicas
Solution
Step 1: Understand why actual replicas lag behind desired
If actual replicas are less than desired, pods may be stuck starting, often due to resource constraints.Step 2: Eliminate other options
Syntax errors prevent deployment creation, incorrect kubectl commands won't show this state, and actual replicas do not update instantly.Final Answer:
Pods are stuck in Pending state due to resource limits -> Option AQuick Check:
Pending pods cause actual < desired replicas [OK]
- Assuming syntax errors cause replica mismatch
- Expecting instant replica count update
- Ignoring pod scheduling issues
replicas: 3 but sometimes kubectl get pods shows only 2 running pods. What should you do to fix this?Solution
Step 1: Investigate why pods are missing
Check pod events and logs to identify issues like crashes, scheduling failures, or resource limits causing pods not to run.Step 2: Fix the root cause
Resolve the identified problem (e.g., fix image, increase resources) so Kubernetes can maintain 3 running pods as desired.Final Answer:
Check pod events and logs to find why one pod is not running and fix the issue -> Option AQuick Check:
Fix pod issues to match actual to desired replicas [OK]
- Scaling replicas blindly without fixing pod issues
- Deleting Deployment unnecessarily
- Assuming Kubernetes fixes pod issues automatically
Ready to prove it?
Pass the test and this pattern moves unknown
