Kubernetes – Creating a ReplicaSet
Last Updated :
19 Jul, 2023
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:
- 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.
- 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.
- 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.
- 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

Step 3: Verify that the ReplicaSet was created
$ kubectl get replicasets
.png)
Step 4: View the ReplicaSet in more detail
$ kubectl describe replicaset my-replicaset
.png)
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.
- Manually by using the command.
- 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.
- ReplicationController
- DeamonSet
- Deployment
- Bare Pods
Difference Between ReplicaSet and 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 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 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.
|
Similar Reads
kubernetes Replicaset VS Deployment
In Kubernetes, both Deployment and ReplicaSet are used to manage the lifecycle of pods. In order to Manage the life cycle of applications in a Kubernetes cluster, it relies on both ReplicaSets and Deployments, which are two important components. The emergence of Kubernetes has set it as a dominant p
6 min read
Kubernetes - Creating an App
Pre-requisite: Kubernetes In this article, we will discuss how to create a simple application on Kubernetes. Kubernetes is an open-source container orchestration system that helps to manage, deploy and scale containerized applications. Kubernetes provides a platform for automating the deployment, sc
2 min read
Kubernetes - Replication Controller
With the help of the open-source container orchestration technology Kubernetes, software deployment, scalability, and management are mostly automated. Another name for Kubernetes is K8s. Google created Kubernetes, which is now overseen by the Cloud Native Computing Foundation. Even though it now wor
7 min read
Scaling Applications With Kubernetes ReplicaSets
Kubernetes is an orchestration tool used to scale , deploy and manage the containerized application. It provides ReplicaSet which is used to run a desired number of pods on any given time . In this guide, I will first discuss what Kubernetes is. Then I will discuss about ReplicaSets. After this I wi
5 min read
What is Azure Kubernetes Service?
Kubernetes is an open-source platform for automating the deployment, scaling, and management of containerized applications. It has become the de facto standard for managing containers in production and is used by organizations of all sizes to improve the speed and reliability of their application de
6 min read
How to Careate Static Pod in Kubernetes ?
Static pods in Kubernetes make it simple to run pods directly on individual cluster nodes without the Kubernetes API server being involved. Not at all like ordinary pods oversaw by the Kubernetes control plane, static pods are characterized and overseen straight by the Kubelet, the essential node ag
7 min read
Kubernetes - Load Balancing Service
Before learning Kubernetes (or K8S in Short), you should have some knowledge of Docker and Containers. Docker is a tool that helps the developer create containers in which applications can run in an isolated environment. Containers are just an abstraction for the applications inside. Docker also pro
12 min read
What are Kubernetes Containers?
Kubernetes is an open-source container orchestration framework that was originally developed by Google. Container orchestration is automation. It can facilitate you to deploy the identical application across different environments like physical machines, virtual machines cloud environments, or perha
15 min read
Deploying a React Application in Kubernetes
Kubernetes is an open-source free manager for your computer program. These programs can be in the containers. Each container holds a program and everything it needs to run. To Keep track of all these containers that contain your application this is where the Kubernetes role comes in. Kubernetes does
5 min read
Kubernetes - Creating Multiple Container in a Pod
Pre-requisite:- Kubernetes Kubernetes is a container management tool and it automates container deployment, load balancing, and container scaling. It is open-source and developed by Google in 2014 and written in Golang. All cloud providers adopt Kubernetes. It is scheduled runs and manages isolated
3 min read