A ReplicaSet (RS) in Kubernetes ensures that a specified number of identical Pods are running at any given time.
It’s one of the key controllers that manages Pod replication and availability.
⚙️ Definition
A ReplicaSet is a Kubernetes controller that maintains a stable set of running Pods — it automatically adds or removes Pods to match the desired count.
🧩 Key Functions
- Ensures high availability — keeps a fixed number of replicas (Pods) running.
- If a Pod crashes or a node fails → the ReplicaSet creates a new Pod automatically.
- If there are too many Pods → it terminates extras to maintain the desired number.
🧱 Core Components
A ReplicaSet has three main parts:
- Selector — identifies which Pods it manages (via labels).
- Replicas — the desired number of Pod copies.
- Pod Template — describes how to create new Pods.
🧾 Example YAML
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myapp-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: nginx-container
image: nginx:latest
ports:
- containerPort: 80
Explanation:
replicas: 3
→ ensures 3 Pods are running.selector.matchLabels
→ ties the ReplicaSet to Pods with labelapp: myapp
.template
→ defines the Pod configuration.
🔄 How It Works
- You apply the ReplicaSet manifest.
- The ReplicaSet controller checks how many Pods match its selector.
- If:
- Fewer Pods exist → it creates new Pods.
- More Pods exist → it deletes extras.
- It continuously monitors and self-heals the desired state.
🧠 Important Notes
- You rarely create ReplicaSets directly — instead, you use a Deployment, which manages ReplicaSets for rolling updates and rollbacks.
- ReplicaSet is the modern replacement for the older ReplicationController.
- If you delete a ReplicaSet, its Pods are deleted too (unless you set
orphan
deletion policy).
📊 Use Cases
- Ensuring constant availability of stateless Pods.
- Maintaining identical copies of an application across nodes.
- Forming the base for Deployments, which handle versioning and updates.