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
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
Lets apply,
~ kubectl apply -f ReplicaSet.yaml
replicaset.apps/replica-set created
View ReplicaSet
Similar to namespace and pods, we can get all the replicaset with command:
~ kubectl get replicaset
or
~ kubectl get rs
Output
NAME DESIRED CURRENT READY AGE
replica-set 3 3 3 4m31s
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
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
How Pods are associated with ReplicaSet and How ReplicaSet manage that Pods ?
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
.
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.
~ kubectl apply -f ReplicaSet.yaml
replicaset.apps/replica-set configured
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
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
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
Edit ReplicaSet
We can edit replicaset with command:
~ kubectl edit replicaset <replicaset-name>
Delete ReplicaSet
We can delete replicaset with command:
~ kubectl delete replicaset <replicaset-name>
We have successfully managed, scaled our application using replicaset.
In next, we will be discussing on deployment object.
Top comments (1)
Nicely put and clearly explained That diagram is quite attractive.