3 min read | by Jordi Prats
Although ReplicaSet's main purpose is to maintain a stable set of replica Pods, it's not a kubernetes object that is commonly created, at least not explicitly. But the replicas attribute on the Deployment object is actually related to this object
In fact, every time we create a Deployment we are implicitly creating a ReplicaSet: The real purpose of a Deployment is to provide declarative updates for Pods and ReplicaSets: like an interface. We can check this using this example:
$ kubectl get deploy pet2cattle
NAME READY UP-TO-DATE AVAILABLE AGE
pet2cattle 2/2 2 2 51d
By using kubectl describe we can get which ReplicaSet this deployment has created:
$ kubectl describe deploy pet2cattle | grep -i replicaset
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: pet2cattle-5479f4b6cb (2/2 replicas created)
Checking the ReplicaSet itself we can see that the number of replicas matched the replicas defined on the deployment:
$ kubectl get deploy pet2cattle -o 'jsonpath={.spec.replicas}{"\n"}'
2
$ kubectl get rs pet2cattle-5479f4b6cb
NAME DESIRED CURRENT READY AGE
pet2cattle-5479f4b6cb 2 2 2 5d5h
Using kubectl scale to scale up the replicas to three on the deploy:
$ kubectl scale deploy pet2cattle --replicas=3
deployment.apps/pet2cattle scaled
It gets propagated to the ReplicaSet by the Deployment controller:
$ kubectl get rs pet2cattle-5479f4b6cb
NAME DESIRED CURRENT READY AGE
pet2cattle-5479f4b6cb 3 3 2 5d5h
Nevertheless, if we try to to the same on the ReplicaSet side we won't get any error:
$ kubectl scale rs pet2cattle-5479f4b6cb --replicas=4
replicaset.apps/pet2cattle-5479f4b6cb scaled
But the number of pods will be quickly scaled down to the previous value by the deployment controller:
Normal SuccessfulCreate 6s replicaset-controller Created pod: pet2cattle-5479f4b6cb-69vzw
Normal SuccessfulDelete 6s replicaset-controller Deleted pod: pet2cattle-5479f4b6cb-69vzw
We need to be fast to actually see the number change, by launching the scale up together with the kubectl get rs we might be able to catch it at 4 instead of 3, but we won't be able to catch it every time we try:
$ kubectl get rs pet2cattle-5479f4b6cb -o 'jsonpath={.spec.replicas}{"\n"}'; kubectl scale rs pet2cattle-5479f4b6cb --replicas=4; kubectl get rs pet2cattle-5479f4b6cb -o 'jsonpath={.spec.replicas}{"\n"}'
3
replicaset.apps/pet2cattle-5479f4b6cb scaled
3
$ kubectl get rs pet2cattle-5479f4b6cb -o 'jsonpath={.spec.replicas}{"\n"}'; kubectl scale rs pet2cattle-5479f4b6cb --replicas=4; kubectl get rs pet2cattle-5479f4b6cb -o 'jsonpath={.spec.replicas}{"\n"}'
3
replicaset.apps/pet2cattle-5479f4b6cb scaled
4
Posted on 04/03/2021