Sub topic 4
Traffic routing: load balancers, services, and weighted shifting
This chapter assumes you know what a Kubernetes Deployment and Service are, how a load balancer spreads requests across instances, and how a readiness probe works. Blue-green and canary releases both depend on controlling which version receives traffic, so this chapter covers how that control works.
Why routing is the control point
A deployment puts a new version on infrastructure. Routing decides who reaches it. If you can change routing quickly and precisely, you can expose a new version to nobody, to a few users, or to everyone, without redeploying anything.
Rolling updates also change routing, but only as a side effect. Pods join and leave the pool as they become ready. Blue-green and canary releases change routing on purpose, so you need to know which layer does the routing.
Where routing happens
Traffic passes through several layers. Each one can split traffic, and each has different precision.
| Layer | Example | What it can do |
|---|---|---|
| Service (L4) | Kubernetes Service | Spread connections across pods matching a label selector |
| Ingress or gateway (L7) | NGINX Ingress, Gateway API | Route by host, path, header, or weight |
| External load balancer | Cloud load balancer | Weighted target groups across environments |
| Service mesh | Istio, Linkerd | Per-request weights, header matching, retries |
A Kubernetes Service picks pods by label and spreads connections roughly evenly across them. It has no concept of weights. That is the main constraint to remember.
Routing by selector: the blue-green switch
Because a Service selects pods by label, you can add a version label and point the Service at one environment.
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
version: blue
ports:
- port: 80
targetPort: 8080Only pods labelled app: web and version: blue receive traffic. Pods labelled version: green can run next to them and be tested directly, but the Service ignores them. To cut over, change the selector:
kubectl patch service web -p '{"spec":{"selector":{"app":"web","version":"green"}}}'This moves all traffic at once. Switching the selector back is the rollback. Existing long-lived connections may stay on the old pods until they close, so the switch is not perfectly instant for every client.
Routing by weight: the canary split
A Service cannot send 10% of traffic to one version. Splitting by percentage needs a layer-7 component. With the NGINX Ingress controller, a second Ingress marked as a canary does this:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-canary
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: 80A primary Ingress for the same host keeps serving the rest. About 10% of requests go to the web-canary Service. Raising the weight to 25, 50, and then 100 is a weighted shift.
Common errors
- Treating the weight as exact. The percentage is statistical. With few requests, the observed share can differ noticeably from 10%.
- Sticky sessions. If users are pinned to a backend by cookie, a user may stay on one version across requests, and the split among users may not match the weight.
- Mixing up pod count and traffic share. Without an L7 weight, one canary pod among nine stable pods gets roughly 10% of connections. That share changes if you scale either side. Prefer explicit weights when you need a stable percentage.
- Selectors that overlap. If the Service selector omits
version, it sends traffic to both colours. Check withkubectl get endpoints webthat only the expected pod IPs are listed. - Skipping readiness. Routing sends traffic to any pod that reports ready. A missing or weak probe sends traffic to a pod that cannot serve it.
Quick comparison
| Strategy | Routing change | Granularity |
|---|---|---|
| Rolling | Pods join and leave the Service as they become ready | Pod count |
| Blue-green | Selector or load balancer target switch | All or nothing |
| Canary | Weighted split at ingress, load balancer, or mesh | Percentage or header rule |
Exercise
Your Service selector is app: web only, and you run 9 stable pods and 1 canary pod, all labelled app: web. Why is this a weak way to run a 10% canary, and what would you use instead?
Show answer
The Service spreads connections across all matching pods, so the canary share depends on pod counts. It drifts when either side scales, and you cannot change it without changing replicas. Use a layer-7 weighted split, such as a canary Ingress with canary-weight, a load balancer with weighted target groups, or a service mesh, so the percentage is set explicitly and independent of pod count.
Exercise
After you patch the Service selector from blue to green, some clients still reach blue pods for a short time. Name one likely cause.
Show answer
Long-lived connections, such as keep-alive or WebSocket connections, stay open to the old pods until they close. Client-side DNS or connection caching can have a similar effect.