Kubernetes ReplicaSets Keeping Your Pods Running

A ReplicaSet ensures that a specific number of identical Pod copies always run in your cluster. If one Pod crashes, the ReplicaSet creates a replacement. If you accidentally delete a Pod, it starts a new one. It is the engine behind Kubernetes self-healing.

The Problem ReplicaSets Solve

Imagine you run a delivery company and you always need exactly 3 drivers on the road. If a driver calls in sick, you immediately call a replacement. You do not accept the situation of having only 2 drivers. A ReplicaSet does the same thing for your Pods — it enforces a headcount.

Desired: 3 Pods running
Actual:  2 Pods running (one crashed)
         ↓
ReplicaSet sees the gap
         ↓
Creates 1 new Pod to restore count to 3

How a ReplicaSet Tracks Pods

A ReplicaSet does not track Pods by name — it tracks them by labels. You define a label selector in the ReplicaSet spec. Any Pod in the cluster that matches that selector belongs to the ReplicaSet's count. If a matching Pod disappears, the ReplicaSet creates a new one. If too many matching Pods exist, it deletes the extras.

ReplicaSet selector: app=frontend

┌──────────┐  ┌──────────┐  ┌──────────┐
│   Pod    │  │   Pod    │  │   Pod    │
│ app=front│  │ app=front│  │ app=front│
└──────────┘  └──────────┘  └──────────┘
  ← All 3 match the selector → Count = 3 ✓

Writing a ReplicaSet YAML

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend-rs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: frontend
  template:
    metadata:
      labels:
        app: frontend
    spec:
      containers:
      - name: web
        image: nginx:1.25
        ports:
        - containerPort: 80

The YAML has three key sections inside spec:

  • replicas — how many Pods you want
  • selector — which label to watch
  • template — the Pod blueprint to create when a new one is needed

Applying and Inspecting the ReplicaSet

kubectl apply -f frontend-rs.yaml
kubectl get replicasets
kubectl get pods -l app=frontend

Output:

NAME          DESIRED   CURRENT   READY   AGE
frontend-rs   3         3         3       1m

Three columns tell the story: DESIRED is your target, CURRENT is how many Pods exist, and READY is how many pass their readiness check.

Scaling a ReplicaSet

You scale a ReplicaSet by changing the replicas value. Two ways to do it:

Method 1: Edit the YAML and Re-apply

# Change replicas: 3 to replicas: 6 in your YAML file
kubectl apply -f frontend-rs.yaml

Method 2: Use the Scale Command

kubectl scale replicaset frontend-rs --replicas=6

Kubernetes starts 3 new Pods immediately. Scale back down:

kubectl scale replicaset frontend-rs --replicas=2

Kubernetes picks 4 Pods to terminate and removes them gracefully.

Testing Self-Healing

Delete one Pod manually and watch the ReplicaSet react:

kubectl delete pod frontend-rs-abc12
kubectl get pods -l app=frontend --watch

Within seconds, the ReplicaSet creates a replacement. The Pod count returns to 3. This is Kubernetes self-healing in action.

Labels and Adoption: An Important Warning

Because ReplicaSets track by labels, any Pod with matching labels — even one you created manually — gets counted. If you already have 3 matching Pods running and create a ReplicaSet asking for 3 replicas, Kubernetes may terminate your manually-created Pod if the total count exceeds 3. Always use labels carefully and consistently.

Manual Pod with label app=frontend = 1
ReplicaSet wants 3 with label app=frontend
Total = 4 → ReplicaSet terminates 1 Pod

ReplicaSets vs. Pods: When to Use Each

SituationUse
Run a one-off task or debug quicklyBare Pod (temporary use only)
Keep N copies of a service runningReplicaSet
Deploy with rolling updates and rollbackDeployment (wraps a ReplicaSet)

Why You Rarely Write ReplicaSets Directly

In practice, you almost never write a ReplicaSet YAML yourself. A Deployment (covered in the next topic) creates and manages a ReplicaSet for you. The Deployment adds rolling update and rollback capabilities on top. Understanding ReplicaSets is important, but Deployments are what you use day to day.

Deployment
   └── creates and manages ReplicaSet
              └── creates and manages Pods

Key Points

  • A ReplicaSet ensures a defined number of identical Pods always run.
  • It tracks Pods by label selector, not by Pod name.
  • When a Pod crashes or is deleted, the ReplicaSet creates a replacement within seconds.
  • You scale a ReplicaSet by changing the replicas count — up or down.
  • In real projects, use Deployments instead of bare ReplicaSets — they add rolling updates and rollback support.

Leave a Comment