Lesson 18 · Section 2: Core Concepts
Lesson 18: Deployments: describe the app once, let Kubernetes roll it out
What a Deployment adds on top of ReplicaSets, how the controllers turn one object into running Pods, how a rolling update and a rollback really work, and the kubectl commands that make it quick in the exam. · 20 min read · 5 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why Deployments exist
A ReplicaSet does one job well: it keeps a fixed number of identical Pods running. What it cannot do is change them. Edit the image in a ReplicaSet's Pod template and nothing visible happens: the ReplicaSet only uses the template when it has to create a Pod, and it has no reason to create one while the count is right. To move to the new image you would have to delete the Pods yourself, one at a time if you care about downtime, and if the new version turned out to be broken there would be no record of what ran before.
A Deployment is the object that manages that change for you. You describe the application once (which Pod template, how many replicas), and whenever you change the description the Deployment replaces the Pods gradually, keeps the old version ready, and can put it back with one command.
What a Deployment is made of
Parts:
- You (kubectl apply)
- Deployment web (3 × nginx:1.28)
- ReplicaSet (web-5d4f8c7b9d)
- Pod (web-5d4f8c7b9d-4kq2x)
- Pod (web-5d4f8c7b9d-9wzjm)
- Pod (web-5d4f8c7b9d-tb7hn)
Connections:
- You to Deployment web: writes
- Deployment web to ReplicaSet: owns
- ReplicaSet to Pod
- ReplicaSet to Pod: owns
- ReplicaSet to Pod
There are three layers, and each one owns the layer below it:
- The Deployment holds the Pod template, the replica count and the update strategy. It is the only object you write and the only one you should edit.
- A ReplicaSet for each version of the Pod template. Its name is the Deployment's name plus a hash of the
template, for example
web-5d4f8c7b9d. At any quiet moment exactly one ReplicaSet has Pods; the older ones are kept at zero replicas as history. - The Pods, created by the ReplicaSet and named after it with a random suffix:
web-5d4f8c7b9d-4kq2x.
That naming is worth a few seconds in the exam. A Pod called web-5d4f8c7b9d-4kq2x tells you, before you run
anything else, that it belongs to a ReplicaSet web-5d4f8c7b9d, which belongs to a Deployment web. Deleting the
Pod will only make the ReplicaSet create another; to remove the application you delete the Deployment.
The fields that matter:
| Field | Meaning | Default |
|---|---|---|
spec.replicas | How many Pods you want | 1 |
spec.selector | Which Pods belong to this Deployment. Must match the template's labels, and cannot be changed later | required |
spec.template | The Pod: labels, containers, volumes. Any change here starts a rollout | required |
spec.strategy.type | RollingUpdate or Recreate | RollingUpdate |
spec.strategy.rollingUpdate.maxSurge | Extra Pods allowed above replicas during an update | 25%, rounded up |
spec.strategy.rollingUpdate.maxUnavailable | Pods allowed to be unavailable during an update | 25%, rounded down |
spec.minReadySeconds | How long a new Pod must stay ready before it counts as available | 0 |
spec.revisionHistoryLimit | How many old ReplicaSets are kept for rollbacks | 10 |
spec.progressDeadlineSeconds | After this long without progress the rollout is reported as failed | 600 |
kubectl get deployment summarises the state in three columns, and each answers a different question:
| Column | Question it answers |
|---|---|
READY | How many Pods are ready, out of how many you asked for? |
UP-TO-DATE | How many Pods already run the current template? |
AVAILABLE | How many Pods have been ready for at least minReadySeconds? |
During a rollout UP-TO-DATE climbs from zero to the replica count. When all three numbers agree, you are done.
How it works
Parts:
- kubectl (apply -f web.yaml)
- API server (stores the objects)
- Deployment ctrl (makes ReplicaSets), in kube-controller-manager
- ReplicaSet ctrl (makes Pods), in kube-controller-manager
- Scheduler (picks a node per Pod)
- kubelet (starts containers)
Connections:
- kubectl to API server: Deployment
- API server and Deployment ctrl
- API server and ReplicaSet ctrl
- API server and Scheduler
- API server to kubelet: Pods for its node
A Deployment is not a program that runs your Pods. It is a record in the API server, and several independent
controllers react to it. When you run kubectl apply -f web.yaml:
- The API server validates the Deployment and stores it. That is all it does.
- The Deployment controller (part of kube-controller-manager) sees a Deployment with no ReplicaSet for its current template, and creates one.
- The ReplicaSet controller (also in kube-controller-manager) sees a ReplicaSet that wants three Pods and has none, and creates three Pod objects.
- The scheduler sees Pods without a node and assigns one to each.
- The kubelet on each of those nodes sees a Pod assigned to it and starts the containers.
None of these components calls another. Each watches the API server and fixes one kind of gap between what is wanted
and what exists. This is why Kubernetes heals itself: delete a Pod and the ReplicaSet controller sees the count is
short; lose a node and the Pods on it are eventually removed and replaced elsewhere. It is also a troubleshooting
map. If a Deployment exists but there is no ReplicaSet, look at the controller manager. If the ReplicaSet exists
but there are no Pods, describe the ReplicaSet. If Pods are Pending, it is the scheduler's stage. If they are
assigned to a node but not running, it is the kubelet's.
Under the hood · How the Deployment finds its Pods: the pod-template-hash label
The Deployment controller hashes the Pod template and adds the result as a label, pod-template-hash, to the
ReplicaSet, to the ReplicaSet's selector and to every Pod it creates. That one label is what keeps the Pods of
two versions apart during a rollout, even though they all match the Deployment's own selector.
Two consequences follow:
- Only the template counts. Changing
replicasor the strategy does not change the hash, so it does not create a new ReplicaSet or a new revision. Changing an image, an environment variable, a label or an annotation in the template does. - The same template always gives the same ReplicaSet. If you set the image back to what it was, the hash matches the old ReplicaSet, and the Deployment scales that one up again instead of creating a third. A rollback is exactly that.
Never edit or remove the pod-template-hash label yourself.
Rolling updates
Parts:
- Deployment web (image: nginx:1.29)
- Old ReplicaSet (web-5d4f… 3 → 0)
- New ReplicaSet (web-7c9b… 0 → 3)
- Pod nginx:1.28 (Ready)
- Pod nginx:1.28 (Ready)
- Pod nginx:1.28 (Terminating)
- Pod nginx:1.29 (Ready)
- Pod nginx:1.29 (ContainerCreating)
Connections:
- Deployment web to Old ReplicaSet: scales down
- Deployment web to New ReplicaSet: scales up
- Old ReplicaSet to Pod nginx:1.28
- Old ReplicaSet to Pod nginx:1.28
- Old ReplicaSet to Pod nginx:1.28
- New ReplicaSet to Pod nginx:1.29
- New ReplicaSet to Pod nginx:1.29
When the template changes, the Deployment controller creates a new ReplicaSet at zero replicas and then moves Pods across in steps: scale the new one up a little, wait for those Pods to become ready, scale the old one down a little, and repeat. Two settings fix the size of each step:
maxSurgeis how many Pods may exist abovereplicaswhile the update runs.maxUnavailableis how many Pods may be unavailable, counted againstreplicas.
Both take a number or a percentage, and the percentages round in the safe direction: surge rounds up and unavailable rounds down. With the defaults of 25% each:
replicas | maxSurge | maxUnavailable | During the update |
|---|---|---|---|
| 3 | 1 (0.75 rounded up) | 0 (0.75 rounded down) | Up to 4 Pods, never fewer than 3 available |
| 4 | 1 | 1 | Up to 5 Pods, never fewer than 3 available |
| 10 | 3 (2.5 rounded up) | 2 (2.5 rounded down) | Up to 13 Pods, never fewer than 8 available |
So a three-replica Deployment with default settings updates one Pod at a time and never drops below three: start one new Pod, wait until it is ready, remove one old Pod, and so on. The two values cannot both be zero, because then no step would be possible.
The old ReplicaSet is not deleted when the rollout ends. It stays at zero replicas, holding the previous template,
until it falls out of the revisionHistoryLimit.
The two strategies
| Strategy | What happens | Use it when |
|---|---|---|
RollingUpdate (default) | Old and new Pods run side by side while Pods are swapped in steps | Almost always: there is no downtime if the two versions can coexist |
Recreate | Every old Pod is stopped first, then the new ones are created | Two versions must never run together, for example a volume only one Pod may mount, or an incompatible schema. There is downtime |
Under the hood · When a rollout gets stuck
A rollout that cannot make progress, for example because the new image does not exist, does not fail loudly. The new
ReplicaSet sits there with Pods in ImagePullBackOff, and the old Pods keep running inside the maxUnavailable
limit. After progressDeadlineSeconds (ten minutes by default) the Deployment gets the condition
Progressing=False with the reason ProgressDeadlineExceeded, and kubectl rollout status exits with an error.
Kubernetes does not roll back on its own. The Deployment keeps trying until you fix the template or run
kubectl rollout undo.
Scaling in the middle of a rollout works too: the controller spreads the extra replicas over the old and the new ReplicaSet in proportion to their size, so the balance of the rollout is kept.
Using it
Example · Create a Deployment and read what it made
Run three replicas of nginx:1.28 as the Deployment web in the namespace shop.
kubectl -n shop create deployment web --image=nginx:1.28 --replicas=3
kubectl -n shop get deployment,replicaset,pod -l app=web
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/web 3/3 3 3 15s
NAME DESIRED CURRENT READY AGE
replicaset.apps/web-5d4f8c7b9d 3 3 3 15s
NAME READY STATUS RESTARTS AGE
pod/web-5d4f8c7b9d-4kq2x 1/1 Running 0 15s
pod/web-5d4f8c7b9d-9wzjm 1/1 Running 0 15s
pod/web-5d4f8c7b9d-tb7hn 1/1 Running 0 15s
kubectl create deployment labels everything app=<name> and names the container after the image (nginx here).
You need that container name for kubectl set image.
Example · Write the manifest when the task asks for more
kubectl create deployment cannot set the strategy, probes or resources. Generate the manifest, add what is
missing, then apply it:
kubectl -n shop create deployment web --image=nginx:1.28 --replicas=3 --port=80 \
--dry-run=client -o yaml > web.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: shop
spec:
replicas: 3
selector:
matchLabels:
app: web
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.28
ports:
- containerPort: 80
readinessProbe:
httpGet:
path: /
port: 80
kubectl apply -f web.yaml
The labels under template.metadata.labels must include everything in selector.matchLabels. If they do not, the
API server rejects the Deployment.
Example · Update the image, watch the rollout, and undo it
Move web to nginx:1.29, confirm it, then go back.
kubectl -n shop set image deployment/web nginx=nginx:1.29 # <container>=<image>
kubectl -n shop rollout status deployment/web # waits until it is done
kubectl -n shop get replicasets # old one at 0, new one at 3
kubectl -n shop rollout history deployment/web # revisions 1 and 2
kubectl -n shop rollout history deployment/web --revision=1 # shows that revision's template
kubectl -n shop rollout undo deployment/web # back to the previous revision
kubectl -n shop rollout undo deployment/web --to-revision=2 # or to a chosen one
An undo is itself a rollout, with the same surge and unavailable limits. It also renumbers: when you go back to revision 1, that template becomes the newest revision (3) and revision 1 disappears from the history.
Example · Scale, and change several things in one rollout
Scaling changes only the replica count, so there is no rollout and no new revision:
kubectl -n shop scale deployment web --replicas=5
If a task changes the image and the resources, each kubectl set command would start its own rollout. Pause
first, make the changes, then resume, and the Pods are replaced only once:
kubectl -n shop rollout pause deployment/web
kubectl -n shop set image deployment/web nginx=nginx:1.29
kubectl -n shop set resources deployment/web -c nginx --requests=cpu=100m,memory=64Mi
kubectl -n shop rollout resume deployment/web
A single kubectl edit deployment web or kubectl apply -f web.yaml achieves the same, because it sends all the
changes together.
Tricks
Common mistakes
- Selector and template labels that do not match. The API server refuses the Deployment with an error that says the selector does not match the template labels. Fix the labels; they are the link between all three layers.
- Trying to change the selector later.
spec.selectoris immutable. If it is wrong, delete the Deployment and create it again. - Using the wrong name in
kubectl set image. The left side is the container name, not the Deployment or the image. Check it withkubectl get deployment <name> -o wide. - Editing the ReplicaSet or a Pod. The Deployment owns them. A replica count you set on the ReplicaSet is put back at once, and a change to a Pod is lost when that Pod is replaced. Always change the Deployment.
- Deleting Pods to remove an application. They come straight back. Delete the Deployment, or scale it to zero.
- Assuming a bad rollout fixes itself. A typo in the image tag leaves the rollout stuck with Pods in
ImagePullBackOff. The old Pods usually keep serving, so it is easy to miss:kubectl rollout statusshows it, andkubectl rollout undofixes it. - Expecting scaling to appear in the history. Only template changes create revisions.
Recap
- A Deployment is the description of your application; it owns a ReplicaSet for each version of the Pod template, and the ReplicaSets own the Pods.
- Independent controllers watch the API server and close the gap: Deployment controller, ReplicaSet controller, scheduler, kubelet.
- Only a change to
spec.templatestarts a rollout.maxSurgeandmaxUnavailable(25% each by default) set its pace, and readiness is the gate. - A rollback scales the old ReplicaSet back up:
kubectl rollout undo, with--to-revisionfor a specific one. - In the exam:
kubectl create deploymentwith--dry-run=client -o yaml,kubectl set image,kubectl scale, andkubectl rollout statusafter every change.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
3 hands-on labs for this lesson. You get goals, not steps: any solution that reaches them passes.
Create a Deployment, release a new version as a rolling update, and scale it for a busy day.
Build · Level 2 · standard · target 15 min
A rolling update of the checkout is stuck half-way. Find the cause and bring the Deployment back to health.
Fix · Level 2 · standard · target 15 min
Section capstone. Take over a half-finished namespace: get its Deployment running under a Pod limit, repair its Service, open it on a node port, add a Pod that calls another namespace, and write it all down as a manifest.
Fix · Level 3 · challenge · target 30 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.