ReplicaSets
A ReplicaSet keeps a specified number of identical Pod replicas running at all times.
A single Pod has no safety net — if it dies, it stays dead. A ReplicaSet fixes that by ensuring a fixed number of identical Pods are always running.
How it works
You set replicas: 3 and a selector that matches Pod labels. The ReplicaSet counts matching Pods and:
- Creates new Pods if there are too few.
- Deletes extras if there are too many.
It uses a template — a built-in Pod spec — to stamp out new Pods.
You usually don't create these directly
In practice you almost never write a ReplicaSet by hand. You create a Deployment, which manages ReplicaSets for you and adds rolling updates on top.
Example
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: web-rs
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.27When to use it
- A platform team configures a ReplicaSet to maintain three replicas of a stateless API so that if one pod crashes the service remains available.
- An operator deletes a pod manually to test self-healing and confirms the ReplicaSet immediately schedules a replacement.
- A developer inspects a ReplicaSet's selector to understand which pods it owns before modifying pod labels.
More examples
ReplicaSet manifest
Defines a ReplicaSet that keeps three pods with the label app=api running at all times.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: api-rs
spec:
replicas: 3
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: myapi:1.0Scale and inspect a ReplicaSet
Lists the ReplicaSet, scales it to 5 replicas, and verifies the desired/current/ready replica counts.
kubectl get rs api-rs
kubectl scale rs api-rs --replicas=5
kubectl describe rs api-rs | grep -E 'Replicas|Pods'Self-healing demonstration
Deletes a pod and watches the ReplicaSet controller immediately create a replacement to restore the desired count.
# Delete one pod
kubectl delete pod api-rs-xkp2t
# ReplicaSet recreates it immediately
kubectl get pods -l app=api -w
# Shows new pod entering Running state
Discussion