In this lesson, you will learn how to shift production traffic incrementally between service versions by assigning specific weights to different backend deployments using Ingress annotations.
The Concept of Weighted Traffic
When deploying a new version of an application, sending 100% of your users to the new code at once is risky. Instead of a binary switch, you can use Weight-Based Routing to split traffic. By modifying your Ingress controller’s configuration, you can instruct the load balancer to send, for example, 90% of requests to your stable (v1) service and 10% to your canary (v2) service.
Implementation via Annotations
Most production-grade Ingress controllers, such as NGINX or ALB, allow you to control traffic distribution through specialized metadata annotations. These annotations tell the controller to maintain a specific ratio of requests across multiple backend services.
Consider this example where we define two services: myapp-v1 (stable) and myapp-v2 (canary):
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: canary-ingress
annotations:
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-weight: "10"
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: myapp-v2
port:
number: 80
In this setup, the Ingress controller recognizes the canary flag. By setting canary-weight to 10, the controller automatically redirects 10% of incoming traffic to myapp-v2, while the remaining 90% continues to your primary Ingress path.
Why Weights Matter
This approach provides a granular "safety dial." By starting at 1% or 5%, you can observe error rates and latency in your logs. If everything looks healthy, you can incrementally update the annotation to 25, 50, and eventually 100 to complete the rollout. This method decouples the deployment of infrastructure from the exposure of your application, ensuring that any failures are isolated to a small percentage of your user base rather than impacting your entire infrastructure at once.
Key Takeaway: Weight-based routing uses Ingress annotations to safely split traffic between service versions, allowing for controlled, incremental rollouts that minimize risk to the end user.