Route web traffic with the Gateway API
In this task you'll send web traffic to the right app by host name and path, and split it between versions.
The Gateway API is the newer, more expressive way to bring traffic into a cluster, and the Kubernetes project now recommends it over Ingress for new work. It splits the job by role: a GatewayClass names the controller (installed by the platform team), a Gateway is the entry point with its listeners and ports, and HTTPRoute objects (owned by app teams) say which host names and paths go to which Services.
Routes can do things Ingress needed vendor annotations for: weighted traffic splits for canary releases, header matching, redirects and rewrites. A route in one namespace can attach to a shared Gateway only if the Gateway allows it, which keeps teams from taking each other's host names.
Example. Fernhill Cloud's platform team runs one shared Gateway for *.fernhill.example. The invoices team's HTTPRoute sends 90% of invoices.fernhill.example to version 1 and 10% to version 2 during a canary, then moves to 100% without touching the Gateway.
Before you start
You need: Docker Desktop, kind and kubectl on your computer (all free), and a cluster from Run a practice cluster on your computer, plus a Gateway API controller such as Envoy Gateway (free, installed with Helm). Time: 40 minutes.
Where to look
- Terminal
- Helm
- kubectl
Steps
- Install a Gateway API controller with its Helm chart (for Envoy Gateway:
helm install eg oci://docker.io/envoyproxy/gateway-helm -n envoy-gateway-system --create-namespace), create its GatewayClass, and checkkubectl get gatewayclassshows it as Accepted. - Create a Gateway with an HTTP listener on port 80 that allows routes from all namespaces.On kind it stays not Programmed, because there's no cloud load balancer to give it an address. That's expected: reach it with port-forward.
- Deploy two versions of an echo app (
shop-v1andshop-v2), each with its own Service. - Create an HTTPRoute for host
shop.localthat sends all traffic toshop-v1, and test it withcurl -H 'Host: shop.local'through a port-forward to the gateway's Service. - Change the route to weights 90 and 10, send 50 requests, and count how many reached v2.Roughly 5 should.
- Move the weight to 100 for v2 and remove v1 from the route.
Commands
# A shared entry point (platform team)
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata: {name: shared, namespace: infra}
spec:
gatewayClassName: eg
listeners:
- {name: http, protocol: HTTP, port: 80, allowedRoutes: {namespaces: {from: All}}}
---
# A canary split (app team)
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata: {name: shop, namespace: shop}
spec:
parentRefs: [{name: shared, namespace: infra}]
hostnames: [shop.local]
rules:
- backendRefs:
- {name: shop-v1, port: 80, weight: 90}
- {name: shop-v2, port: 80, weight: 10}
kubectl get httproute shop -n shop -o jsonpath='{.status.parents[0].conditions[*].type}'
Watch out for
- Cost
- Free on kind. In a cloud, each Gateway usually creates a paid load balancer, which is why teams share one.
- Security
- Let only the platform team create Gateways and certificates; app teams own only their routes, in their own namespaces.
- Common mistakes
- Creating a route whose parentRef names the wrong Gateway or namespace (it shows as not Accepted), and forgetting the Gateway's allowedRoutes setting.
Clean up
Delete the HTTPRoute, Gateway, GatewayClass and apps first, then helm uninstall the controller. If you uninstall first, the GatewayClass delete hangs waiting for a controller that has gone.
Words to know
- Gateway API
- The Kubernetes standard for routing traffic into a cluster, by role and by route.
- Gateway
- An entry point into the cluster: its listeners, ports and certificates.
- HTTPRoute
- Rules that match host names and paths and send traffic to Services.
- Weighted backends
- Splitting a route's traffic between Services by percentage.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On