Skip to content
Visual explainer 3h to learn 5 steps

Deployments, rollouts & rollbacks

Roll out, pause, scale and roll back a Deployment.

By Invicly Tech, , in Kubernetes Administrator (CKA). About 9 minutes to read.

Topic 12 of 33

In short

A Deployment manages ReplicaSets, which keep a set number of identical Pods running. Changing the Pod template triggers a rolling update that replaces Pods gradually. Each change is a revision, so you can pause, resume or roll back.

Explained simply

A Deployment is how you tell Kubernetes: "keep 3 copies of this app running, and when I give you a new version, swap them in carefully". You almost never create Pods by hand for an app. You create a Deployment, and it does the boring work for you.

Under the hood there is a helper object called a ReplicaSet. Its only job is to keep the right number of Pods alive. If a Pod dies or a node disappears, the ReplicaSet creates a new Pod somewhere else. The Deployment sits on top and decides which ReplicaSet should be active.

When you change the image, the Deployment makes a new ReplicaSet for the new version and slowly moves Pods over: a few new ones up, a few old ones down. Users keep getting answers the whole time. This is called a rolling update.

If the new version is bad, you do not need to remember the old YAML. Kubernetes kept the old ReplicaSet (scaled to zero), so one command, kubectl rollout undo, brings the previous version back in seconds.

Press Play on a diagram to walk through it one step at a time.

LayersDeployment ownership
  1. 1
    Deployment

    Desired replicas, Pod template and update strategy.

  2. 2
    ReplicaSet

    One per template revision; keeps the Pod count.

  3. 3
    Pods

    Running copies labeled to match the selector.

FlowRolling update
  1. 1
    Change image

    kubectl set image updates the Pod template.

  2. 2
    New ReplicaSet

    Scales up within maxSurge.

  3. 3
    Old ReplicaSet

    Scales down within maxUnavailable.

  4. 4
    Repeat until done

    Only after new Pods become Ready.

  5. 5
    Old RS kept at 0

    Enables kubectl rollout undo.

SequenceWho does what during kubectl set image
kubectl
API server
Deployment controller
ReplicaSet controller
  1. 1PATCH Pod template image
    kubectl to API server: PATCH Pod template image
  2. 2Watch: Deployment changed
    API server to Deployment controller: Watch: Deployment changed
  3. 3Create new ReplicaSet, replicas 1
    Deployment controller to API server: Create new ReplicaSet, replicas 1
  4. 4Create Pod for new ReplicaSet
    ReplicaSet controller to API server: Create Pod for new ReplicaSet
  5. 5New Pod Ready: scale old RS down by 1
    Deployment controller to API server: New Pod Ready: scale old RS down by 1
  6. 6Repeat until new RS is full
    Deployment controller to API server: Repeat until new RS is full

In depth

How Pods are tied to a ReplicaSet

Every time the Pod template changes, the Deployment controller hashes the template and creates a ReplicaSet named after that hash, for example web-7c5d8f9b6. The same hash is added to the Pods as the label pod-template-hash. That label keeps the old and new ReplicaSets from fighting over each other's Pods, even though both match the Deployment's selector.

The selector itself is immutable in apps/v1. If you need a different selector, you must delete and recreate the Deployment. This is also why the selector labels must be a subset of the template labels; the API rejects a Deployment where they do not match.

  • kubectl get rs -l app=web shows every revision's ReplicaSet
  • kubectl get pods --show-labels reveals pod-template-hash
  • Scaling changes replicas only, so no new ReplicaSet appears

The rollout math: maxSurge and maxUnavailable

Both settings accept a number or a percentage, and both default to 25 percent. Percentages are rounded: maxSurge rounds up and maxUnavailable rounds down. With 10 replicas and the defaults, the controller may run up to 13 Pods (10 plus 3) and must keep at least 8 available (10 minus 2).

The two values cannot both be 0, because then the controller could never make progress. maxSurge 1 with maxUnavailable 0 is the safest choice: one new Pod at a time, never below full capacity, but it needs spare room in the cluster. maxSurge 0 with maxUnavailable 1 needs no extra room but runs one Pod short during the update. The Recreate strategy skips all of this and stops every old Pod first, which you want only when two versions must never run together.

CompareThree update settings for 4 replicas
Surge 1, unavailable 0
  • Up to 5 Pods at once
  • Never below 4 ready
  • Needs spare capacity
  • Slowest but safest
Defaults 25% and 25%
  • Up to 5 Pods at once
  • At least 3 ready
  • Good general balance
  • Works on most clusters
Recreate
  • All old Pods stop first
  • Short full outage
  • Never two versions together
  • Use for schema clashes

Readiness gates the rollout

The controller only counts a new Pod as available when it is Ready, and if minReadySeconds is set, after it has stayed Ready that long. A readiness probe is therefore what stops a broken version from replacing healthy Pods: if the new Pods never pass the probe, the rollout simply stalls with old Pods still serving.

A stalled rollout does not roll back by itself. After progressDeadlineSeconds (default 600 seconds) the Deployment gets a Progressing condition with reason ProgressDeadlineExceeded, and kubectl rollout status exits with an error. Your pipeline or you must then run rollout undo.

  • minReadySeconds defaults to 0
  • progressDeadlineSeconds defaults to 600
  • kubectl describe deploy shows the Progressing condition

History, pause and restart

revisionHistoryLimit (default 10) controls how many old ReplicaSets are kept for rollback. kubectl rollout history lists them, and adding --revision=3 prints that revision's full Pod template. To give revisions a readable reason, set the annotation kubernetes.io/change-cause; the old --record flag is deprecated.

Rolling back is itself a new revision: undo to revision 2 copies its template forward, so revision 2 disappears from the list and reappears as the newest number. kubectl rollout pause lets you make several template edits (image, env, resources) and release them as one rollout on resume. kubectl rollout restart changes an annotation in the template, which rolls every Pod without changing the image.

Learn it in 5 short steps

About 35m each. Track them with a timer in your plan.

  1. 1
    Create a Deployment~35m
  2. 2
    Scale replicas up and down~35m
  3. 3
    Do a rolling update and pause it~35m
  4. 4
    Roll back a bad release~35m
  5. 5
    Tune maxSurge and maxUnavailable~35m

Key points

  • maxSurge and maxUnavailable both default to 25 percent.
  • Only Pod template changes create a new revision; scaling does not.
  • kubectl rollout undo returns to the previous revision or --to-revision=N.
  • Readiness probes keep a broken version from replacing healthy Pods.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
kubectl create deployment web --image=nginx:1.26 --replicas=3
kubectl scale deployment web --replicas=5
kubectl set image deployment/web nginx=nginx:1.27
kubectl rollout status deployment/web
kubectl rollout pause deployment/web
kubectl rollout resume deployment/web
kubectl rollout history deployment/web
kubectl rollout undo deployment/web --to-revision=1

Ship a release, spot the failure, roll back

You update the web Deployment to a tag that does not exist. Step 1: set the image and record why. Step 2: watch the rollout; it hangs because new Pods are stuck in ImagePullBackOff while old Pods keep serving. Step 3: confirm with get pods. Step 4: undo, which brings the previous ReplicaSet back to full size. Step 5: check history to see the new revision number.

bash
kubectl set image deployment/web nginx=nginx:1.99-typo
kubectl annotate deployment/web kubernetes.io/change-cause="try 1.99"
kubectl rollout status deployment/web --timeout=60s   # times out
kubectl get pods -l app=web                           # ImagePullBackOff
kubectl rollout undo deployment/web
kubectl rollout status deployment/web
kubectl rollout history deployment/web

Zero-downtime settings from YAML

A team wants updates that never drop below full capacity and wants every new Pod to stay healthy for 10 seconds before it counts. Step 1: generate a Deployment with dry-run. Step 2: add the strategy block and minReadySeconds. Step 3: apply and test with a new image. With 4 replicas, you will see at most 5 Pods and never fewer than 4 ready.

yaml
apiVersion: apps/v1
kind: Deployment
metadata: { name: web }
spec:
  replicas: 4
  minReadySeconds: 10
  strategy:
    type: RollingUpdate
    rollingUpdate: { maxSurge: 1, maxUnavailable: 0 }
  selector: { matchLabels: { app: web } }
  template:
    metadata: { labels: { app: web } }
    spec:
      containers: [{ name: nginx, image: "nginx:1.27" }]

Quick reference

  • kubectl create deploy web --image=nginx:1.27 --replicas=3 --dry-run=client -o yaml > web.yaml
  • kubectl rollout history deploy/web --revision=2 shows that revision's Pod template
  • kubectl rollout restart deploy/web re-creates all Pods with the same image
  • maxSurge rounds up, maxUnavailable rounds down; both default to 25%
  • revisionHistoryLimit defaults to 10 old ReplicaSets kept for rollback
  • A Deployment's selector is immutable; it must match the template labels
  • Find the container name for set image: kubectl get deploy web -o jsonpath='{..containers[*].name}'

Common mistakes

  • Using a container name that does not match in kubectl set image.
  • Editing a ReplicaSet directly; the Deployment will overwrite it.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1You run kubectl scale deployment web --replicas=6. What happens to the revision history?

Question 2

2A Deployment has 10 replicas and the default strategy. What is the most Pods that can exist during a rollout?

Question 3

3New Pods never become Ready during a rollout. What does the Deployment do by default?

Question 4

4Which command triggers a fresh rollout without changing the image?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Create Deployment api with image nginx:1.26 and 2 replicas, scale it to 4, then show which ReplicaSet owns the Pods.

  2. Exercise 2medium

    Pause api, change its image to nginx:1.27 and set an env var MODE=prod, then resume so both changes ship in one revision.

  3. Exercise 3hard

    Make api roll out one Pod at a time without ever dropping below 4 ready Pods, then roll back to revision 1 and explain the history output.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

What is the difference between a Deployment and a ReplicaSet?
A ReplicaSet only keeps a number of identical Pods running. A Deployment manages ReplicaSets so it can do rolling updates and rollbacks, so you should create Deployments and let them handle ReplicaSets.
How do I restart all Pods of a Deployment?
Run kubectl rollout restart deployment/NAME. It replaces Pods gradually using the normal rolling update rules, so there is no downtime if you have several replicas.
Why does my rollout never finish?
Usually the new Pods are not becoming Ready: a bad image, a crash, or a failing readiness probe. Check kubectl get pods and kubectl describe on a new Pod, then fix the cause or roll back.

Go deeper

ShareXLinkedInWhatsApp