Deployment, Replication Controller & ReplicaSet
Video: Day 8/40 — Kubernetes Deployment, Replication Controller and ReplicaSet • https://www.youtube.com/watch?v=oe2zjRb51F0 • Duration: ~35 min
Key terms
| Term | Meaning |
|---|---|
| Deployment | Manages ReplicaSets for rolling updates and rollback |
| ReplicaSet (RS) | Keeps N pod replicas running |
| ReplicationController (RC) | Legacy predecessor of the ReplicaSet |
| Replica | One copy of a pod |
| Rolling update | Gradual pod replacement with no downtime |
| Rollback | Revert to a previous revision |
| Selector | Label query matching the managed pods |
Problem & solution
A bare pod has no self-healing and no scaling: if it dies, it's gone, and there is no safe way to roll out a new version. We need controllers that keep a desired number of pod copies alive and manage rollouts.
Solution: Use a Deployment to manage a ReplicaSet that keeps N pod replicas running and enables rolling updates and rollbacks.
The analogy
A shipping line files a standing order with the port: always keep three identical ships docked at this berth, no matter what. A dock supervisor walks the pier, and the moment one ship sinks or sails off he tugs in an identical replacement so the count is always three. A Kubernetes Deployment with its ReplicaSet is that standing order, and the ReplicaSet controller is the supervisor that keeps N identical pods running and recreates any that die.
Where this fits in the cluster
The same cluster entities appear in every day's notes; the diagram below shows where this day's topic fits.
Graph legend — each node maps to the web nginx Deployment this day creates:
| Graph node | Maps to | What it does |
|---|---|---|
| api-server / etcd / scheduler | the control-plane components | Accept, store, and schedule the Deployment's pods |
| controller-mgr - Deployment to ReplicaSet | kube-controller-manager | Keeps replicas: 3 of web alive |
| Pod - one of 3 identical web replicas | a pod from the web ReplicaSet | One copy of the app |
| container nginx:1.27 | template.spec.containers[0].image | The nginx web server process |
Why not just run bare Pods?
A lone pod has no self-healing and no scaling. If it dies, it's gone. Controllers keep a desired number of pod copies alive.
The hierarchy (ASCII)
Controllers stack on top of each other — a Deployment owns ReplicaSets, which own the Pods.
Graph legend — each node maps to the layers of the web Deployment:
| Graph node | Maps to | What it does |
|---|---|---|
| Deployment web | kind: Deployment, metadata.name: web | Manages ReplicaSets, adds rollouts and rollbacks |
| ReplicaSet web-xxxx | the ReplicaSet the Deployment creates | Keeps replicas: 3 nginx pods running |
| Pod web nginx:1.27 | one managed pod | A single nginx replica from the template |
- Replication Controller (RC): the OLD way to keep N pods (legacy).
- ReplicaSet (RS): the newer RC, supports set-based label selectors.
- Deployment: manages ReplicaSets; adds rolling updates & rollbacks. -> Use Deployments in practice.
Self-healing & scaling
The ReplicaSet constantly compares actual vs desired pod count and recreates any that die.
Graph legend — each node maps to the self-healing of the web ReplicaSet:
| Graph node | Maps to | What it does |
|---|---|---|
| desired replicas 3 | spec.replicas: 3 | The target count of nginx pods |
| web nginx pod dies | a crashed/deleted pod | Drops actual count to 2 |
| ReplicaSet notices actual 2 != desired 3 | the ReplicaSet controller | Detects the gap from desired state |
| NEW web nginx pod | the recreated pod | Restores the count back to 3 |
Deployment YAML
A Deployment manifest wraps a Pod template plus a replica count and a label selector.
deploy.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web # MUST match template labels
template: # this is the Pod spec
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80
kubectl apply -f deploy.yaml
kubectl get deploy,rs,pods # see all three layers
kubectl get pods -l app=web # filter by label
Scaling
Change the replica count to scale up or down — imperatively or by editing the YAML.
kubectl scale deployment web --replicas=5 # imperative
# or edit replicas in YAML and re-apply (declarative)
Rolling updates & rollback (the Deployment superpower)
Deployments swap pods gradually between old and new ReplicaSets, giving zero-downtime updates you can undo.
Graph legend — each node maps to a rolling update of the web Deployment:
| Graph node | Maps to | What it does |
|---|---|---|
| Old RS nginx:1.27 | the current ReplicaSet | Runs the old image while the update proceeds |
| New RS nginx:1.28 | the ReplicaSet created by kubectl set image | Brings up pods on the new image |
| scale down old, scale up new | the Deployment rolling strategy | Shifts replicas gradually for zero downtime |
kubectl set image deployment/web nginx=nginx:1.28 # trigger update
kubectl rollout status deployment/web # watch progress
kubectl rollout history deployment/web # revisions
kubectl rollout undo deployment/web # rollback!
RS vs RC selector difference
The key upgrade from ReplicationController to ReplicaSet is set-based label selectors.
ReplicationController: equality only -> app = web
ReplicaSet: set-based too -> app in (web, api)
End-to-end flow
Deployment to ReplicaSet to Pods, plus a zero-downtime rolling update that swaps an old ReplicaSet for a new one.
Graph legend — each node maps to the web Deployment lifecycle:
| Graph node | Maps to | What it does |
|---|---|---|
| kubectl apply -f deploy.yaml | the declarative create | Submits the web Deployment |
| Deployment controller | kube-controller-manager | Owns ReplicaSets and drives rollouts |
| Creates ReplicaSet (nginx:1.27) | the initial ReplicaSet | Manages the first generation of pods |
| Creates 3 web Pods | replicas: 3 | The running nginx replicas |
| kubectl set image nginx=nginx:1.28 | the update trigger | Starts a new revision |
| New ReplicaSet (nginx:1.28) | the second ReplicaSet | Brings up updated pods |
| scale up / scale down | the rolling strategy | Swaps old pods for new with no downtime |
| Rollout complete | final state | All pods on nginx:1.28 |
Key takeaways
- Hierarchy: Deployment -> ReplicaSet -> Pods.
selector.matchLabelsMUST matchtemplate.metadata.labels.- Deployments add rolling updates + rollback; prefer them over RS/RC.
Checklist
- [ ] Created a Deployment with 3 replicas
- [ ] Killed a pod, watched it self-heal
- [ ] Scaled up/down
- [ ] Did a rolling update and an
undorollback