ReplicaSets
Keeping N identical pods alive with a selector.
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/v1kind: ReplicaSetmetadata: { name: web }spec:replicas: 3selector:matchLabels: { app: web } # the pods this RS ownstemplate:metadata:labels: { app: web } # MUST match the selectorspec: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.