Ravindra BagaleCourses & study guides Track your progress

Labs · DevOps

Lab: Do a Rolling Update of a Kubernetes Deployment, Watch a Bad Image Get Stuck, and Roll Back with kubectl rollout undo

Intermediate40 minkind · kubectl · Docker

Course: DevOps · Chapter 18: Kubernetes Workloads · Lesson: 18.15 Lab: Deployment, not a bare Pod

Chapter 18 explains ReplicaSets and Deployments; this lab shows why a Deployment is safer than a bare Pod.

Chala mitrano! Navin version takla ani site band zali, he nako. Aaj rolling update ani rollback haatane karuya.

Suppose we are…

Suppose we are on a Netflix-style team that releases several times a day. A release must not cause downtime, and a bad release must be undone in seconds, not by rebuilding servers.

In this lab we upgrade nginx with a rolling update, push a broken image tag on purpose, see that old Pods keep serving, and roll back.

Goal of this lab

By the end you will have:

  • A Deployment with maxUnavailable: 0 and maxSurge: 1.
  • A successful update with kubectl rollout status.
  • A stuck bad rollout fixed with kubectl rollout undo.

What you need

  • A kind cluster (kind create cluster --name lab).
  • 40 minutes.

Part 1: Version 1

  1. Save the Deployment with a safe rollout strategy:

    shop.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: shop
    spec:
      replicas: 3
      revisionHistoryLimit: 5
      strategy:
        type: RollingUpdate
        rollingUpdate:
          maxUnavailable: 0
          maxSurge: 1
      selector:
        matchLabels: { app: shop }
      template:
        metadata:
          labels: { app: shop }
        spec:
          containers:
            - name: web
              image: nginx:1.26
              ports: [{ containerPort: 80 }]
              readinessProbe:
                httpGet: { path: /, port: 80 }
                periodSeconds: 3
    

    maxUnavailable: 0 means Kubernetes never removes an old Pod before a new one is ready.

  2. Apply and wait:

    kubectl apply -f shop.yaml
    kubectl rollout status deploy/shop
    

    What you should see: deployment "shop" successfully rolled out

Part 2: A good update

  1. Update the image and record why:

    kubectl set image deploy/shop web=nginx:1.27
    kubectl annotate deploy/shop kubernetes.io/change-cause="upgrade nginx to 1.27" --overwrite
    kubectl rollout status deploy/shop
    

    What you should see: Pods replaced one by one, then successfully rolled out.

  2. Check the image and the history:

    kubectl get deploy shop -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'
    kubectl rollout history deploy/shop
    

    What you should see: nginx:1.27 and two revisions.

Part 3: A bad update, then rollback

  1. Push a tag that does not exist:

    kubectl set image deploy/shop web=nginx:9.99-doesnotexist
    kubectl get pods -l app=shop
    

    What you should see: one new Pod in ErrImagePull or ImagePullBackOff, and the three old Pods still Running. Users are still served.

  2. See that the rollout is stuck:

    kubectl rollout status deploy/shop --timeout=30s
    

    What you should see: error: timed out waiting for the condition

  3. Roll back to the previous revision:

    kubectl rollout undo deploy/shop
    kubectl rollout status deploy/shop
    kubectl get pods -l app=shop
    

    What you should see: three Running Pods on nginx:1.27 and no failing Pod.

Clean up

  1. Delete the Deployment, or the whole cluster:

    kubectl delete -f shop.yaml
    # kind delete cluster --name lab
    

Ravindra Bagale's Tip

Add a readiness probe to every Deployment. Without it, Kubernetes thinks a Pod is ready as soon as the process starts, and a rolling update can send traffic to a Pod that is still booting.

Common mistakes

MistakeWhat happensFix
No readiness probeTraffic goes to Pods that are not ready yetAdd readinessProbe
maxUnavailable left at 25% with 1 replicaShort downtime during updatesUse 0 and maxSurge: 1
Using latest image tagsYou cannot tell which version runs or roll back cleanlyPin versions like nginx:1.27
Editing live objects with kubectl edit onlyGit and cluster drift apartChange the YAML in Git and apply

Self-check checklist

0 of 4 done

Try-at-home challenge

Use kubectl rollout undo deploy/shop --to-revision=1 to go back to 1.26, then write in two lines when you would roll back versus roll forward with a fix.

Samajla ka? Rolling update for zero downtime, undo for bad days. Aata pudhe jaauya autoscaling.