CoursesKubernetes administrationArchitecture & Core Objects

ReplicaSets

Keeping N identical pods alive with a selector.

Beginner10 min · lesson 8 of 65
In plain terms
A ReplicaSet is a manager told “always keep three cashiers on the floor.” Someone clocks out, they call in a replacement; too many showed up, they send one home. The number is the whole job.

A ReplicaSet has one job: keep exactly N pods matching a label selector running. Too few and it creates more; too many and it deletes some. It is the mechanism behind scaling and self-healing — though you almost never create one by hand.

replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata: { name: web }
spec:
replicas: 3
selector:
matchLabels: { app: web } # the pods this RS owns
template:
metadata:
labels: { app: web } # MUST match the selector
spec:
containers: [{ name: app, image: nginx:1.25 }]

The selector is the contract: a ReplicaSet owns every pod matching it, even ones it did not create. The template labels must satisfy the selector: if they do not, the API server rejects the ReplicaSet at apply time with `selector` does not match template `labels` — apps/v1 validates this on every create and update, so the object is never created rather than running away. Always make the template labels a superset of the selector.

Manage Deployments, not ReplicaSets
Deployments create and version ReplicaSets for you. Editing a ReplicaSet directly fights the controller above it, which will just revert your change.