Sub topic 9
Sending traffic to Services and splitting it by weight
This chapter covers the backendRefs field of an HTTPRoute. It assumes you can write an HTTPRoute with parentRefs and matches, and that you know how Kubernetes Services select Pods.
What a backendRef is
A rule's matches decide which requests the rule handles. Its backendRefs decide where those requests go. Each entry names a backend, most often a Service, and the Gateway's controller forwards matching requests to it.
A Service backendRef needs two fields:
name: the Service name.port: the Service port number. Unlike the Ingressservice.portfield, this is required, and it has no default.
The Service must be in the route's namespace. Otherwise a ReferenceGrant is required, which a later chapter covers.
Splitting traffic by weight
When a rule lists several backendRefs, the controller divides requests among them in proportion to each entry's weight. This is the usual way to run a canary release, where a small share of traffic goes to a new version.
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: shop-split
namespace: shop-team
spec:
parentRefs:
- name: web-gateway
namespace: infra
hostnames:
- shop.example.com
rules:
- matches:
- path:
type: PathPrefix
value: /
backendRefs:
- name: shop-v1
port: 8080
weight: 90
- name: shop-v2
port: 8080
weight: 10This sends about 90% of requests to shop-v1 and 10% to shop-v2.
How weights are calculated
A weight is a ratio, not a percentage. A backend receives weight / sum of all weights of the traffic.
- Weights of 90 and 10 give the same split as 9 and 1.
- If you omit
weight, it defaults to 1. Two backends with no weights split traffic evenly. - A weight of 0 sends no traffic to that backend. This lets you keep a backend in the route and switch it on later.
- With only one backendRef, the weight has no effect.
The split is statistical. The controller makes its choice per request, so a small sample of requests can differ from the ratio. The Gateway API does not promise session affinity, so one client may reach both versions.
Common errors
| Symptom | Likely cause |
|---|---|
| The route is rejected or the backend is ignored | `port` is missing or does not match a port on the Service |
| Some requests return HTTP 500 | A backendRef points to a Service that does not exist. The controller sends that backend's share of traffic to an error response. |
| `ResolvedRefs` is `False` with reason `RefNotPermitted` | The Service is in another namespace and no ReferenceGrant allows it |
| Traffic split looks wrong | You expected weights to sum to 100, or you changed one weight and forgot the other |
Check the route's status to see these problems:
kubectl describe httproute -n shop-team shop-splitLook at the ResolvedRefs condition under the parent status. It reports whether every backendRef resolved to a usable backend.
Exercise
A rule has two backendRefs. app-a has weight: 3 and app-b has no weight. What share of requests does each receive?
Show answer
app-b defaults to weight 1, so the sum is 4. app-a receives 3/4 (75%) and app-b receives 1/4 (25%).
Exercise
You set weight: 0 on shop-v2 and weight: 1 on shop-v1. What happens to requests that match the rule?
Show answer
All requests go to shop-v1. A weight of 0 sends no traffic to shop-v2, but the backendRef stays in the route.