replicaset

 ReplicaSet:

    A RepicaSet helps you monitor and ensure that the specified number of pod replicas are running at any given time. For example, if you define "replicas: 3" in your yaml file, then your ReplicaSet ensures to keep the pods count as "3" at any given time. When ReplicaSet needs to create new pods, it uses its Pod template, which means that the part of pod yaml is used as a template within the same yaml file as below. 




Why Selector is Required ?

    In a cluster, you may have thousands of pods running, but your replicaset job is to find and monitor those 3 pods created as part of the replicaset. yaml and need to keep it always "3". To perform this, there should be a linkage between PODS and ReplicaSet. The Labels and Selectors will do this linkage. Let us say that if someone deletes one of the pods, then it will automatically create the deleted pods.

Commands to create replicaset:


Please download your code here: replicasetpods.yaml

kubectl create -f replicasetpods.yaml
replicaset.apps/replicasetdemo created

kubectl get rs
NAME             DESIRED   CURRENT   READY   AGE
replicasetdemo   3         3         3       7s

kubectl get pods
NAME                   READY   STATUS    RESTARTS   AGE
replicasetdemo-bsqtf   1/1     Running   0          15s
replicasetdemo-rd6p4   1/1     Running   0          15s
replicasetdemo-z5h79   1/1     Running   0          15s

How to test self-healing steps?

    To demonstrate the self-healing, I am going to delete the highlighted pods that is running

kubectl get pods
NAME                   READY   STATUS    RESTARTS   AGE
replicasetdemo-bsqtf   1/1     Running   0          14m
replicasetdemo-rd6p4   1/1     Running   0          14m
replicasetdemo-z5h79   1/1     Running   0          14m

kubectl delete pods replicasetdemo-bsqtf
pod "replicasetdemo-bsqtf" deleted

The below output shows that the new pod "replicasetdemo-hrbdh" got created within a few seconds. By making the new pod in the place of deleted pods, the replicaset completes its job as per the desired count to keep it "3"

kubectl get pods
NAME                   READY   STATUS    RESTARTS   AGE
replicasetdemo-hrbdh   1/1     Running   0          5s
replicasetdemo-rd6p4   1/1     Running   0          14m
replicasetdemo-z5h79   1/1     Running   0          14m

Is this the best practice to have replicaset to manage desired state, scaling?

The answer is straight "NO". In the Kubernetes cluster, the "Deployment Controller" sits above the "ReplicaSet Controller" to manage all these desired state and scaling etc. In addition to that, it also has features to perform rollout, rollback, pause rollout etc. Let us see this in details under the deployment section