Replica count
- Docker & Kubernetes course
- Intermediate
- Lesson 27/82
- 4 min
Learn the Pod replica count with the replicas field.
Replica count
The replica count changes in two ways: editing replicas in the YAML and applying again (declarative) or kubectl scale (imperative). The declarative way is preferred; otherwise the next apply undoes your manual change:
kubectl scale deployment web --replicas=5
# set replicas: 5 in web.yaml and:
kubectl apply -f web.yamlSetting the replica count to 0 stops the application completely without deleting it.
Watching scaling
After scaling, kubectl rollout status waits until all replicas are ready; it is used in CI/CD scripts to make sure the deployment has finished:
$ kubectl scale deploy web --replicas=0
$ kubectl get deploy web
NAME READY UP-TO-DATE AVAILABLE
web 0/0 0 0
$ kubectl scale deploy web --replicas=3
$ kubectl rollout status deploy/web
deployment "web" successfully rolled outPractice Stop and start again
Write three commands: scale the web deployment down to 0 replicas; then up to 4; wait for the rollout to finish.
Starter code
# buraya yazSample solution
kubectl scale deployment web --replicas=0
kubectl scale deployment web --replicas=4
kubectl rollout status deployment/webExpected output
deployment.apps/web scaled deployment.apps/web scaled Waiting for deployment "web" rollout to finish: 2 of 4 updated replicas are available... deployment "web" successfully rolled out
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 You scaled to 5 with kubectl scale, but web.yaml says replicas: 3. How many replicas after kubectl apply -f web.yaml?
- 0
- 3 Correct answer
- 5
- 8
Explanation: The file is the desired state; apply applies it again.
2 Wait until all replicas are ready.
kubectl rollout ____ deploy/web
kubectl rollout status deploy/web
Explanation: rollout status exits when the rollout is done.
3 Match.
| Term | Meaning |
|---|---|
kubectl scale |
Imperative scaling |
replicas: N + apply |
Declarative scaling |
--replicas=0 |
Stop without deleting |
rollout status |
Wait until done |