Sub topic 13
Beyond Ingress: the Gateway API
This chapter assumes you can write Ingress manifests with host and path rules, that you know the difference between an Ingress resource and an Ingress controller, and that you understand IngressClass and TLS Secrets. It covers where the Gateway API fits and when to choose it over Ingress.
Why a successor exists
Ingress covers host and path routing for HTTP and HTTPS, and little else. Everything beyond that lives in controller-specific annotations, such as rewrites, timeouts, header matching, and canary weights. Annotations are not portable, and they are untyped strings that the API server does not validate. A manifest tuned for one controller often behaves differently on another.
Ingress also puts everything in one object. The person who owns the load balancer and TLS settings edits the same resource as the person who owns the /api route.
The Gateway API is a newer set of resources from the Kubernetes project that addresses both problems. Kubernetes still supports Ingress, but the Ingress API is frozen. It receives no new features, and new routing capabilities are added to the Gateway API.
Three resources, three owners
The Gateway API splits the work into separate resources:
| Resource | Describes | Typical owner |
|---|---|---|
| GatewayClass | A type of gateway and the controller that implements it | Infrastructure provider |
| Gateway | A listener: port, protocol, hostname, TLS | Cluster operator |
| HTTPRoute | Routing rules that attach to a Gateway | Application developer |
GatewayClass plays the role that IngressClass plays for Ingress. A Gateway is roughly the entry point that an Ingress controller provisions. An HTTPRoute holds the rules that an Ingress puts under spec.rules.
Rewriting the shop example
The Gateway API resources are custom resources. Your cluster needs the Gateway API CRDs installed and a controller that implements them. A stock cluster does not include either. The following manifests are the equivalent of the shop Ingress from earlier in this tutorial.
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: example-gateway-class
spec:
controllerName: example.com/gateway-controller
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: shop-gateway
spec:
gatewayClassName: example-gateway-class
listeners:
- name: http
protocol: HTTP
port: 80
hostname: shop.example.com
allowedRoutes:
namespaces:
from: Same
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: shop
spec:
parentRefs:
- name: shop-gateway
hostnames:
- shop.example.com
rules:
- matches:
- path:
type: PathPrefix
value: /api
backendRefs:
- name: api
port: 80
- matches:
- path:
type: PathPrefix
value: /
backendRefs:
- name: web
port: 80The parentRefs field attaches the route to the Gateway. The Gateway's allowedRoutes setting controls which namespaces may attach routes. Same accepts only routes from the Gateway's own namespace.
What you gain
- Typed fields instead of annotations. Header matching, query parameter matching, redirects, URL rewrites, and request header changes are part of the HTTPRoute schema.
- Traffic splitting. Each entry in
backendRefsaccepts aweight, so a canary rollout needs no controller-specific annotation. - Safe delegation. Operators control the Gateway and its TLS, and developers control only their routes. Routes in other namespaces can reference a Service only if a ReferenceGrant allows it.
- More protocols. The API defines route types beyond HTTP, such as GRPCRoute and TLSRoute. Their maturity varies, so check what your controller supports.
- Status you can read. Routes and Gateways report whether they were accepted and attached, which makes debugging more direct than with Ingress.
Choosing between them
Keep Ingress when you have a working setup with simple host and path routing, and your controller needs nothing more. Choose the Gateway API for new clusters, for multi-team clusters that need clear ownership boundaries, and whenever you would otherwise reach for several annotations.
Both APIs can run side by side in one cluster, so you can migrate one application at a time.
Exercise
Your team needs a canary release that sends 10% of requests for shop.example.com to a new Service. The Ingress controller you use requires a vendor annotation for this. Which Gateway API feature replaces the annotation, and which resource holds it?
Show answer
Weighted backends. The HTTPRoute lists both Services in backendRefs and gives each a weight, for example 90 for the stable Service and 10 for the new one. The weights are fields in the HTTPRoute schema, so they do not depend on one controller's annotations.
Exercise
A developer creates an HTTPRoute in the namespace team-a with a parentRefs entry pointing at a Gateway in the namespace infra. The Gateway listener uses allowedRoutes.namespaces.from: Same. Will the route attach? Why?
Show answer
No. Same allows only routes from the Gateway's own namespace, which is infra. The operator must change allowedRoutes to allow team-a, for example with from: All or a namespace selector.