Sub topic 6
Translating Ingress rules into an HTTPRoute
This chapter assumes you know Pods, Services, Namespaces and kubectl. It also assumes you have used an Ingress with host and path rules, and that you have read the previous chapters on GatewayClass, Gateway and listeners. Here you move the routing rules from an Ingress into an HTTPRoute.
What moves where
An Ingress mixes three concerns in one object: TLS, listener settings and routing rules. The Gateway API separates them. The Gateway holds ports and TLS. The HTTPRoute holds hostnames, matches and backends.
| Ingress field | Gateway API location |
|---|---|
| `spec.tls` | Gateway `listeners[].tls` |
| `spec.ingressClassName` | Gateway `gatewayClassName` |
| `rules[].host` | HTTPRoute `hostnames` |
| `paths[].path` and `pathType` | HTTPRoute `matches[].path` |
| `backend.service` | HTTPRoute `backendRefs` |
| Annotations | Filters, or implementation-specific settings |
The translation
The starting Ingress routes /api to the api Service and / to web. The equivalent HTTPRoute is below.
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: app
namespace: shop
spec:
parentRefs:
- name: shared-gateway
namespace: gateway-system
sectionName: https
hostnames:
- app.example.com
rules:
- matches:
- path:
type: PathPrefix
value: /api
backendRefs:
- name: api
port: 8080
- matches:
- path:
type: PathPrefix
value: /
backendRefs:
- name: web
port: 80Differences to notice:
pathType: Prefixbecomestype: PathPrefix.ExactstaysExact.- A backend is
nameplusport, not a nestedserviceobject. Theportfield is a plain number. - Each rule holds a list of
matches. A request that satisfies any match in the list uses that rule's backends. parentRefsis new. It says which Gateway and listener this route attaches to. The next chapter covers it in detail.
Matching order
Ingress controllers often resolve overlapping paths in controller-specific ways. In the Gateway API the order is defined. The most specific match wins: exact path before prefix, then the longest prefix. In the example, /api/users goes to api because /api is longer than /.
Common errors
- Route never attaches. The
sectionNamedoes not match a listener name, or the listener'sallowedRoutesdoes not allow the route's namespace. - Hostname mismatch. A route hostname must fit within the listener's
hostname. Otherwise the route is not accepted. - Missing backend. A
backendRefsentry points to a Service that does not exist in the route's namespace. The route reportsResolvedRefsasFalse. - Port omitted. Unlike Ingress, a Service
backendRefrequires an explicit port.
Exercise
An Ingress rule has host: shop.example.com, path: /cart, pathType: Exact and backend cart on port 9000. Which HTTPRoute fields express this?
Show answer
Set hostnames to shop.example.com. Add a rule with a match of path.type: Exact and path.value: /cart. Set backendRefs to name: cart and port: 9000.