Kubernetes cookbook · Advanced
Send a percentage of traffic to a new version with the Gateway API
Release a new version to 10% of users first, then increase.
Approach
Run the stable and canary versions as separate Deployments and Services, and split traffic with weighted
backendRefs in an HTTPRoute. Change weights step by step while watching error rates.
Solution
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata: {name: shop}
spec:
parentRefs: [{name: public-gateway, namespace: infra}]
hostnames: [shop.example.com]
rules:
- backendRefs:
- {name: web-stable, port: 80, weight: 90}
- {name: web-canary, port: 80, weight: 10}
Pitfalls
- Sticky sessions or caches that mix versions in one user journey unexpectedly.
- Database changes that the stable version can't handle.
Variations
- Header-based routing (
matches: headers) to send internal users to the canary first. - Argo Rollouts or Flagger to automate weights based on metrics.
Further reading: Gateway API: traffic splitting
Reference: https://gateway-api.sigs.k8s.io/guides/traffic-splitting/
More Kubernetes recipes
- Autoscale a Deployment on CPU and memory
- Debug a distroless pod with an ephemeral container
- Default-deny network policies that keep DNS working
- Enforce the restricted Pod Security Standard
- Expose a service over HTTPS with automatic certificates
- Give a CI pipeline least-privilege deploy access
- Handle SIGTERM in your application for Kubernetes
- Manage dev, staging and prod with Kustomize overlays