ReplicaSet

Difficulty: Beginner

Question

What is a ReplicaSet, how does it keep the right number of Pods running, and why do we usually use a Deployment instead?

Answer

A ReplicaSet is a Kubernetes controller that keeps a specified number of identical Pods running at all times. It finds its Pods through a label selector, creates new ones from its Pod template when there are too few, and deletes extras when there are too many. In practice you rarely create ReplicaSets directly; a Deployment creates and manages them for you so it can do rolling updates and rollbacks.

The three parts of a ReplicaSet

  • replicas: the desired count, for example 3.
  • selector: which Pods it owns, for example app: cart. Supports set-based expressions (In, NotIn, Exists), which was one improvement over the older ReplicationController.
  • template: the Pod spec used to create new Pods. Its labels must match the selector.

The reconciliation loop

The ReplicaSet controller in kube-controller-manager continuously compares desired and actual state:

  1. Count Pods that match the selector and are not being deleted.
  2. If fewer than replicas, create Pods from the template.
  3. If more, delete the extras (preferring Pods that are pending, not ready, or on nodes running more of its replicas).
  4. Repeat whenever a watched Pod or the ReplicaSet itself changes.

So if a node dies and takes a Pod with it, or someone runs kubectl delete pod, a replacement appears within seconds. Each Pod it creates has an ownerReferences entry pointing at the ReplicaSet, which is also how garbage collection removes the Pods when the ReplicaSet is deleted.

Why you use a Deployment instead

A ReplicaSet does not update existing Pods when you change its template. Only newly created Pods get the new spec. A Deployment solves this by creating a new ReplicaSet for each template change and shifting replicas from the old one to the new one gradually.

ReplicaSetDeployment
Keeps N Pods runningYesYes, through ReplicaSets
Rolling updatesNoYes
Rollback / revision historyNoYes, keeps old ReplicaSets
Pause and resume rolloutsNoYes
Typical direct useRareDefault for stateless apps

That is why kubectl get rs on a real cluster shows names like cart-5c9d7f6b8d: the suffix is the pod-template-hash label the Deployment adds so each ReplicaSet's Pods stay distinct.

Gotchas

  • Label overlap: a ReplicaSet will adopt any existing Pod that matches its selector and has no controller. Two controllers with overlapping selectors fight over Pods. Keep selectors specific.
  • Editing the ReplicaSet behind a Deployment: the Deployment will revert your changes. Always edit the Deployment.
  • Scaling with kubectl scale rs works, but on a Deployment-owned ReplicaSet it will be overridden.
Deployment, ReplicaSet and Pods. Each layer manages the one below it through owner references.
  1. Deployment: cart: Manages rollouts, keeps old ReplicaSets for rollback.
  2. ReplicaSet: cart-5c9d7f6b8d: Keeps exactly 3 Pods for the current template.
  3. Pods: cart-5c9d7f6b8d-xxxxx: Three identical Pods labelled app=cart.
  4. Containers on nodes: Started by the kubelet on each node.

Code examples

A standalone ReplicaSet

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: cart
spec:
  replicas: 3
  selector:
    matchLabels:
      app: cart
  template:
    metadata:
      labels:
        app: cart          # must match the selector
    spec:
      containers:
        - name: cart
          image: myorg/cart:1.0.0

The controller keeps three Pods labelled app=cart. Changing the image here would not update the existing Pods, which is why Deployments exist.

Watching self-healing

kubectl get rs
# NAME   DESIRED   CURRENT   READY
# cart   3         3         3

kubectl delete pod -l app=cart --wait=false
kubectl get pods -l app=cart -w   # new Pods appear immediately

kubectl get pod <pod-name> -o jsonpath='{.metadata.ownerReferences[0].kind}'
# ReplicaSet

Deleting Pods triggers the reconciliation loop to create replacements, and ownerReferences shows which controller owns each Pod.

Key points

  • ReplicaSet = replicas + selector + template; it keeps N matching Pods alive.
  • It reconciles continuously: deleted or crashed Pods are replaced automatically.
  • It does not roll out template changes to existing Pods.
  • Deployments create one ReplicaSet per revision, enabling rolling updates and rollback.
  • pod-template-hash keeps each revision's Pods separate.

Common follow-up questions

  • How does a Deployment use ReplicaSets during a rolling update?
  • What is the difference between a ReplicaSet and a ReplicationController?
  • What happens if two ReplicaSets have overlapping selectors?
  • How does Kubernetes garbage collect Pods when you delete a ReplicaSet?

Interview tips

  • Describe the reconciliation loop (desired vs actual state); it is the core Kubernetes idea.
  • Immediately explain why Deployments are preferred: ReplicaSets cannot roll out changes.
  • Mention the pod-template-hash suffix; it shows you have looked at a real cluster.

Concepts covered

ReplicaSet, Label Selector, Reconciliation Loop, Deployment, ownerReferences

More Kubernetes & Orchestration questions