Kubernetes and its ReplicaSets
Updated: Aug 3, 2020
In the previous article, I mentioned about the Pods and Containers and how it gets deployed. Now that was pretty straight forward step to design the pods. The question here arise that how will you ensure that you application will be running without any downtime and how to determine that certain number of Pods instances will always be there up and running at any given moment of time? The answer is ReplicaSet
In this article, i will walk you through the concept of ReplicaSet and its alternatives.
As name suggests, the number of sets available as replica. The replicaset ensures that a certain number of pods will always be running and available no matter what. For example, if you define that 3 replicas should always running, there will be 3 copies of the pods created. If 1 or 2 or all Pods go down for some reason, the controller will be deploying a new one within no time and terminate the faulty one and even if the number exceeds, they get killed as well (Pods are mortal,they born and die).
I created below "frontend.yaml" file to explain replicaset
The manifest holds the basic information for
a. apiVersion : apps/v1
b. kind : ReplicaSet
c. metadata : holds information about name and labels in particular
d. spec : defines the no. of replicas (3 ins this example) and selectors
Point to be noted here is that ReplicaSet uses labels and selectors to check if new pods that it owns. If it is a new pod and matches the selector, the replicaset will take ownership and control it even it that new pod was created manually. Basically, ReplicaSets and Replication controllers go hand-in-hand. There is not much difference between both. Infact, I would say replicaset is next-gen replication controller. The only difference I could find out with both are "Selectors"
You should apply frontend.yaml file with "kubectl apply" command and frontend file is available at K8s.io/docs
(you can change the number and edit the name as per your wish)
Once you have run the command, you can check the output with below commands
# kubectl get rs (brief output)
# kubectl decribe rs/frontend (detailed output)
With above output, you can see there are 3 replicaset created with below info. That means the replicasets of pod is running fine without any error/warning.
DESIRED : 3
CURRENT : 3
READY : 3
and now if you give command to check if the new pods are created
# kubectl get pods
and see 3 new pods are created
So far so good ? Now, Lets test by deleting the pod, As per the technology, controller will match the current number of pods with desired number of pods and get the missing pod back. I deleted one pod and checked after some seconds, the pod automatically got created. Really amazing isn't it.
Command to delete
# kubectl delete pod frontend-2nmvn
Deleted pod : frontend-2nmvn (created 59mins ago)
Auto created new pod : frontend-vlj69 (created 76secs ago)
This is it from ReplicaSet end. There are few alternatives to replicaset as well which are explained below
1.Deployment is an object which can own ReplicaSets and update them and their Pods via declarative, server-side rolling updates. While ReplicaSets can be used independently, today they're mainly used by Deployments as a mechanism to orchestrate Pod creation, deletion and updates. When you use Deployments you don’t have to worry about managing the ReplicaSets that they create. Deployments own and manage their ReplicaSets. As such, it is recommended to use Deployments when you want ReplicaSets.
2. Bare pods: Unlike the case where a user directly created Pods, a ReplicaSet replaces Pods that are deleted or terminated for any reason, such as in the case of node failure or disruptive node maintenance, such as a kernel upgrade. For this reason, we recommend that you use a ReplicaSet even if your application requires only a single Pod. Think of it similarly to a process supervisor, only it supervises multiple Pods across multiple nodes instead of individual processes on a single node. A ReplicaSet delegates local container restarts to some agent on the node (for example, Kubelet or Docker).
3. Replication Controller ReplicaSets are the successors to ReplicationControllers. The two serve the same purpose, and behave similarly. except the selectors
Thank you !
Related articles :