Hello everyone. My name is Anish Rana and
you are watching Kubernetes web series.

Today, we’re going to cover one
interesting topic, i.e.  “What is
ReplicaSet?”

First, we will cover the theory part and
I cover below topics.

Ø What
is ReplicaSet?
    As I mentioned in my
previous topic
i.e ReplicationController,
where I had informed you that we have less options available with
ReplicationController.
Thus, more options are added in the ReplicaSet binary.
Ø
Understand the yaml file fields.
  ==> Again, there we need to understand more options in the yaml file.
ØHow
to create Yaml file for Replicaset ?
    :
we can create the Yaml file our own. How? That we will discuss on this topic.
Ø How
to create ReplicaSet from Yaml file?
Ø How
to check the ReplicaSet?
Ø How
to delete the ReplicaSet?
Ø When
to use a ReplicaSet?    In the last we
will do the
Ø LAB

What is replicaset? In previous pod video, we had covered that for pod and how to create it. Let’s suppose if we want to create three identical pods and Kubernetes API should manage and monitor these pods.

For this reason, Kubernetes offer replicaset functionality. A replicaset purpose is to maintain a stable set of replica pods running at any given time. As such, it is often used to grantee the availability of specified number of identical pods. In short, we could say that if we create a pod through replicaset and any pod goes down, then our replicaset will automatically create a new pod for us.

Same option we have with replication Controller. replicaset is also a part of controller like replication controller. I had already explained the functionality of controller.

If
you haven’t watched this video, then you may check “Kubernetes basic” video in my Youtube channel “anishrana2001”.

Now, let’s drill down our second topic. That is understand the field in yaml file.

 

 

 

We have four main parts i.e 

API version

kind, 

metadata

specs. 

 In the API version we mentioned which API version we are using for replicaset. We should use “apps/v1”. No need to remember this value. You may find it in the official documentation of Kubernetes. In kind section, we specify what resources we are going to create.

Here, we specified ReplicaSet, R & S are Capital words, please bear in mind. In the metadata, we generally add the name of the resources and some other useful information like labels and namespaces. In specs, we add all the information such as replicaSet, pod, Container. 

 

 

 

 

I download this yaml file from kubernetes.io website and I created the replicaset. With the help of this yaml file, we will create the replicaset and this replicaset will create pots. What is the name of this replicaset is frontend. While giving the name, we must adhere the dns sub domain status. contain no more than 253 characters. contain only lowercase alphanumeric characters, ‘-‘ or ‘.’
start with an alphanumeric character, and end with an alphanumeric character. If  you not follow this instruction, you will definitely observe an error message. 

Replica equal to 3 eight means that this replicaset will create 3 identical pods. 

 

 

 

In the kind section, we have mentioned ReplicaSet. This replicaset has its own label that is “tier : frontend”.

In this spec section, we have selector field. This will instruct the replicaset to look for labels which are defined under match labels. For the label is tier equal to frontend. In simple word, we can say that replicaset will select the pods with this label. Under template, we have metadata, and beneath this we have again labels.

This label is for pod. Replicaset will create the pod with this label. In the cluster, if any pod running with this label tier equal frontend, it’ll add in its list. So both value should be equal. Then only this pod will be selected by replicaset. So, here replicas first we need to create a replicaset and we instruct our replicaset to look for tier equal to frontend label and then create a pod and attach the label tier equal to frontend. So that when this pod will create, it’ll acquire these pods.

How to create the yaml file for replicaset. Understand the yaml file fields here so we have simple yaml file. Let’s explore.

First, we need to create a syntax for yaml file, “AKMS”. API version, kind, Metadata and specs. API version is “Apps/v1”, Kind is “ReplicaSet”. In the metadata part, we defined the name by which we can refer to this replicaset. It means giving a name to our replicaset. We also define a number of labels through which we can identify it.

In spec, we mentioned the identical pods that need to run under the control of this replicaset. Further, we instruct our replicaset to acquire the pod, which has the label tier equal to frontend. So here, we have the pod yaml file, so which is all the values except these.

 

So, the first section is for replicaset. The second section, we can further divide it to this section is for pod, and inside this section, we have container section. So, I hope now you understand the fields in yaml file and how we can create the replicaset yaml file by our own if we have the pod yaml file.

Below are the printscreen for your references.

So how to create replicaset from yaml file. First, I’m going to download the yaml file directly from kubernetes.io website. Here I use apply option. In order to create the replicaset, you may also use “create” option instead of that. No worries. We can execute the Command “kubectl get replicasets” or “rs” short form for replicasets.

Name of the replicaset is frontend, desired is 3.

This is what our controller manager will check. If you observe one pod less, then it’ll create it automatically. Current is 3. Ready is 3. Now, we can check the running pods by executing the command “kubectl get pods”. All the pods are running, and you can see that replicasets gave the name of pods by itself.

Naming convention is its own kubectl name followed by hyphen, and then five characters mixed with alphabet and numerics. Now we understand it, how to create replicaset?

Next topic is what happened when we delete one pod from RC? In the previous slide, we created one replicaset name frontend, and 3 pods are running.

We can also execute the command “kubectl get pod”. If one pod destroyed, then replicaset will auto create a new pod. Let me delete the last pod from the list. Command would be “kubectl delete pod, and then pod name”. From the message, it is confirmed that this pod is deleted again. If we execute this command “kubectl get pods”, and now a new pod is created by replicaset.

The new name is also given by the replicaset. Therefore, we can say that if one pod got deleted, it will get the new pod created by replicaset and the name. It would be a new name and new IP address is assigned to this pod probably.

Next topic is how to find more information of replicaset? How to check the replicaset.

Here we can execute the command “kubectl describe rs/frontend”. The first thing is the name. The second is label. The third one is replicas. How many identical pods this replicasets is created. The next would be pod templates “Labels: tier=frontend”. What image we have used for this pod. And here, what are the current running pods?

The commands are very straight forward, “kubectl delete replicasets and then replicaset name”. How to verify if our replicaset is deleted and it’s pods. So, we can execute the command “kubectl get rs”. RS is the short form of replicaset. We can also execute the command “kubectl get pods” to see if any pods are running, which are managed and controlled by this replicaset.

A ReplicaSet ensures that a specified number of pod replicas are running
at any given time. Updates to Pods / Upgrade to containers is not
possible.

This actually means that you may never need to
manipulate ReplicaSet objects. If you want to update the containers,
then it is recommended to use Deployment instead of ReplicaSet.
Nowadays, no one is using ReplicaSet. However, in order to understand
the deployment topic, it is worth to learn what is replicaset.

Besides
this, in the interview, people are generally asking this topic. So,
let’s jump into our lab section. That is very interesting and where we
will play with some commands. I have already uploaded the LAB questions
on my Github page.

Leave a Reply

Your email address will not be published. Required fields are marked *