Lesson 35 of 70 · 7 min read · Updated
Canary and Blue/Green Releases with Gateway API Traffic Weights
On this page
The PedalShare pricing team has a new fare calculator. Sending all riders to it at once is risky, so the team wants to start with a small share of traffic, let QA reach it on purpose, and switch back in seconds if something goes wrong. With the Gateway API, all of this is the weight field on backendRefs. No service mesh and no controller-specific annotations are needed.
This lesson runs a canary from 10% to 50% to 100%, then a blue/green switch and rollback, on NGINX Gateway Fabric. It also shows two cases where weights do not do what people expect.
Technical Requirements
- The
gateway-labcluster with NGINX Gateway Fabric 2.8.1 and thepublicGateway - Sample repository: Fcakiroglu16/dotnetacademy, branch gateway-api-traffic-splitting
scripts/port-forward.shfrom the repository. Plainkubectl port-forwardexits after one broken connection, for example when NGINX reloads. The script restarts it and keepslocalhost:8080open.
The pricing team runs both versions side by side, each with three replicas and its own Service:
Core Concepts
Weights are proportions
Each entry in backendRefs has a weight (default 1). A request goes to a backend with probability weight / sum of weights within that rule. The weights do not have to add up to 100: 9 and 1 give the same split as 90 and 10.
| Weights (v1 / v2) | Share of v2 |
|---|---|
| 1 / 1 (default) | 50% |
| 90 / 10 | 10% |
| 3 / 1 | 25% |
| 0 / 100 | 100% |
The split is per request and random. It is not sticky: one rider can hit v1 and then v2. If the two versions must not mix within a session, add a header-based rule (shown below) or use session persistence, which is still an experimental feature in Gateway API.
Canary and blue/green are the same mechanism
- Canary: shift traffic in steps (10%, 50%, 100%) while you watch error rates and latency.
- Blue/green: run both versions at full size and switch with weights
100/0to0/100. A rollback is the reverse edit.
Both versions keep running during the change, so a rollback does not need to start new Pods. It only changes the proxy configuration.
Weights and backend health
A weight is applied before any health check. If a backend with weight 10 has no ready endpoints, about 10% of requests fail, and the route status still says ResolvedRefs=True, because the Service exists. Weights do not move traffic away from an unhealthy backend for you. That is the job of your rollout tooling or of a person watching the canary.
Hands-On: Kubernetes Commands
count-versions.sh calls GET /fares/quote?minutes=25 with Host: pricing.pedalshare.local and counts the version field in the answers. Extra arguments go to curl:
scripts/count-versions.sh
To change a split during a release, edit the weights and apply again, or patch them in place:
In a GitOps setup, the weights live in Git and each step is a commit, so the history of the rollout is the Git history.
Step-by-Step Example
Step 1: Canary at 10%
k8s/06-traffic-splitting/01-canary-10.yaml
Step 2: Let QA reach the canary on purpose
QA should not depend on luck to test v2. A rule with a header match has higher precedence than the weighted rule, so requests with X-PedalShare-Canary: always always go to v2, and everyone else keeps the 90/10 split:
k8s/06-traffic-splitting/02-canary-10-with-header.yaml
Step 3: 50% and then 100%
The metrics look good, so the team moves to 50/50 and then to 0/100. Only the weights change:
At this point, v1 still runs and takes no traffic. That is the blue/green position: v2 is live, v1 is the standby.
Step 4: Roll back
A pricing bug shows up after the cutover. The rollback sets the weights back to 100/0:
k8s/06-traffic-splitting/06-rollback.yaml
The first request after the apply already reached v1. The v1 Pods were running the whole time, so the rollback takes as long as one configuration update.
Step 5: Two cases to know
All weights at zero. A rule whose backends all have weight 0 has no destination. NGF answers 500:
A canary without ready Pods. Back at 90/10, the v2 Deployment is scaled to zero, as if every v2 Pod were crashing:
About 10% of the requests fail with 503, and the route status looks healthy. Watch the canary's error rate during a rollout, and set its weight to 0 when it fails.
Results
| Weights v1 / v2 | Requests | v1 | v2 |
|---|---|---|---|
| 90 / 10 | 1000 | 899 | 101 |
| 90 / 10 + header rule | 1000 | 891 | 109 |
Header X-PedalShare-Canary: always | 100 | 0 | 100 |
| 50 / 50 | 1000 | 512 | 488 |
| 0 / 100 (cutover) | 1000 | 0 | 1000 |
| 100 / 0 (rollback) | 300 | 300 | 0 |
| Edge case | Result on NGF 2.8.1 |
|---|---|
| All backends at weight 0 | 500 |
| 90 / 10 with no ready v2 Pods | 28 of 300 requests return 503, ResolvedRefs=True |
Summary
weightonbackendRefssplits traffic by proportion, per request. It is core Gateway API and works the same on any implementation.- Canary releases change the weights in steps, blue/green flips them between
100/0and0/100, and a rollback is one more edit that takes effect at once because both versions keep running. - A header-match rule beats the weighted rule, so testers can always reach the new version.
- Weights ignore backend health. A canary with no ready Pods fails its share of requests while the route status stays green, so watch the canary's metrics.
The last lesson of the series shares one Gateway between teams in different namespaces with ReferenceGrant, and routes gRPC traffic with GRPCRoute.