Open In App

Kubernetes – Creating a ReplicaSet

Last Updated : 19 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisite: Kubernetes

A ReplicaSet is a key component of a Kubernetes application. It is a controller that ensures that a specified number of pod replicas are running at any given time. It is used to automatically replace any pods that fail, get deleted, or are terminated, ensuring the desired number of replicas are always available to serve requests. When a ReplicaSet is created, it creates the desired number of replicas of the specified pod. It then continuously monitors the status of the replicas to ensure that the desired number is always maintained. If the number of replicas exceeds the desired number, the ReplicaSet will delete excess replicas.

  • Pods: A pod is the smallest deployable unit that can be created and managed. It is a logical host for one or more containers and all containers in a pod run on the same node in a cluster.
  • Cluster: A cluster is a group of physical or virtual machines that are used to host containerized applications. It consists of a group of worker machines, called nodes, that run the containers, and a control plane that manages the nodes and the applications running on them.

How a ReplicaSet works?

ReplicaSet is a next generation of replicationcontroller both replicaset and replicationcontroller ensure that pods that are running in the kubernetes cluster is matching the desired state of pods which are required. Following was the overview of how a replicaset operates:

  1. Mention the desired state: You can define the no.of pods required for the application while creating the replicaset in the yaml file. The no.of replicas depends upon the incoming traffic of the application.
  2. Scheduling pods: After writing the yaml file know to deploy the pods using that yaml file replicaset will start scheduling pods according to the number you have mentioned.
  3. Maintains the Desired: Replicaset will always try to maintain the desired no.of pods which is mentioned in the yaml file if any of the pod is deleted and the replicaset will automatically create a new pod.
  4. Monitoring the pods: Replicaset monitors the pod continuously and always makes sure that the pod desired count of the pod is maintained in the kubernetes cluster.

When to use a ReplicaSet?

Replicaset will always ensure that pods running in the kubernetes cluster are matching to the no.of replicas mentioned in the yaml file. Deployment is a higher-level concept that manages the replicaset by using the deployment we can perform more operations than using the replicaset for example we can perform rolling updates which will help us to update the application whenever there is a new code update

In Kubernetes, Deployment is the recommended way to deploy Pod or RS, simply because of the advance features it comes with.

Example

apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset
spec:
replicas: 2
selector:
matchLabels:
app: nginx-rs-pod
matchExpressions:
- key: env
operator: In
values:
- dev
template:
metadata:
labels:
app: nginx-rs-pod
env: dev
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80

Non-Template Pod Acquisitions

You need to make sure that the selectors which are mentioned in the replicaset don’t match other pod labels. ReplicaSet has the ability to acquire pods that are not created by itself as long as the selector doesn’t match other pod labels. This is known as non-template pod acquisitions.

Example

apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: 1st-replicaset
spec:
podSelector:
matchLabels:
app: Web-app
replicas: 5

Replicaset will acquire the pods which are having the same label as mentioned in the manifestfile. It will manage all the pods and monitor all the pods with the same labels.

Working with ReplicaSets

Step 1: Create a YAML file that defines the ReplicaSet. This file should include the number of replicas you want, the container image to use, and any other desired properties such as environment variables or resource limits.

To create the ReplicaSet, you can use the kubectl create command and pass it to the YAML file as an argument:

$ kubectl create -f replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: <RSName>
spec:
replicas: <noOfPODReplicas>
selector: # To Match POD Labels.
matchLabels: # Equality Based Selector
<key>: <value>
matchExpressions: # Set Based Selector
- key: <key>
operator: <in/not in>
values:
- <value1>
- <value2>
template:
metadata:
name: <PODName>
labels:
<key>: <value>
spec:
- containers:
- name: <nameOfTheContainer>
image: <imageName>
ports:
- containerPort: <containerPort>

Writing a ReplicaSet manifest

  • The apiVersion field specifies the version of the Kubernetes API that the object is using.
  • The kind field specifies the type of object that this file represents. In this case, it is a ReplicaSet.
  • The metadata field contains metadata about the ReplicaSet, such as its name.
  • The spec field contains the specification for the ReplicaSet. It includes the following fields:
    • replicas: the number of replicas of the pod that should be running at any given time
    • selector: a label query that determines which pods should be managed by the ReplicaSet
  • template: the pod template that will be used to create new pods when the ReplicaSet needs to scale up or down. The template field contains the following fields:
    • metadata: metadata for the pod
    • spec: the specification for the pod. The spec field for the pod includes a containers field, which specifies the containers that should be run in the pod. In this case, there is a single container named my-app that is based on the my-app: latest image and exposes port 80.

Step 2: Create a ReplicaSet using the configuration in replicaset.yaml

$ kubectl create -f 
gmemegen_deployment.yaml

replicaset created

Step 3: Verify that the ReplicaSet was created

$ kubectl get replicasets

verifying the replicaset

Step 4: View the ReplicaSet in more detail

$ kubectl describe replicaset my-replicaset

Description

Deleting a ReplicaSet and its Pods

Deleting a ReplicaSet

Replicaset can be deleted by using the following command:

kubectl delete rs <name of the replicaset>

Kubectl is a command line interface that will help you to connect to the kubernetes cluster and rs is the short name of replicaset.

Deleting The Pod

The pod can be deleted by using the following command.

kubectl delete pods –selector <key= pair>

You can delete the pod without deleting the replicaset by using the above command. If you want to update the pods then you need to delete the pod and then again redeploy it because the replicaset will not support the rolling updates of the pod.

Isolating Pods from a ReplicaSet

Pods can be isolated from the replicaset by changing the labels of the pods without matching the selectors of the replicaset. You can change the pod labels by performing the following steps:

Step 1: Select the pod which you need to be isolated. For that list all the pods

kubectl get pods

Step 2: Edit the pod labels which no longer match the replicaset selectors.

kubectl edit pod <Name of the Pod> 

Step 3: Apply the pod by using the following command.

Kubectl apply -f <name of the pod> 

Scaling a ReplicaSet

Scaling the replicaset can be done by using two methods.

  1. Manually by using the command.
  2. ReplicaSet as a Horizontal Pod Autoscaler Target.

Manually by using the command

You can scale the replicaset manually by changing the no.of replicas needed by using the following command.

kubectl scale rs <name of replicaset> --replicas=5

Kubectl is the command line interface tool used to communicate with the kubernetes cluster “scale rs” scale the replicaset and the name of replicaset and mentioned the no.of replicas is needed in the command the replicas will be scaled to 5 replicas.

ReplicaSet as a Horizontal Pod Autoscaler Target

Scaling of replicaset can also be done by using a resource called horizontal pod autoscaler. The pods will scale automatically when the threshold value of the pod CPU will reach the maximum value as mentioned in the manifest file it depends on our requirement based on the incoming traffic.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: mavenwebapprc
namespace: test-ns
spec:
replicas: 2
selector:
matchLabels:
app: mavenwebapp
template:
metadata:
labels:
app: mavenwebapp
spec:
containers:
- name: mavenwebapp
image: dockerhandson/maven-web-application:1
ports:
- containerPort: 8080
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 80

In the above yaml file, you can see the “targetaverageUtilization” which is 80 after the CPU utilization becomes 80 the HPA will automatically scale the pod replicas into the max replicas mentioned in the manifest file. In this case, the max replicas will be four.

Alternatives to ReplicaSet

There are four alternatives to the replicaset.

  1. ReplicationController
  2. DeamonSet
  3. Deployment
  4. Bare Pods

Difference Between ReplicaSet and ReplicationController

ReplicaSet

ReplicationController

ReplicaSet is the next-generation Replication Controller.

Replication Controller is one of the key features of Kubernetes, which is responsible for managing the pod lifecycle.

ReplicaSet will ensure that no.of pods running is matching the desired no. of pods in the Kubernetes cluster.

ReplicationController is responsible for making sure that the specified number of pod replicas are running at any point in time.

ReplicaSet supports the new set-based selector requirements as described in the labels user guide whereas a Replication Controller only supports equality-based selector requirements.

Replication Controllers and PODS are associated with labels.

Difference Between ReplicaSet and DaemonSet

ReplicaSet

DaemonSet

ReplicaSet will ensure that no.of pods running is matching the desired no. of pods in the Kubernetes cluster on any node.

DaemonSet will ensure that each node has at least one pod of the application which we deployed.

It is most suitable for applications like web applications which are stateless.

It is most suitable for the application which is stateful.

If the pod is deleted automatically replicaset will automatically create a new copy of that pod.

When a new node is added to the cluster daemonset make sure a copy of the pod is added to that node.

Difference Between ReplicaSets and Deployments

ReplicaSet

Deployments

ReplicaSet will ensure that the desired no.of pods are matching the specified no.of pods as mentioned in the yaml file.

Deployment is an advanced replication set that will manage the lifecycle of pods.

ReplicaSet is not suitable for applications that are going to have rolling updates and rollbacks.

Deployment supports the rolling update and rollbacks.

Internally replicaset will take care of pods and pods will take care of containers.

Internally deployment is going to take care of the replicaset and replicaset will take care of the pod.

FAQs On Kubernetes ReplicaSet

1. What is a ReplicaSet in Kubernetes?

ReplicaSet is the next-generation Replication Controller.

2. What are ReplicaSets used for?

ReplicaSet is used to deploy the pods. ReplicaSet supports the new set-based selector requirements as described in the labels user guide whereas a Replication Controller only supports equality-based selector requirements.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads