What is a ReplicaSet and how does it relate to a Deployment?
A ReplicaSet keeps a specified number of identical Pods running, replacing any that die. A Deployment manages ReplicaSets, creating a new one for each version to enable rolling updates and rollbacks.
Explain it simply
A ReplicaSet is a supervisor who says 'there must always be 3 conductors on this route'. If one falls sick, he sends another. The Deployment is the manager above him who plans changes, such as moving to new uniforms gradually.
The ReplicaSet uses a label selector to count matching Pods. Too few, it creates more from its Pod template; too many, it deletes extras. It does not know about versions or rollouts. A Deployment adds that: when you change the image, it creates a new ReplicaSet, scales it up while scaling the old one down, and keeps old ReplicaSets (scaled to zero) so it can roll back.
In practice you create Deployments and never touch ReplicaSets, though you will see them in kubectl get rs, with names like web-7d4b9c6f5.
Example
kubectl create deployment web --image=nginx:1.27 --replicas=3
kubectl get deploy,rs,pods
# deployment/web -> replicaset/web-7d4b9c6f5 -> 3 podsWhat to say in the interview
"A ReplicaSet maintains a set number of identical Pods through a label selector. A Deployment owns ReplicaSets, one per version, and adds rolling updates and rollbacks, so I create Deployments rather than ReplicaSets."
Common mistakes
- Editing Pods owned by a ReplicaSet; they are recreated from the template.