08

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

TermMeaning
DeploymentManages ReplicaSets for rolling updates and rollback
ReplicaSet (RS)Keeps N pod replicas running
ReplicationController (RC)Legacy predecessor of the ReplicaSet
ReplicaOne copy of a pod
Rolling updateGradual pod replacement with no downtime
RollbackRevert to a previous revision
SelectorLabel 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 nodeMaps toWhat it does
api-server / etcd / schedulerthe control-plane componentsAccept, store, and schedule the Deployment's pods
controller-mgr - Deployment to ReplicaSetkube-controller-managerKeeps replicas: 3 of web alive
Pod - one of 3 identical web replicasa pod from the web ReplicaSetOne copy of the app
container nginx:1.27template.spec.containers[0].imageThe 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 nodeMaps toWhat it does
Deployment webkind: Deployment, metadata.name: webManages ReplicaSets, adds rollouts and rollbacks
ReplicaSet web-xxxxthe ReplicaSet the Deployment createsKeeps replicas: 3 nginx pods running
Pod web nginx:1.27one managed podA 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 nodeMaps toWhat it does
desired replicas 3spec.replicas: 3The target count of nginx pods
web nginx pod diesa crashed/deleted podDrops actual count to 2
ReplicaSet notices actual 2 != desired 3the ReplicaSet controllerDetects the gap from desired state
NEW web nginx podthe recreated podRestores 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 nodeMaps toWhat it does
Old RS nginx:1.27the current ReplicaSetRuns the old image while the update proceeds
New RS nginx:1.28the ReplicaSet created by kubectl set imageBrings up pods on the new image
scale down old, scale up newthe Deployment rolling strategyShifts 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 nodeMaps toWhat it does
kubectl apply -f deploy.yamlthe declarative createSubmits the web Deployment
Deployment controllerkube-controller-managerOwns ReplicaSets and drives rollouts
Creates ReplicaSet (nginx:1.27)the initial ReplicaSetManages the first generation of pods
Creates 3 web Podsreplicas: 3The running nginx replicas
kubectl set image nginx=nginx:1.28the update triggerStarts a new revision
New ReplicaSet (nginx:1.28)the second ReplicaSetBrings up updated pods
scale up / scale downthe rolling strategySwaps old pods for new with no downtime
Rollout completefinal stateAll pods on nginx:1.28

Key takeaways

  • Hierarchy: Deployment -> ReplicaSet -> Pods.
  • selector.matchLabels MUST match template.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 undo rollback