Deployment strategies: rolling, blue-green and canary
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Shipping a new version means replacing the running copies of your app without users noticing. Watch four pods move from v1 to v2 four ways: Recreate (stop, then start, with downtime), a Kubernetes rolling update (one pod at a time, never below four ready), blue-green (a full second fleet and one switch), and a canary (10% of real traffic first, then grow or pull it). A short Python routing rule shows how a load balancer splits the requests.
Swap versions safely
Users send requests to a load balancer, which spreads them over four pods running version 1. A pod is one running copy of the app. Shipping version 2 means replacing those pods. A deployment strategy is the plan for that swap.
users → load balancer → 4 pods (v1)
pod = one running copy of the app
(one or more containers)
goal: v1 → v2 with no one noticing
strategy = which pods run when,
and who gets which versionاقرأ الدرس خطوة بخطوة
Swap versions safely
Users send requests to a load balancer, which spreads them over four pods running version 1. A pod is one running copy of the app. Shipping version 2 means replacing those pods. A deployment strategy is the plan for that swap.
users → load balancer → 4 pods (v1) pod = one running copy of the app (one or more containers) goal: v1 → v2 with no one noticing strategy = which pods run when, and who gets which versionRecreate: stop, then start
The simplest plan stops every v1 pod, then starts the v2 pods. Kubernetes calls it Recreate. Until v2 is ready, nothing can answer, so users get errors: downtime. Teams pick it when v1 and v2 cannot run side by side.
strategy: type: Recreate v1 ready 4 → 0 (all stopped) v2 ready 0 → 4 (all started) in between: 0 ready = downtime only one version ever runs use it when versions cannot coexist e.g. an incompatible database schemaRolling update, one by one
Kubernetes' default is a rolling update. With maxSurge: 1 it starts one extra v2 pod. Only when its readiness probe (a health check) passes does it get traffic; TO V2 shows v2's share of ready pods. Then one v1 pod stops, and the cycle repeats.
spec: replicas: 4 strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 # at most 1 extra pod maxUnavailable: 0 # never fewer than 4 ready $ kubectl set image deployment/web web=web:v2Never below four ready
Repeat until every pod runs v2. With maxUnavailable: 0, four pods are always ready, and Kubernetes never runs more than one extra pod beyond the four (a stopping pod may linger briefly while it shuts down). Both versions answer, so v2 must work beside v1.
ready pods always 4 counted pods at most 4 + 1 = 5 (pods shutting down not counted) TO V2 = v2 ready ÷ all ready e.g. 2 v2 of 4 ready → 50% $ kubectl rollout status deployment/web undo = roll back the same way, pod by pod: $ kubectl rollout undo deployment/webBlue-green: a second fleet
Blue-green keeps the live fleet (blue, v1) and starts a complete copy with the new version (green, v2) beside it. Green gets no users yet, so the team can test it in place. Eight pods: double the capacity, for a while.
blue = live, v1, 100% of users green = idle, v2, 0% of users 8 pods running = double the cost test green privately before anyone sees itFlip all traffic at once
When green passes its checks, the load balancer switches in one move: route(100). Every new request now goes to v2. Nobody sees a mix: before the flip everyone gets v1, after it everyone gets v2.
route(0) → every request to v1 (blue) route(100) → every request to v2 (green) in Kubernetes: change the Service selector $ kubectl patch service web -p \ '{"spec":{"selector":{"app":"web","version":"green"}}}'Rollback is one more flip
Blue is still running. If green starts failing, one flip back, route(0), restores v1 in seconds without starting a single pod. The broken green is then removed and fixed. Had it been healthy, blue would be removed instead.
green errors climb → flip back route(100) → route(0) blue was never stopped, so the rollback takes seconds healthy green? then retire blueCanary: test on a few
A canary release starts one v2 pod and sends it a small share of real traffic, here 10% with route(10). Miners once carried canaries to warn of bad air. Only 1 request in 10 risks the new code.
>>> route(10) roll = random.random() * 100 # 0 ≤ roll < 100 roll < 10 → "v2" (about 1 in 10) else → "v1" the figure sends exactly 1 in 10, so you can countSick canary? Pull it
The team compares the canary with v1 on the golden signals, such as errors and latency. Here the canary's error rate jumps, so they set route(0) and remove it. Only about 10% of requests ever met the bug.
compare canary vs v1: errors, latency (golden signals) canary errors ↑ → route(0), remove it blast radius ≈ 10% of requests, for a few minutesHealthy? Grow in stages
If the canary looks as healthy as v1, its share grows in stages, 10% then 50% then 100%, with a pause to watch the numbers at each one. v1 pods are removed as v2 takes over.
route(10) pause, watch route(50) pause, watch route(100) done tools such as Argo Rollouts run these steps (setWeight, pause) for youPick by risk and cost
Rolling needs no spare fleet but mixes versions and undoes slowly. Blue-green costs double for a while but switches and undoes at once. Canary limits the damage of a bug but needs weighted routing and good monitoring.
extra pods mix? undo recreate 0 no redeploy (downtime) rolling +1 (surge) yes roll back, pod by pod blue-green +4 (×2) no one flip, seconds canary +1 at first yes route(0), seconds
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01A Deployment has 4 replicas with maxSurge: 1 and maxUnavailable: 0. During a rolling update, what is true at every moment?
maxUnavailable: 0 means ready pods never drop below the 4 replicas; maxSurge: 1 lets Kubernetes add one extra pod, so it counts at most 5. Pods that are shutting down are not counted, so a few more may briefly exist.
02Why does the Recreate strategy cause downtime?
Recreate stops all of v1, then starts v2. Between the two, no pod is ready, so requests fail.
03In a blue-green deployment, how do you roll back a bad green release?
Blue was never stopped, so one switch back (route(0) in the figure) restores v1 in seconds, without starting any pods.
04The router runs route(10). Roughly what share of requests goes to v2?
roll = random.random() * 100 is a number from 0 up to 100; it is below 10 about 10% of the time, so about 1 request in 10 goes to v2.
05Which strategy needs the most extra capacity while it runs?
Blue-green runs a complete second fleet beside the live one, so for a while you pay for double the pods.
المسرد
- Pod
- The smallest thing Kubernetes runs: one or more containers that run one copy of your app.
- Load balancer
- A service that spreads incoming requests over several copies of an app. In Kubernetes, a Service does this for a set of pods.
- Deployment strategy
- The plan for replacing an old version with a new one: which copies run when, and which users get which version.
- Rolling update
- Replacing pods a few at a time, so old and new versions run side by side until every pod is new. Kubernetes' default.
- Readiness probe
- A health check Kubernetes runs on a pod; the pod only receives traffic once the check passes.
- Blue-green deployment
- Running the new version as a complete second fleet (green) beside the live one (blue), then switching all traffic at once.
- Canary release
- Sending a small share of real traffic to the new version first, watching its health, then growing the share or pulling it.
- Rollback
- Returning to the previous working version after a bad release.
الملاحظات والمصادر
- Left unset, Kubernetes uses maxSurge 25% (rounded up) and maxUnavailable 25% (rounded down). For 4 replicas that is 1 and 1, so ready pods may dip to 3; the figure sets maxUnavailable: 0 to keep all 4.
- A plain Kubernetes Service spreads requests over all ready pods (kube-proxy in iptables mode picks one at random), so in a rolling update v2's share follows the pod count. An exact weight such as 10% needs weighted routing: an ingress controller, a service mesh or a tool such as Argo Rollouts.
- route() is real Python: random.random() returns a float from 0.0 up to but not including 1.0, so route(0) always answers v1 and route(100) always v2. Random choice is only right on average; the figure sends exactly 1 request in 10 so you can count.
- Whenever two versions serve at once (rolling, canary, and blue-green while both fleets run), the database and any APIs must work with both. Recreate avoids that, at the cost of downtime.
- maxSurge limits the pods Kubernetes counts. Pods that are shutting down are not counted, so for a few seconds (until their termination grace period ends) more than 5 pods may exist.
- The blue and green rows are only places to draw old and new pods. In a rolling update Kubernetes does not keep two named fleets; it replaces pods in one Deployment.
- Kubernetes: Deployments (strategy, maxSurge, maxUnavailable, rollback)
- Kubernetes: Performing a Rolling Update
- Kubernetes: Pods
- Kubernetes: Liveness, Readiness, and Startup Probes
- Kubernetes: Service
- Kubernetes: Virtual IPs and Service Proxies
- Martin Fowler: BlueGreenDeployment
- Danilo Sato: CanaryRelease
- Google SRE workbook, ch. 16: Canarying Releases
- Google SRE book, ch. 6: Monitoring Distributed Systems (the four golden signals)
- Google Cloud: Application deployment and testing strategies
- AWS whitepaper: Blue/Green Deployments on AWS
- Argo Rollouts: Canary
- Python: random.random()
اختبار يتاح بعد الخطوة الأخيرة.