Sub topic 6
Canary Deployments: Exposing a Small Share of Traffic First
This chapter assumes you know what a Kubernetes Deployment and Service are, how a load balancer spreads requests, and what error rate and latency mean. It builds on the rolling and blue-green chapters.
The idea
A canary deployment runs the new version next to the old one and sends it only a small share of real traffic. If the new version misbehaves, a small share of users is affected. If it behaves, you raise the share step by step until the new version serves everyone.
The name comes from canaries in coal mines. The small group of exposed users is the early warning for everyone else.
Canary differs from the other two strategies in what it limits:
- Rolling limits how many instances change at once, but traffic shifts as instances are replaced.
- Blue-green moves all traffic in one switch.
- Canary controls the traffic share directly, so you pick the exposure.
What you need first
Canary depends on two things from earlier chapters.
- Traffic routing that can split by weight. This can be an ingress controller, a service mesh, or a cloud load balancer.
- Metrics that can be compared between the stable and canary versions. Without them, a canary only delays the problem.
Both versions must also be compatible with the same database and with each other, because they serve requests at the same time.
A canary with the NGINX Ingress controller
Assume the stable version already runs behind a Service named web and a normal Ingress for web.example.com. You add a second Deployment and Service for the new version, called web-canary. Then you add a canary Ingress for the same host.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-canary
annotations:
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-weight: "10"
spec:
ingressClassName: nginx
rules:
- host: web.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web-canary
port:
number: 80The canary: "true" annotation marks this Ingress as a canary of the primary Ingress for the same host. The canary-weight annotation sends about 10% of requests to web-canary. The primary Ingress keeps the other 90%.
The split is statistical. With few requests, the real share can differ noticeably from 10%.
Running the rollout
A typical canary follows a fixed sequence.
- Deploy the canary pods and wait until their readiness probes pass.
- Set the weight to a small value, such as 5 or 10.
- Observe the canary for a set period, comparing it with the stable version.
- If it is healthy, raise the weight: for example 10, 25, 50, then 100.
- At 100%, update the stable Deployment to the new image and remove the canary Ingress.
- If it is not healthy, set the weight to 0 or delete the canary Ingress. All traffic returns to stable.
Rollback is fast because the stable version never stopped running. Aborting means removing the canary from the routing, not rebuilding anything.
Common errors
- Too little traffic. At 1% of a low-traffic service, you may see only a handful of requests. You cannot tell a real problem from noise. Use a larger share, or wait longer.
- Observation window too short. Some failures appear only after caches fill, memory grows, or a scheduled job runs. A five-minute check can miss them.
- Comparing against the wrong baseline. Compare the canary with the stable pods running at the same time, not with last week. Traffic patterns change by hour.
- Per-request splitting breaks sessions. A single user may hit both versions on consecutive requests. If versions render pages or APIs differently, that user sees inconsistent behavior. Where this matters, route by a header or cookie instead of a random weight.
- Incompatible database changes. Both versions write to the same database during the canary. A schema change that the stable version cannot handle will break stable, not only the canary.
- Forgetting the canary replica count. A canary with one pod at 10% weight can be overloaded. Size canary capacity to the share of traffic it receives.
Deciding to promote or abort
Choose the decision rules before the release, not during it. For example: abort if the canary's 5xx error rate is more than one percentage point above stable, or if its p95 latency is more than 20% higher, over ten minutes. Writing these thresholds down avoids arguing about a graph while an incident unfolds. Tools such as Argo Rollouts and Flagger can apply such rules automatically and move the weight for you.
Exercise
A service receives about 20 requests per minute. A team starts a canary at 1% weight and checks the error rate after five minutes. Give two reasons this check is unreliable, and one change that helps.
Show answer
At 1% of 20 requests per minute, the canary receives about one request every five minutes, so about one request in the whole window. One error or none says almost nothing about the real error rate. Five minutes is also too short to expose slow problems such as memory growth. Raising the weight (for example to 25 or 50%) and lengthening the observation window gives enough requests to compare the canary with stable.
Exercise
During a canary at 25% weight, the canary shows a normal error rate but a p95 latency 40% higher than stable. What should the team do, and how is it undone?
Show answer
Abort, because the pre-agreed latency threshold is exceeded even though errors look normal. Set the canary weight to 0 or delete the canary Ingress. The stable pods still serve all traffic, so no redeploy is needed.