Labs · DevOps
Lab: Do a Rolling Update of a Kubernetes Deployment, Watch a Bad Image Get Stuck, and Roll Back with kubectl rollout undo
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: 0andmaxSurge: 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
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: 3maxUnavailable: 0means Kubernetes never removes an old Pod before a new one is ready.Apply and wait:
kubectl apply -f shop.yaml kubectl rollout status deploy/shopWhat you should see:
deployment "shop" successfully rolled out
Part 2: A good update
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/shopWhat you should see: Pods replaced one by one, then
successfully rolled out.Check the image and the history:
kubectl get deploy shop -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}' kubectl rollout history deploy/shopWhat you should see:
nginx:1.27and two revisions.
Part 3: A bad update, then rollback
Push a tag that does not exist:
kubectl set image deploy/shop web=nginx:9.99-doesnotexist kubectl get pods -l app=shopWhat you should see: one new Pod in
ErrImagePullorImagePullBackOff, and the three old Pods stillRunning. Users are still served.See that the rollout is stuck:
kubectl rollout status deploy/shop --timeout=30sWhat you should see:
error: timed out waiting for the conditionRoll back to the previous revision:
kubectl rollout undo deploy/shop kubectl rollout status deploy/shop kubectl get pods -l app=shopWhat you should see: three Running Pods on
nginx:1.27and no failing Pod.
Clean up
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
| Mistake | What happens | Fix |
|---|---|---|
| No readiness probe | Traffic goes to Pods that are not ready yet | Add readinessProbe |
maxUnavailable left at 25% with 1 replica | Short downtime during updates | Use 0 and maxSurge: 1 |
Using latest image tags | You cannot tell which version runs or roll back cleanly | Pin versions like nginx:1.27 |
Editing live objects with kubectl edit only | Git and cluster drift apart | Change 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.