Traefik’s weighted service composition is the building block for both blue-green and canary deployments. Two backends, a percentage split, and a CI hook that flips the weights between deploys — that’s it. Add header-based routing on top and you can do internal-tester traffic before the canary, or pinned-user testing for a specific cohort. This article walks through both patterns, the rollback flow, and the observability you need to know whether the new release is actually healthier.
How to verify
# Confirm both services are healthy
curl -s http://127.0.0.1:8082/api/http/services | jq '.[] | select(.name | startswith("app-v")) | {name, status}'
# Generate traffic and confirm the split is roughly what you set
for i in $(seq 1 1000); do
curl -sI https://app.example.com/ | grep -i x-version
done | sort | uniq -c
# Expected: ~900 X-Version: v1, ~100 X-Version: v2 for a 90/10 split
# Per-service request rate in Prometheus
curl -s http://127.0.0.1:9100/metrics | grep -E 'traefik_service_requests_total{service="app-v(1|2)'
What’s happening
Blue-green deployment runs two identical fleets — blue (current) and green (new). 100% of traffic goes to blue while green is validated; a single config change flips 100% to green; if green fails, flip back to blue. The window where both fleets serve traffic is short.
Canary is the same machinery with a gentler ramp — 100/0, then 95/5, then 80/20, then 50/50, then 0/100, with health checks between each step. Bad releases get caught before they’re fully deployed.
Both patterns use Traefik’s weighted TraefikService (Kubernetes) or weighted service (file provider). The service references two backend services with weights; Traefik distributes requests proportionally. Weights are integers but represent ratios — 90:10, 1:1, 7:3 all work.
For internal-tester traffic, layer a header-matching router with higher priority above the split. Testers send X-Canary: true (set by your VPN proxy or a feature-flag service); their requests bypass the split and always hit green.
Each backend should emit a version header (X-Version: v1, X-Version: v2) so the access log and Prometheus can break out per-version metrics. Without that header, you cannot answer “is v2 actually causing more 5xx than v1?”.
The procedure
Blue-green flip
-
Deploy both fleets. Each has its own service name.
http: services: app-blue: loadBalancer: servers: - url: "http://10.0.1.10:8080" - url: "http://10.0.1.11:8080" app-green: loadBalancer: servers: - url: "http://10.0.2.10:8080" - url: "http://10.0.2.11:8080" app-split: weighted: services: - name: app-blue weight: 100 - name: app-green weight: 0 routers: app: rule: "Host(`app.example.com`)" entryPoints: [websecure] service: app-split -
Flip the weights to switch traffic. A CI job rewrites the file and Traefik picks it up via the file provider’s watch.
app-split: weighted: services: - name: app-blue weight: 0 - name: app-green weight: 100 -
Rollback is the inverse — flip the weights back. The blue fleet is still running, so the rollback latency is the file-watch interval, typically sub-second.
Canary ramp
-
Same backend setup. Define the ramp as a series of weight values; the CI job steps through them on a timer or after a health-gate check.
# ramp values progressed over 60 minutes # step 1: 95/5 # step 2: 80/20 # step 3: 50/50 # step 4: 20/80 # step 5: 0/100 app-split: weighted: services: - name: app-v1 weight: 95 - name: app-v2 weight: 5 -
Add a health-gate. Between steps, the CI checks the per-version 5xx rate.
# Pseudo-CI step curl -s http://prometheus/api/v1/query --data-urlencode \ 'query=sum(rate(traefik_service_requests_total{service="app-v2@file",code=~"5.."}[2m])) / sum(rate(traefik_service_requests_total{service="app-v2@file"}[2m]))' \ | jq '.data.result[0].value[1] | tonumber < 0.01' # If false, halt the ramp and roll back
Header-based opt-in
-
Add a higher-priority router that always sends traffic with the canary header to v2.
http: routers: app-canary: rule: "Host(`app.example.com`) && Header(`X-Canary`, `true`)" priority: 200 entryPoints: [websecure] service: app-v2 app: rule: "Host(`app.example.com`)" priority: 100 entryPoints: [websecure] service: app-split -
The canary header should come from a trusted source. Set it via your office VPN ingress, your auth proxy after login (testers in a specific group), or a feature-flag service.
Kubernetes flavour
apiVersion: traefik.io/v1alpha1
kind: TraefikService
metadata:
name: app-split
namespace: my-app
spec:
weighted:
services:
- name: app-v1
port: 8080
weight: 95
- name: app-v2
port: 8080
weight: 5
---
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: app
namespace: my-app
spec:
entryPoints: [websecure]
routes:
- match: Host(`app.example.com`) && Header(`X-Canary`, `true`)
priority: 200
kind: Rule
services:
- name: app-v2
port: 8080
- match: Host(`app.example.com`)
priority: 100
kind: Rule
services:
- name: app-split
kind: TraefikService
Common pitfalls
- Flipping weights without a per-version metric to gate on — you ramp blind and only learn v2 is bad from user reports.
- Sticky sessions on top of weighted services — once a user pins to v1, they stay on v1 forever; turn off sticky for the canary phase.
- Health checks not configured — a v2 fleet that comes up unhealthy still receives canary traffic until you notice.
- Different cookie or session formats between v1 and v2 — users bounced between versions during the ramp see logout / CSRF errors.
- File-provider weight changes that fail to reload — verify the file watcher picks up the change with a
docker logs traefik | grep 'configuration reloaded'.
Blue-green and canary patterns work because they make rollback cheap and observable. The CI pipeline that drives the weight changes, the metrics that gate them, and the runbook for emergency rollback are part of how Stack Harbor runs managed operations for clients with frequent deploys. For the underlying service-composition mechanics see traefik-routers-services-middlewares.