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 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.
~ 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
~ kubectl apply -f ReplicaSet.yaml replicaset.apps/replica-set created
Similar to namespace and pods, we can get all the replicaset with command:
~ kubectl get replicaset
~ kubectl get rs
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
~ kubectl get pods --show-labels
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
As we have seen in above manifest file, We have Pods with label
app:backend and in ReplicaSet there is option
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
An application can be scaled up and down depending on the situations in two ways.
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.
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
We can edit replicaset with command:
~ kubectl edit replicaset <replicaset-name>
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.