Deployment patterns: rolling, blue-green, canary, GitOps
Ship new versions without downtime: a rolling update, a bad release caught and undone, a blue-green switch, a canary taking a share of traffic, and how GitOps and CI/CD automate it.
By the end of this lesson you'll be able to
0 of 4 completeVerified lab: a rolling update, a broken release undone with rollout undo, a blue-green switch and a
Pod-count canary, on the course's EKS cluster. The output shown is real.
Code walkthrough: Argo CD and the CI pipeline (manifests and workflow shown, not run in the lab).
Cloud lab: none needed; works on kind.
Releasing is when most outages happen. Kubernetes gives you a safe default, the rolling update, and the pieces
to build the other common patterns. This lesson runs each one with the shop-web images: v1, v2, and
v3-broken, which crashes on start.
Rolling updates and rollbacks
Changing a Deployment's image starts a rolling update: new Pods come up, and old ones go away only once new ones are ready.
$ kubectl set image deployment/shop-web web=626159997645.dkr.ecr.ap-south-1.amazonaws.com/shop-web:v2 deployment.apps/shop-web image updated $ kubectl rollout status deployment/shop-web --timeout=180s Waiting for deployment "shop-web" rollout to finish: 1 out of 2 new replicas have been updated... Waiting for deployment "shop-web" rollout to finish: 1 old replicas are pending termination... deployment "shop-web" successfully rolled out $ kubectl run c --image=busybox:1.37 --restart=Never --rm -i --quiet -- wget -qO- http://shop-web/api {"version": "v2", "pod": "shop-web-57f494cdcb-ltvkc", "greeting": "Hello from Kubernetes", "uptime_seconds": 11}
kubectl set image deployment/shop-web web=626159997645.dkr.ecr.ap-south-1.amazonaws.com/shop-web:v2kubectltalk to a Kubernetes cluster
kubectl rollout status deployment/shop-web --timeout=180srolloutmanage a rollout
kubectl run c --image=busybox:1.37 --restart=Never --rm -i --quiet -- wget -qO- http://shop-web/api--image=busybox:1.37container image
Now a bad release:
$ kubectl set image deployment/shop-web web=626159997645.dkr.ecr.ap-south-1.amazonaws.com/shop-web:v3-broken $ kubectl rollout status deployment/shop-web --timeout=60s Waiting for deployment "shop-web" rollout to finish: 1 out of 2 new replicas have been updated... error: timed out waiting for the condition $ kubectl get pods -l app=shop-web NAME READY STATUS RESTARTS AGE shop-web-555d869f9-7fzmm 0/1 CrashLoopBackOff 3 (34s ago) 67s shop-web-57f494cdcb-ltvkc 1/1 Running 0 86s shop-web-57f494cdcb-tckth 1/1 Running 0 79s $ kubectl get deployment shop-web NAME READY UP-TO-DATE AVAILABLE AGE shop-web 2/2 1 2 51m
kubectl set image deployment/shop-web web=626159997645.dkr.ecr.ap-south-1.amazonaws.com/shop-web:v3-brokenkubectltalk to a Kubernetes cluster
kubectl rollout status deployment/shop-web --timeout=60srolloutmanage a rollout
kubectl get pods -l app=shop-webgetlist objects-lonly objects with this label
This is the rolling update protecting you. The one new Pod never became ready, so the Deployment never removed
an old one: READY 2/2, users still on v2. The readiness probe (lesson 4) is what makes this work; without it,
the crashing Pod could have been counted as ready. Undo:
$ kubectl rollout undo deployment/shop-web Warning: resource deployments/shop-web was previously managed with 'kubectl apply'. Rolling back will not update the kubectl.kubernetes.io/last-applied-configuration annotation, ... deployment.apps/shop-web rolled back $ kubectl rollout status deployment/shop-web --timeout=180s deployment "shop-web" successfully rolled out $ kubectl get pods -l app=shop-web -o custom-columns=POD:.metadata.name,IMAGE:.spec.containers[0].image POD IMAGE shop-web-57f494cdcb-ltvkc 626159997645.dkr.ecr.ap-south-1.amazonaws.com/shop-web:v2 shop-web-57f494cdcb-tckth 626159997645.dkr.ecr.ap-south-1.amazonaws.com/shop-web:v2
kubectl rollout undo deployment/shop-webkubectltalk to a Kubernetes clusterrolloutmanage a rollout
kubectl get pods -l app=shop-web -o custom-columns=POD:.metadata.name,IMAGE:.spec.containers[0].imagegetlist objects-lonly objects with this label-ooutput format
Read that warning: rollout undo changes the cluster but not your YAML. The next kubectl apply of the old
file would bring v3-broken back. In real teams the fix goes into Git (or the Helm values), which is the GitOps
idea below. Tune the pace with maxSurge and maxUnavailable in the Deployment's strategy.
Blue-green
Run the new version next to the old one, test it, then move all traffic at once. Two Deployments with a
color label, and one Service choosing between them:
# 14-bluegreen.yaml (excerpt) kind: Deployment metadata: name: shop-blue # image shop-web:v1, labels app=shop, color=blue --- kind: Deployment metadata: name: shop-green # image shop-web:v2, labels app=shop, color=green --- apiVersion: v1 kind: Service metadata: name: shop spec: selector: { app: shop, color: blue } # live version ports: - port: 80 targetPort: 8080
$ kubectl run c --image=busybox:1.37 --restart=Never --rm -i --quiet -- wget -qO- http://shop/api {"version": "v1", "pod": "shop-blue-55c7dcf55f-jth8r", "greeting": "Hello from Kubernetes", "uptime_seconds": 6} $ kubectl patch service shop -p '{"spec":{"selector":{"app":"shop","color":"green"}}}' service/shop patched $ kubectl run c --image=busybox:1.37 --restart=Never --rm -i --quiet -- wget -qO- http://shop/api {"version": "v2", "pod": "shop-green-5c46897fcf-2r9kg", "greeting": "Hello from Kubernetes", "uptime_seconds": 17}
kubectl run c --image=busybox:1.37 --restart=Never --rm -i --quiet -- wget -qO- http://shop/apikubectltalk to a Kubernetes cluster--image=busybox:1.37container image
One patch, and every new request goes to v2. Rolling back is the same patch with blue. The cost: both versions
run in full during the switch, so you pay for double capacity, and both must work with the same database.
Canary
Send a small share of traffic to the new version first and watch its errors before going further. The
simplest form uses Pod counts: remove color from the selector so the Service matches both, then run 3 blue and
1 green.
$ kubectl patch service shop -p '{"spec":{"selector":{"app":"shop","color":null}}}' $ kubectl scale deployment shop-blue --replicas=3 $ kubectl scale deployment shop-green --replicas=1 $ for i in $(seq 1 20); do wget -qO- http://shop/api; done | grep -o '"version": "v[0-9]"' | sort | uniq -c 28 "version": "v1" 12 "version": "v2"
kubectl patch service shop -p '{"spec":{"selector":{"app":"shop","color":null}}}'kubectltalk to a Kubernetes cluster
kubectl scale deployment shop-blue --replicas=3scalechange the number of replicas--replicas=3how many copies
kubectl scale deployment shop-green --replicas=1--replicas=1how many copies
for i in $(seq 1 20); do wget -qO- http://shop/api; done | grep -o '"version": "v[0-9]"' | sort | uniq -cforloop over a list$(seq 1 20);the output of another commanddostart of what the loop runs each timewgetdownload a filedoneend of the loop|pipe: send output to the next commandgrepfind lines that match a pattern-oprint only the matching partsortsort linesuniqdrop repeated lines-ccount each one
The counts are doubled because of a kubectl quirk in the lab: the client Pod's attach failed and kubectl replayed its logs, so every line appears twice. The real split was 14 v1 and 6 v2 out of 20, about the 25% that one Pod in four gives (random spread over a small sample). If v2's error rate stays flat, scale green up and blue down until blue is gone.
Pod-count canaries are coarse: you can't do 1% without 99 old Pods. For exact weights, use an ingress or Gateway API route with weights, a service mesh, or Argo Rollouts, which also checks metrics and rolls back automatically when the canary looks bad.
GitOps
In every example above a person typed commands. GitOps makes Git the source of truth: the desired state of the cluster lives in a repository, and a controller in the cluster keeps the cluster matching it. Change Git, and the cluster follows; change the cluster by hand, and the controller puts it back.
The CI/CD course animates the sync loop, including self-heal undoing a manual scale.
Argo CD is the most common controller. An Application points it at a path in a repo:
# shop-app.yaml (Argo CD Application) apiVersion: argoproj.io/v1alpha1 kind: Application metadata: name: shop namespace: argocd spec: project: default source: repoURL: https://github.com/<you>/shop-deploy path: helm/shop targetRevision: main helm: valueFiles: [values-prod.yaml] destination: server: https://kubernetes.default.svc namespace: shop syncPolicy: automated: prune: true # delete what was removed from Git selfHeal: true # undo manual changes
A release is then a pull request that changes image.tag in values-prod.yaml. A rollback is reverting that
commit, which fixes the problem the rollout undo warning pointed at: Git and the cluster never disagree for
long. Every change has an author, a review and a history. Flux is the other main GitOps tool and works the same
way.
CI/CD integration
The pieces join up like this:
- CI (GitHub Actions, GitLab CI, Jenkins) runs tests, builds the image, tags it with the commit SHA, scans it and pushes it to the registry (ECR in this course).
- CI then updates the image tag in the deployment repo (or opens a PR for production).
- CD, Argo CD here, sees the change and syncs the cluster. The Deployment does a rolling update, or Argo Rollouts runs a canary.
CI never needs cluster credentials in this setup, which is a real security win: the cluster pulls from Git instead of CI pushing into the cluster.
Release, break, switch and split
Roll shop-web to v2, then to v3-broken, and undo it. Apply the blue-green manifest, switch from blue to green and back, then run a 3:1 canary and count versions over 40 requests. Record each step.
~/releases.txtReveal one safe solution
$ kubectl set image deployment/shop-web web=<registry>/shop-web:v2 && kubectl rollout status deployment/shop-web $ kubectl set image deployment/shop-web web=<registry>/shop-web:v3-broken $ kubectl rollout status deployment/shop-web --timeout=60s >> ~/releases.txt 2>&1 $ kubectl get pods -l app=shop-web >> ~/releases.txt $ kubectl rollout undo deployment/shop-web && kubectl rollout status deployment/shop-web $ kubectl apply -f 14-bluegreen.yaml $ kubectl patch service shop -p '{"spec":{"selector":{"app":"shop","color":"green"}}}' $ kubectl patch service shop -p '{"spec":{"selector":{"app":"shop","color":null}}}' $ kubectl scale deployment shop-blue --replicas=3; kubectl scale deployment shop-green --replicas=1 $ kubectl run c --image=busybox:1.37 --restart=Never --rm -i --quiet -- sh -c 'for i in $(seq 1 40); do wget -qO- http://shop/api; echo; done' | grep -o '"version": "v[0-9]"' | sort | uniq -c >> ~/releases.txt $ kubectl delete -f 14-bluegreen.yaml
kubectl set image deployment/shop-web web=<registry>/shop-web:v2 && kubectl rollout status deployment/shop-webkubectltalk to a Kubernetes cluster&&run the next command only if this one workedrolloutmanage a rollout
kubectl rollout status deployment/shop-web --timeout=60s >> ~/releases.txt 2>&1>>append output to a file2>&1send errors to the same place as output
kubectl get pods -l app=shop-web >> ~/releases.txtgetlist objects-lonly objects with this label
kubectl apply -f 14-bluegreen.yamlapplycreate or update from a file-ffrom this file
kubectl scale deployment shop-blue --replicas=3; kubectl scale deployment shop-green --replicas=1scalechange the number of replicas--replicas=3;how many copies--replicas=1how many copies
kubectl run c --image=busybox:1.37 --restart=Never --rm -i --quiet -- sh -c 'for i in $(seq 1 40); do wget -qO- http://shop/api; echo; done' | grep -o '"version": "v[0-9]"' | sort | uniq -c >> ~/releases.txt--image=busybox:1.37container image|pipe: send output to the next commandgrepfind lines that match a pattern-oprint only the matching partsortsort linesuniqdrop repeated lines-ccount each one
kubectl delete -f 14-bluegreen.yamldeletedelete an object
Expect a timeout for v3-broken with READY still 2/2, and roughly three v1 requests for every v2.
Try it yourself
0 of 5 steps doneRun each task in your own account or terminal, then tick it off.
Quick check
A rolling update to a crashing image stalls with 1 of 2 new replicas updated. Is the app down?
Official reading for this lesson
Or get every quiz answer right and it completes itself.
Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.