Goglides Dev 🌱

Goglides Dev 🌱 is a community of amazing users

We are working on this space so that IT professionals can grow together.

Create account Log in
Cover image for ReplicaSet, how it manages Pods in k8s cluster.
Niraj Pradhan
Niraj Pradhan

Posted on

ReplicaSet, how it manages Pods in k8s cluster.

In the previous post we have discussed about namespace, how namespace is used to manage resources in kubernetes cluster.
In this post, we are going to discuss about replicaset.

ReplicaSet

ReplicaSet is a process that run multiple instances of Pods. It constantly monitors the status of Pods and if any one fails or terminates then it restores by creating new instance of Pod and by deleting old one.

Creating ReplicaSet using manifest file (yaml)

~ vim ReplicaSet.yaml
Enter fullscreen mode Exit fullscreen mode
apiVersion: apps/v1
kind: ReplicaSet  
metadata:
  name: replica-set
spec:
  replicas: 3
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
    spec:
      containers:
      - name: app-container
        image: nginx
Enter fullscreen mode Exit fullscreen mode

Lets apply,

~ kubectl apply -f ReplicaSet.yaml

  replicaset.apps/replica-set created
Enter fullscreen mode Exit fullscreen mode

View ReplicaSet

Similar to namespace and pods, we can get all the replicaset with command:

~ kubectl get replicaset
Enter fullscreen mode Exit fullscreen mode

or

~ kubectl get rs
Enter fullscreen mode Exit fullscreen mode

Output

NAME          DESIRED   CURRENT   READY   AGE

replica-set   3         3         3       4m31s
Enter fullscreen mode Exit fullscreen mode

In above manifest file, we have mentioned replicas:3 so that it runs 3 instances of Pods with label app:backend.
Lets see,

~ kubectl get pods --show-labels
Enter fullscreen mode Exit fullscreen mode

Output

NAME              READY STATUS   RESTARTS AGE    LABELS

replica-set-85d7k 1/1   Running  0        8m22s  app=backend
replica-set-nm4dr 1/1   Running  0        8m22s  app=backend
replica-set-q2thz 1/1   Running  0        8m22s  app=backend
Enter fullscreen mode Exit fullscreen mode

How Pods are associated with ReplicaSet and How ReplicaSet manage that Pods ?

Image description

As we have seen in above manifest file, We have Pods with label app:backend and in ReplicaSet there is option matchLabels with app:backend. It means that pods are associated with replicaset with the help of one of kubernetes feature known as label. The pods having label app:bakend are only associated with the replicaset replica-set that we have created using manifest file. As per our requirement, we can easily scale, upgrade pods with the help of replicaset.

Image description

Scaling application

An application can be scaled up and down depending on the situations in two ways.

Method 1: By updating value of replicas in the configuration file and apply the changes.

Lets update the value of replicas to 5 in configuraton file.

Image description
Apply above changes,

~ kubectl apply -f ReplicaSet.yaml

  replicaset.apps/replica-set configured
Enter fullscreen mode Exit fullscreen mode

Lets check the pods, we will see

NAME              READY STATUS   RESTARTS  AGE   LABELS

replica-set-85d7k 1/1   Running  0         115m  app=backend
replica-set-nm4dr 1/1   Running  0         115m  app=backend
replica-set-q2thz 1/1   Running  0         115m  app=backend
replica-set-stdmp 1/1   Running  0         13s   app=backend
replica-set-t9zmt 1/1   Running  0         13s   app=backend
Enter fullscreen mode Exit fullscreen mode

Similarly we can scale down by assigning the lower value to the replicas in the configuration file.

Method 2: By using CLI

We can scale up or down our application by using kubectl command. Lets scale down the application with CLI kubectl scale --replicas=2 replicaset <replicaset-name>.

~ kubectl scale --replicas=2 replicaset replica-set
Enter fullscreen mode Exit fullscreen mode

Lets check the pods again,

NAME              READY STATUS   RESTARTS  AGE   LABELS

replica-set-85d7k 1/1   Running  0         121m  app=backend
replica-set-nm4dr 1/1   Running  0         121m  app=backend
Enter fullscreen mode Exit fullscreen mode

Edit ReplicaSet

We can edit replicaset with command:

~ kubectl edit replicaset <replicaset-name>
Enter fullscreen mode Exit fullscreen mode

Delete ReplicaSet

We can delete replicaset with command:

~ kubectl delete replicaset <replicaset-name>
Enter fullscreen mode Exit fullscreen mode

We have successfully managed, scaled our application using replicaset.
In next, we will be discussing on deployment object.

Discussion (1)

Collapse
bkpandey profile image
Balkrishna Pandey

Nicely put and clearly explained That diagram is quite attractive.