What is a ReplicaSet?
- Docker & Kubernetes course
- Intermediate
- Lesson 31/82
- 4 min
Learn that a ReplicaSet ensures Pod replicas.
What is a ReplicaSet?
A ReplicaSet's only job is to make sure that, at every moment, exactly replicas pods matching its selector exist. If some are missing it creates new ones from the template; if there are too many it deletes some. It knows nothing of version transitions or rollbacks; the Deployment adds those.
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
web-7c5b9f6d8 3 3 3 2h
web-6b4d8c7f5 0 0 0 3h # old version, for rollbackThat is why ReplicaSets are used not directly but through a Deployment.
The trouble with a bare ReplicaSet
If you change the image in the template of a directly created ReplicaSet, the existing pods aren't updated; only pods created from then on get the new image. A Deployment in this case opens a new ReplicaSet and replaces all the pods in an orderly way.
kubectl set image rs/api api=api:2.0
kubectl get pods -l app=api -o jsonpath='{.items[*].spec.containers[0].image}'
# api:1.0 api:1.0 api:1.0 <- still oldPractice Inspect the ReplicaSets
Write two commands: list the ReplicaSets labelled app=web; then show the history of the web deployment.
Starter code
# buraya yazSample solution
kubectl get rs -l app=web
kubectl rollout history deployment/webExpected output
NAME DESIRED CURRENT READY AGE web-7c5b9f6d8 3 3 3 2h web-6b4d8c7f5 0 0 0 3h REVISION CHANGE-CAUSE 1 <none> 2 nginx 1.27.3
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Why isn't the old ReplicaSet with DESIRED 0 deleted?
- It can't be deleted
- It remained because of an error
- It creates new pods
- It is kept in the Deployment history for rollback Correct answer
Explanation: revisionHistoryLimit determines how many are kept.
2 List the ReplicaSets.
kubectl get ____
kubectl get rs
Explanation: rs is short for replicasets.
3 Match.
| Term | Meaning |
|---|---|
DESIRED |
Desired replicas |
CURRENT |
Existing replicas |
READY |
Ready replicas |
DESIRED 0 |
Old version |