Sub topic 9
Rolling out to production
This chapter assumes you know the basics of Git, CI, containers, and YAML. It also assumes you have read the earlier chapters on building one immutable artifact and promoting it through staging. Here we look at how that artifact reaches production users.
Why the rollout method matters
Your pipeline has already proven that the image works in staging. Production still differs from staging in traffic, data volume, and the number of instances. A rollout strategy limits the damage if something differs in a way no test caught.
A strategy answers three questions:
- How many users see the new version at first?
- How do you know it is healthy?
- How fast can you return to the old version?
Common strategies
| Strategy | How it works | Extra cost | Rollback speed |
|---|---|---|---|
| Rolling update | Replaces instances a few at a time | Low | Minutes, by rolling the update back |
| Blue-green | Runs a full new copy next to the old one, then switches traffic | A second full environment | Seconds, by switching traffic back |
| Canary | Sends a small share of traffic to the new version, then increases it | Small extra capacity and traffic routing | Fast, by sending traffic back to the old version |
Rolling update
A rolling update is the default for a Kubernetes Deployment. The cluster starts new instances (pods) and stops old ones in small batches. It only continues while the new pods pass their readiness checks. For a time, both versions serve traffic. Your code and database must tolerate that overlap.
The deploy.sh script from this tutorial already uses this method:
#!/usr/bin/env bash
# deploy.sh <environment> <image>
set -euo pipefail
env="$1"
image="$2"
kubectl --namespace "$env" set image deployment/app app="$image"
kubectl --namespace "$env" rollout status deployment/app --timeout=120sThe set image command changes only the image reference. The rollout status command waits for the rollout to finish. If it does not finish within 120 seconds, the command fails and so does the job. A failed job is your signal to stop and investigate.
Blue-green
In blue-green, "blue" is the current production and "green" is the new version. You deploy green, test it, then move the router or load balancer to it. Blue stays running, so going back means switching the router again. The cost is double capacity during the switch. Shared resources such as the database are still a single copy, so blue-green does not protect you from a bad data change.
Canary
A canary release sends a small share of traffic, for example 5%, to the new version. You compare its error rate and latency with the old version. If the numbers hold, you raise the share in steps until it reaches 100%. Canary gives the most real-world evidence for the least risk. It needs traffic routing (a service mesh or a load balancer with weighted rules) and metrics you trust.
Choosing a strategy
Match the strategy to the risk and to your tooling:
- Start with a rolling update. It needs no extra tools and works well for stateless services.
- Use blue-green when you need an instant switch back and can afford the extra capacity.
- Use canary when a failure would be costly and you have metrics that can detect it quickly.
Whatever you choose, the artifact stays the same. The strategy changes how the image is introduced, not what the image contains.
Where the strategy sits in the pipeline
The production job runs only after the build and staging jobs succeed. In the pipeline from this tutorial, deploy-production uses needs: [build, deploy-staging] and the production environment, which can hold an approval gate. It receives the same image reference that staging used.
Common errors
- Skipping readiness checks. Without them, the cluster sends traffic to pods that are not ready, and a rolling update can take down a working service.
- Watching the wrong signal. A rollout that "completes" only means the pods started. Check error rates and latency too.
- Ignoring version overlap. During a rolling update or canary, old and new code run together. A change that breaks the old version breaks the rollout.
- No time limit. A rollout that waits forever hides a stuck release. Use a timeout, as
--timeout=120sdoes.
Exercise
Exercise
Your team runs a stateless web service on Kubernetes and has no traffic-routing tools or detailed metrics yet. Which rollout strategy should you start with, and why?
Show answer
Start with a rolling update. It is built into a Kubernetes Deployment, needs no extra capacity or routing tools, and replaces pods gradually while readiness checks block unhealthy pods. Move to canary later, once you have traffic routing and metrics that can detect problems quickly.
Exercise
During a blue-green switch, the new version starts failing because of a bad database change. Why does switching traffic back to blue not fully fix the problem?
Show answer
Blue and green share the same database. The switch changes only where traffic goes, not the data or schema. If the change broke the old version too, or already altered data, blue still fails. Database changes must be backward compatible.