ReplicaSet

A ReplicaSet ensures a specified number of Pods are always running.

  • If a Pod dies → ReplicaSet spins up a new one.

  • If you scale the replica count → more Pods are created.

Uses labels to know which Pods it manages.

You usually don’t create ReplicaSets directly, they managed by ReplicaSet (which gives you extra feature).

Commands

To apply and run a Replicaset configuration:

kubectl apply -f replicaset.yaml

To list all the Replicasets, even the ones created by Deployments:

kubectl get replicasets

To describe a Replicaset:

kubectl describe replicaset <replicaset-name>

To delete a Replicaset:

kubectl delete replicaset <replicaset-name>

Example

replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-rs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  # Pod template
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp-container
          image: nginx:1.25
          ports:
            - containerPort: 80
          env:
            - name: NODE_ENV
              value: "production"

Last updated