CKA & CKAD Series (Part 3): Replication controller & replicaset
Tags: replicaset, kubernetes
Published: 2022-07-30 07:52:00

Assume that we had a single POD running our application. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ljj7bpx7lxzz5d0re41.png)
 

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ho0z6v4yjo4ri1qi9a81.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/urwcgceg2tz6kne4lm8s.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azxjm27r8h56c4c081nr.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0trkeqn6s89a62aekpb.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuxbchjovnckb0n1yw3t.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p401lc5bbfgn6k2niwas.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnbwtebvnv7x0uf4oxso.png)


#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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mmr2tznus1koybuyzvp9.png)
When the number of users increase we deploy additional POD to
balance the load across the two pods.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nvqexv46qq587nwmq9xc.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v8j0kmoxjh882r2y29c3.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/635gb2dgnj57h02kmm8c.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kevst9eptcv0ckr3qatp.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w78ybhm1kwmef7w1y0k1.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a3xjbea2z14yni34pfhe.png)
We will also mention how many replicas  we would need, for that, we will add replicas section there:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5p6ic49s06y4mh5xtq93.png)
It has to be under spec section.

Lets give it the value 3
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/irdi5mnlgjbrictwq52c.png)
After that , we can create the replication controller 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eh6ts37tc7a59ahg1xgw.png)

      
So, what will happen?
The replication controller will create 3 replicas .
Check out the pods it creates:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qdx2ce6b5u234on9qcxn.png)
 
You may check the replication controller using this one
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8by0q4ra08xf9ij9p10y.png)
 
 
Done with Replica controller .


Let's create replica set now.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/62vkcpv978copy75ky7s.png)

The apiVersion will be apps/v1
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8u4hx0d4dilvp6exe7oy.png)
 
The kind will be Replicaset . Other than this, nothing changes that much.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ybdantebuo1vx34l47wp.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6dsk6gyvhafhz6kf3p8d.png)

SO, we are done!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mddcpec47poc5i4aacmn.png)
  
You can create replicasets now 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/li5kx49f54nhbgxi8m8n.png)
and then check the replicaset 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vdbm3lr3oiaxkcv8255y.png)
 
as well as pods created by it.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hg969rlt9axjgqmhbxtj.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oeqpqpcfeaenkg3vkzxe.png)
Then apply those changed using this on
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jacrrg3sx9jjwvqub41l.png)
or, you can use scale command to scale it to 6 replicas
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xrioj507sgchk6yr0uvi.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51cit1whddygfcm2hvjg.png)
SO, that's the commands we basically used:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q77qz0i1hde7sv3nl59z.png)

Let's now create the replica set using yaml:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4lxbff0zrhmnhtmilbq5.png)
So, in the 1st part
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gj0cun9du04a31bp5nun.png)
We have provided name for our pods and labels for it
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rglj53ni932t40xw8od5.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2foo41lvccs659rd6rx5.png)
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
```
     
Lets go to the terminal and check if the file exists and what is inside of it
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bha5a8mllmydwqpo19yt.png)
 Let's create the replicaset

```
kubectl apply -f replicaset.yaml
```

OHHO! We have a bug
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fzd0gdb03pa7sqr1pfj0.png)

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

Template has the labels "myapp"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f6rpyt6wu09kuxutce9x.png)
But replicaset has the labels "myaapp" which is not same
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zlam78o8ha85s7iqa9gh.png)

Also the selector has the labels "myaapp"
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r83lffpgiayz4b7fo82t.png)
 
Lets solve this bug
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xvlwytdgeu9gtvvgjcf3.png)

NOW apply the code

```
kubectl apply -f replicaset.yaml
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oh7g1pbtnjqxa19prn1a.png)
Now check the replicaset

```
kubectl get replicaset
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ov2m7chrpoxbm5kukg9h.png)
Lets check the pods

```
kubectl get pods
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ouink17bz58epnl8r92a.png)
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
```

 We have deleted this one
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02fpyin7y0f46i5h2f0h.png)
Now check the pods 

```
kubectl get pods
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxx9bj4lot86gmrm4nt8.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fgn4otwk58l7au5mc4s3.png)
You can compare from here and see the AGE
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/79zfuvx9tab1i2polgsx.png)
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
```
 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
  
  
```
And go tot the terminal to create the pod

```
kubectl apply -f nginx.yaml
```
Now we have tried to check the pods count
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0bniafuhkkibxkp71ncq.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uixl8pmrk06n3zlxzain.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pyjsy35asfdog46xsr4r.png)
It had this label to match to keep balance of the replicas.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zzihr7tyqi12tcfzcxfk.png)
  
 
**Update the replicaset**

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

```
kubectl edit replicaset myapp-replicaset
```
myapp-replicaset is the replicaset name .
After we press this command, we get this:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jt3bwyleawzhp4dllpl1.png)
 
 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/94qust8ulcwy17nx1pm7.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/65n2npe75a1uxnsob2z6.png)
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
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpdq3muevdy42xz0qg9s.png)
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

```

Here myapp-replicaset was the name of replicaset

and check the pods
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjg8wouyone9wvc457q3.png)
Look , we have just 2 replicas (pods)


Done for this lab!!!
<img src="https://media0.giphy.com/media/Nfs5y1kzJKfu/giphy.gif?cid=ecf05e472d3w835ptrri5m1p6ifnwl9mi7ope9pifvhpcxf6&rid=giphy.gif&ct=g">