ENع
←Deploy strategies
Lesson 16 · Delivery

Deployment strategies: rolling, blue-green and canary

14 min11 steps5 questionsIntermediate
About this lesson

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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 16 · Interactive
01 / 11

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
Loading figure…
or ← →
Read the lesson step by step
  1. 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
  2. Recreate: 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 schema
  3. Rolling 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:v2
  4. Never 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/web
  5. Blue-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 it
  6. Flip 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"}}}'
  7. 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 blue
  8. Canary: 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 count
  9. Sick 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 minutes
  10. Healthy? 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 you
  11. Pick 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

Quiz Unlocks after the last step.