Sub topic 9
Replacing canary annotations with weights
This chapter assumes you know how an Ingress routes by host and path, and that you have read the earlier chapters on Gateways and HTTPRoute basics. You should be comfortable applying YAML with kubectl.
How Ingress NGINX does canaries
Ingress NGINX implements a canary release with a second Ingress. That Ingress points at the new version and carries annotations that mark it as a canary:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-canary
namespace: shop
annotations:
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-weight: "10"
spec:
ingressClassName: nginx
rules:
- host: web.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web-canary
port:
number: 80The controller finds the primary Ingress for the same host and path. It then sends about 10 percent of requests to web-canary. The split is hidden in annotations on a separate object, so you have to read two Ingresses to see the full picture.
The Gateway API equivalent
In the Gateway API, weights are part of the route. A rule lists several backendRefs, and each one has a weight. One HTTPRoute describes the whole split:
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: web-canary
namespace: shop
spec:
parentRefs:
- name: shared-gateway
namespace: gateway-system
sectionName: https
hostnames:
- web.example.com
rules:
- backendRefs:
- name: web-stable
port: 80
weight: 90
- name: web-canary
port: 80
weight: 10Each backend receives its weight divided by the sum of all weights in that rule. Here that is 90/100 and 10/100. The weights do not have to add up to 100. A weight of 0 sends no traffic to that backend, and the default is 1 when you omit it.
Differences to watch for
- One object, not two. The stable and canary backends sit in the same rule, so you no longer need a second Ingress.
- Both Services must exist. The stable backend is named explicitly. In Ingress NGINX it came from the primary Ingress.
- Splits are approximate. Each request is routed independently, so small samples can drift from the ratio.
- Header and cookie canaries. Annotations such as
canary-by-headerandcanary-by-cookiehave no portable weight-based equivalent. You can often approximate them with a separate rule that uses headermatchesand points at the canary Service. Check that your chosen implementation supports it.
Rolling out a change
To shift traffic, edit the weights and reapply the route:
kubectl apply -f web-canary-route.yaml
kubectl describe httproute web-canary -n shopCheck that the Accepted and ResolvedRefs conditions are True before you trust the split. A misspelled Service name makes ResolvedRefs false. Depending on the implementation, traffic meant for that backend may then fail.
Exercise
A route has two backends, web-stable with weight 3 and web-canary with weight 1. What share of requests goes to the canary, and what happens if you set the canary weight to 0?
Show answer
The canary receives 1 / (3 + 1) = 25 percent of requests. With weight 0 it receives no traffic, and all requests go to web-stable.