Sub topic 6
HTTPRoute: describing routing rules
This chapter assumes you can read Kubernetes YAML and know what Services and Namespaces are. It also assumes you have met GatewayClass and Gateway in the earlier chapters. You should know basic HTTP: hosts, paths, and headers.
What an HTTPRoute does
An HTTPRoute is the resource an application developer writes to describe how HTTP requests reach their Services. A Gateway opens the listener, which is the port and protocol. An HTTPRoute says what to do with the requests that arrive there.
Every HTTPRoute has four parts:
- parentRefs name the Gateway (or Gateways) the route attaches to.
- hostnames limit the route to requests for certain hosts.
- rules hold the matching and forwarding logic.
- status is written by the controller and reports whether the route was accepted.
Each entry in rules combines matches, optional filters, and backendRefs. A request is checked against the matches in a rule. If one matches, the filters run and the request goes to one of the backends.
A first route
This route lives in the shop-team namespace and attaches to the Gateway web-gateway in the infra namespace. It sends requests for shop.example.com whose path starts with /cart to port 8080 of the cart-svc Service.
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: shop
namespace: shop-team
spec:
parentRefs:
- name: web-gateway
namespace: infra
hostnames:
- shop.example.com
rules:
- matches:
- path:
type: PathPrefix
value: /cart
backendRefs:
- name: cart-svc
port: 8080Read it from the top down:
parentRefspoints at the Gateway. Thenamespacefield is required here because the Gateway is not in the route's namespace.hostnamesrestricts the route toshop.example.com. If you omit it, the route matches any host the listener accepts.- The rule has one match.
PathPrefixwith/cartmatches/cartand/cart/items, but not/cartoon, because prefixes are compared by path segment. backendRefsnames the Service and its port. Theportfield is required for a Service.
How a rule is chosen
A route can hold many rules, and several routes can share one hostname. The Gateway picks the most specific match. In general, a longer path wins over a shorter one. Within the same path, a match with an exact method or more header conditions wins. If two rules tie, the older route (by creation time) wins. Avoid relying on tie-breaking. Write rules that do not overlap.
Inside one match, all conditions must hold (AND). If a rule lists several entries under matches, a request needs to satisfy only one of them (OR).
Common errors
- Route never receives traffic. The
parentRefsname or namespace is wrong, or the Gateway'sallowedRoutesrejects your namespace. Check the route's status. - The path
/cartdoes not match/cartoon. This is correct behavior forPathPrefix, not a bug. - Backend not found. The Service name or port is misspelled, or the Service is in another namespace without a ReferenceGrant.
- Wrong hostname. A route for
shop.example.comdoes not serve requests forwww.shop.example.com. If the listener sets a hostname, the route's hostnames must overlap with it.
Exercise
A route has hostnames: [shop.example.com] and one rule with PathPrefix /cart. Which of these requests does it match: shop.example.com/cart/items, shop.example.com/cartoon, api.example.com/cart?
Show answer
Only shop.example.com/cart/items. /cartoon fails because PathPrefix compares whole path segments, and api.example.com fails the hostname restriction.