Header-based and Cookie-based Traffic Routing¶
Header-based and Cookie-based traffic routing allows for granular control over request distribution in a system, often utilized within Canary Deployment strategies.^[400-devops__06-Kubernetes__k8s-ithelp__Day15__README.md]
In Kubernetes environments using the Nginx Ingress Controller, this functionality is enabled through specific resource annotations.^[400-devops__06-Kubernetes__k8s-ithelp__Day15__README.md]
Implementation¶
To enable these routing rules, an Ingress resource must include the annotation nginx.ingress.kubernetes.io/canary: "true", marking it as a Canary Ingress designed to handle split traffic.^[400-devops__06-Kubernetes__k8s-ithelp__Day15__README.md]
Header-based Routing¶
This method directs traffic based on specific HTTP headers included in the client request.^[400-devops__06-Kubernetes__k8s-ithelp__Day15__README.md]
Configuration is handled via the following annotations:
nginx.ingress.kubernetes.io/canary-by-header-value: Routes traffic to the Canary Ingress if a request header matches the configured value.^[400-devops__06-Kubernetes__k8s-ithelp__Day15__README.md]nginx.ingress.kubernetes.io/canary-by-header-pattern: Functions similarly tocanary-by-header-valuebut supports regular expressions for pattern matching.^[400-devops__06-Kubernetes__k8s-ithelp__Day15__README.md] This annotation is ignored ifcanary-by-header-valueis also set.^[400-devops__06-Kubernetes__k8s-ithelp__Day15__README.md]
Cookie-based Routing¶
This method utilizes cookies to determine routing behavior, which is useful for ensuring a consistent user experience during testing by routing the same user to the same version.^[400-devops__06-Kubernetes__k8s-ithelp__Day15__README.md]
It is configured using the nginx.ingress.kubernetes.io/canary-by-cookie annotation.^[400-devops__06-Kubernetes__k8s-ithelp__Day15__README.md] If the value is set to always, the controller will route all traffic possessing the matching cookie to the Canary Ingress.^[400-devops__06-Kubernetes__k8s-ithelp__Day15__README.md]
Evaluation Priority¶
When multiple routing rules are configured simultaneously, the Nginx Ingress Controller applies them in a specific order of precedence^[400-devops__06-Kubernetes__k8s-ithelp__Day15__README.md]:
canary-by-headercanary-by-cookiecanary-weight
This hierarchy ensures that explicit matches (Headers or Cookies) are evaluated before percentage-based weight distribution.^[400-devops__06-Kubernetes__k8s-ithelp__Day15__README.md]
Related Concepts¶
- Kubernetes
- [[Nginx Ingress]]
- Canary Deployment
Sources¶
- 400-devops__06-Kubernetes__k8s-ithelp__Day15__README.md