DEV Community

Cover image for CKA & CKAD Series (Part 3): Replication controller & replicaset
Shahriyar Al Mustakim Mitul
Shahriyar Al Mustakim Mitul

Posted on • Edited on

CKA & CKAD Series (Part 3): Replication controller & replicaset

Assume that we had a single POD running our application.
Image description

What if for some reason, our application crashes and the POD fails? Users will no longer be able to access our application.

Image description

o prevent users from losing access to our application, we
would like to have more than one instance or POD running at the same time. That way if one fails we still have our application running on the other one. The replication
controller helps us run multiple instances of a single POD in the kubernetes cluster thus providing High Availability.
Image description

So does that mean you can’t use a replication controller if you plan to have a single POD? No! Even if you have a single POD, the replication controller can help by automatically bringing up a new POD when the existing one fails. Thus the replication controller ensures that the specified number of PODs are running at all times. Even if it’s just 1 or 100.

Image description
Image description
Image description

Image description

Image description

Load balancing

Another reason we need replication controller is to create multiple PODs to share the load across them. For example, in this simple scenario we have a single POD serving a set of users.

Image description
When the number of users increase we deploy additional POD to
balance the load across the two pods.
Image description
If the demand further increases and If we were to run out of resources on the first node, we could deploy additional PODs
across other nodes in the cluster.

Image description

As you can see, the replication controller spans across multiple nodes in the cluster. It helps us balance the load across multiple pods on different nodes as well as scale our application when the demand increases.

Note: Replication controller & replica set means almost the same.

Let's create a replication controller
Image description

we will use set apiversion as "v1" , kind will "ReplicationController" as we are creating that. We will name the replication controller as "myapp" and add some labels.Under the spec, we will add the pod template which will create pods if necessary.
Image description

We will use our pod-definition.yaml files code here in template so that, if needed the replication controller can create a pod.

Image description

Image description
We will also mention how many replicas we would need, for that, we will add replicas section there:
Image description
It has to be under spec section.

Lets give it the value 3
Image description
After that , we can create the replication controller
Image description

So, what will happen?
The replication controller will create 3 replicas .
Check out the pods it creates:
Image description

You may check the replication controller using this one
Image description

Done with Replica controller .

Let's create replica set now.
Image description

The apiVersion will be apps/v1
Image description

The kind will be Replicaset . Other than this, nothing changes that much.
Image description
Same as the replication controller.

Note: There is a slightly change here. Selector has to be added here.
Here under the selector, you need to mention which pods you want to be selected. They might be pods not created by the replicaset itself. They might be pods which were created before working with this project. We will add those pods, by using their labels in the selector of this replica set.

But for this blog, we will use the pods which the replica set itself creates. How?
By using the same label used for replica set in the selector.
Image description

SO, we are done!

Image description

You can create replicasets now
Image description
and then check the replicaset
Image description

as well as pods created by it.
Image description

*Scale *
If we want to increase the replicas to 6 , we can edit the yaml file and

Image description
Then apply those changed using this on
Image description
or, you can use scale command to scale it to 6 replicas
Image description
Image description
SO, that's the commands we basically used:
Image description

Let's now create the replica set using yaml:
Image description
So, in the 1st part
Image description
We have provided name for our pods and labels for it
Image description
Under the spec section, we have selector, which has the matchlabels tag to track pods with the label "env" with value "production"
We can set that to our pods we will create. How?
we can change the tag and it will now track the pods the replicaset itself creates.

Image description
Now it the selector will track the containers having label "app" with value "myapp"
Also, we mentioned the number of replicas & template to create a pod

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  labels: 
     app: myaapp
spec:
  selector:
    matchLabels:
      app: myaapp
  replicas: 3
  template:
      metadata:
        name: nginx-2
        labels:
          app: myapp
      spec:
        containers:
          - name: nginx
            image: nginx
Enter fullscreen mode Exit fullscreen mode

Lets go to the terminal and check if the file exists and what is inside of it
Image description
Let's create the replicaset

kubectl apply -f replicaset.yaml
Enter fullscreen mode Exit fullscreen mode

OHHO! We have a bug
Image description

The selector must have the same labels as template. SO, lets check.

Template has the labels "myapp"

Image description
But replicaset has the labels "myaapp" which is not same
Image description

Also the selector has the labels "myaapp"
Image description

Lets solve this bug
Image description

NOW apply the code

kubectl apply -f replicaset.yaml
Enter fullscreen mode Exit fullscreen mode

Image description
Now check the replicaset

kubectl get replicaset
Enter fullscreen mode Exit fullscreen mode

Image description
Lets check the pods

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Image description
You can see the pods name has a common portion "myapp-replicaset" and some other string , which means that, they are from a common replicaset and they vary with that string.

OKay, lets use the replicaset basics and delete one pod. Let's check if it replaces the pod and create another one to maintain the 3 replicas .
Lets delete one pod

You might have different pods . choose any from them.

kubectl delete pod myapp-replicaset-t4tg9
Enter fullscreen mode Exit fullscreen mode

We have deleted this one
Image description
Now check the pods

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Image description

Now, you can see that instead of 2 pods, we still have 3 pods but the pod name is new. That means that the replicaset has created one for us as one got deleted.

Image description
You can compare from here and see the AGE
Image description
It shows 2 were built 53minutes ago and one is built 2minutes back.

To see more informations, you can use the command

kubectl describe replicaset myapp-replicaset
Enter fullscreen mode Exit fullscreen mode

where myapp-replicaset is the replicaset name and there are pods named myapp-replicaset-w7vwj, myapp-replicaset-pr6vx, myapp-replicaset-vw4ln.

Now, lets create a pod which has the same labels "myapp". Lets see what this replicaset does .

Lets create a nginx.yaml file

apiVersion: v1
kind: Pod
metadata:
  name: nginx-2
  labels:
    app: myapp
spec:
  containers:
    - name: nginx
      image: nginx


Enter fullscreen mode Exit fullscreen mode

And go tot the terminal to create the pod

kubectl apply -f nginx.yaml
Enter fullscreen mode Exit fullscreen mode

Now we have tried to check the pods count
Image description

You can see that the recent created pod nginx-2 is terminating. Why?
Because replicaset knows it can have 3 pods (replicas) which we set in replicaset.yaml
Image description

with the label "app= myapp"
As it has already 3 replicas with this labels, it is terminating the new pod which had the labels myapp.

Remember, selector???
Image description
It had this label to match to keep balance of the replicas.

Image description

Update the replicaset

Assume , you need 4 replicas each time. now edit the file using

kubectl edit replicaset myapp-replicaset
Enter fullscreen mode Exit fullscreen mode

myapp-replicaset is the replicaset name .
After we press this command, we get this:
Image description

This is not the same file we created but it is the file kubernetes keeps in its memory and has given us access to change it. Our changes will directly impact .

Lets change the replicaset from here:
Image description
Image description
Lets save it and automatically this replicaset witll create 4 replicas (pods). [note; we already have 3. WE will have 1 more]
Now exit the vim editor and check for pods

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Image description
Automatically there will be 4 pods (replicas)

You can use the same way to scale down.

Again, we can use a command to easily set the replicas. Lets sclae down using that command. Till now we had 4 replicas, we will now have 2 replicas

kubectl scale replicaset myapp-replicaset --replicas=2

Enter fullscreen mode Exit fullscreen mode

Here myapp-replicaset was the name of replicaset

and check the pods
Image description
Look , we have just 2 replicas (pods)

Done for this lab!!!

Top comments (1)

Collapse
 
jhawithu profile image
Alok Kumar

Very nice collection of points for CKA preparation. I also created a dedicated course on Udemy for CKA exam and more than 1k+ people get benefited

My CKA Course