Skip to main content

What is a ReplicaSet?

Replication Controller and ReplicaSet serve the same purpose, but ReplicaSet is the next generation of Replication Controller.
A ReplicaSet ensures a specified number of pod replicas are running at all times. It maintains the desired state of the application by creating or deleting pods as needed.
User → Service → ReplicaSet → Pod 1
                             → Pod 2
Key features:

Self-healing

If a pod fails, a new one is automatically created to replace it.

Scaling

Scale up or down the number of pods as needed.

Load Balancing

Distribute traffic across all managed pods.

Replication Controller

rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: rc
spec:
  replicas: 3
  template:
    metadata:
      name: pod-name
    spec:
      containers:
        - name: container-name
          image: web-server-image
kubectl get replicationcontroller

ReplicaSet

When you edit a ReplicaSet, you will need to delete the previously deployed pods first, as it won’t auto-update existing pods.
The key difference between Replication Controller and ReplicaSet is that ReplicaSet requires a selector definition. The selector identifies the pods that the ReplicaSet will manage using label selectors. ReplicaSet can also manage pods that were not created as part of the ReplicaSet creation.
replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replicaset-name
spec:
  replicas: 3
  selector:
    matchLabels:       # must match the pod labels in the template section
      app: web-server
  template:
    metadata:
      name: pod-name
      labels:
        app: web-server
    spec:
      containers:
        - name: container-name
          image: web-server-image
kubectl get replicaset
kubectl scale --replicas=5 -f <file-name>
kubectl scale --replicas=5 replicaset <replicaset-name>
kubectl delete replicaset <replicaset-name>

Build docs developers (and LLMs) love