A ReplicaSet is a Kubernetes controller whose primary purpose is to maintain a specified number of identical Pods called replicas. It acts as a self-healing mechanism, if a Pod fails, crashes, or is deleted, the ReplicaSet controller will immediately start a new one to bring the count back to the desired state. This ensures high availability and reliability for your applications.
Purpose
The core function of a ReplicaSet revolves around ensuring application stability and scalability.
- High Availability: By maintaining a minimum number of running Pods, a ReplicaSet ensures that your application can survive node failures or Pod crashes. If one instance goes down, others are still available to serve traffic, preventing downtime.
- Load Balancing: When combined with a Kubernetes Service, a ReplicaSet allows you to distribute network traffic across its set of identical Pods. As you scale the number of replicas up or down, the Service automatically adjusts, ensuring efficient resource utilization.
- Scalability: You can easily scale your application by simply changing the
replicasfield in the ReplicaSet definition. The controller will automatically create or terminate Pods to match the new desired count.
Working
A ReplicaSet continuously monitors the Pods that match its selector and compares the current number of Pods with the desired number of replicas.
The basic workflow is:

For example, if a ReplicaSet is configured with 3 replicas:
- If 3 Pods are running, no action is required.
- If 1 Pod fails, the ReplicaSet creates a new Pod.
- If the number of Pods increases beyond the desired count, the ReplicaSet removes the extra Pods.
- If the replica count is increased, the ReplicaSet creates additional Pods.
This allows the ReplicaSet to maintain the desired state of the application automatically.
Overcoming Past Limitations
ReplicaSets are the direct successor to the older Replication Controller. The single most important improvement they offer is more expressive and flexible selectors.
- Replication Controllers use equality-based selectors. This means they can only match Pods that have the exact same key-value pairs in their labels (e.g.,
app: frontend). This is quite restrictive. - ReplicaSets use set-based selectors. This allows for more complex selection criteria. You can match Pods based on whether a label key exists, or if a key's value is within a specific set of values.
For example, a ReplicaSet can manage Pods where the environment label is either production or qa (environment in (production, qa)). This selection capability makes ReplicaSets far more versatile for managing complex application deployments.
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:
replicas: 5
selector:
matchLabels:
app: Web-app
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.yamlapiVersion: apps/v1
kind: ReplicaSet
metadata:
name: <RSName>
spec:
replicas: <noOfPODReplicas>
selector:
matchLabels:
<key>: <value>
template:
metadata:
labels:
<key>: <value>
spec:
containers:
- name: <nameOfTheContainer>
image: <imageName>
ports:
- containerPort: <containerPort>
Writing a ReplicaSet manifest
- TheapiVersion 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:
The Pod initially has the matching label:
labels:app: nginx
Step 1: List the Pods
First, list the Pods managed by the ReplicaSet:
kubectl get pods --show-labelsIdentify the Pod that you want to isolate.
Step 2: Change the Pod Label
Change the label so that it no longer matches the ReplicaSet selector:
kubectl label pod <pod-name> app=isolated --overwriteFor example:
kubectl label pod nginx-replicaset-abc12 app=isolated --overwriteStep 3: Verify the Labels
Check the Pod labels again:
kubectl get pods --show-labelsThe isolated Pod will now have:
app=isolatedinstead of:
app=nginxStep 4: Check the ReplicaSet
Check the ReplicaSet and Pods:
kubectl get rskubectl get pods --show-labelsBecause the isolated Pod no longer matches the ReplicaSet selector, the ReplicaSet considers the number of matching Pods to be below the desired count and creates a replacement 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=5Kubectl 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: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nginx-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: ReplicaSet
name: nginx-replicaset
minReplicas: 2
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 80In the above yaml file, you can see the "averageUtilization" 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.
Labels and Selectors in ReplicaSet
ReplicaSets use labels and selectors to identify the Pods they manage.
A label is a key-value pair attached to a Kubernetes resource, while a selector defines which Pods should be managed by the ReplicaSet.
For example:
selector:
matchLabels:
app: nginxThe ReplicaSet will manage Pods that have the matching label:
labels:
app: nginxThe selector and Pod labels should match correctly. Otherwise, the ReplicaSet may not manage the expected Pods.
ReplicaSets support two types of selectors:
- matchLabels: Matches Pods using exact key-value pairs.
- matchExpressions: Uses set-based conditions such as
In,NotIn,Exists, andDoesNotExist.
For example:
selector:
matchExpressions:
- key: environment
operator: In
values:
- dev
- testThis selector matches Pods whose environment label is either dev or test.
Self-Healing with ReplicaSet
One of the main features of a ReplicaSet is its ability to maintain the desired number of Pods.
If a Pod managed by a ReplicaSet is deleted or fails, the ReplicaSet automatically creates a replacement Pod.
For example, if a ReplicaSet is configured with 3 replicas, check the Pods using:
kubectl get podsDelete one of the Pods:
kubectl delete pod <pod-name>Check the Pods again
The ReplicaSet will create a new Pod to maintain the desired replica count.
This self-healing behavior helps applications remain available even when individual Pods fail.
Limitations
Although ReplicaSet provides Pod replication and self-healing, it has some limitations when used directly for application deployments.
- ReplicaSet does not provide rolling updates.
- ReplicaSet does not provide built-in rollback functionality.
- ReplicaSet does not provide the deployment management features available with a Deployment.
- ReplicaSets are generally managed by Deployments for modern application deployments.
For example, a Deployment can create a new ReplicaSet when an application version changes and gradually replace the Pods managed by the older ReplicaSet.
Therefore, ReplicaSets are mainly responsible for maintaining the desired number of Pods, while Deployments provide higher-level application deployment and update management.
ReplicaSet Vs 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. |
ReplicaSet Vs Deployment
| Feature | ReplicaSet | Deployment |
|---|---|---|
| Maintains Pod replicas | Yes | Yes |
| Self-healing | Yes | Yes |
| Scaling | Yes | Yes |
| Rolling updates | No | Yes |
| Rollbacks | No | Yes |
| Manages ReplicaSets | No | Yes |
| Recommended for application deployments | Less common | Yes |
ReplicaSet Vs 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 commonly used to maintain multiple replicas of an application, especially workloads that require a specific number of running Pods. | It is commonly used for node-level workloads such as logging agents, monitoring agents, and security agents that need to run on each eligible node. |
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. |