Sub topic 6
Routing by host and by path
This chapter assumes you can write and apply YAML manifests with kubectl, that you have a ClusterIP Service running, and that an Ingress controller is installed in your cluster. It also assumes you know basic HTTP: the Host header, URL paths, and status codes.
How a rule is evaluated
An Ingress holds a list of rules. Each rule can name a host and lists paths. The controller reads a request's Host header and path, finds the matching rule, and forwards the request to the Service named in that path's backend.
The backend Service
Every path points at a Service. Here is the web Service used in this chapter. It exposes port 80 and forwards to container port 8080.
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 8080The Ingress refers to the Service port (80), not the container port (8080). An api Service is assumed to exist with the same shape.
Path-based routing
This Ingress sends shop.example.com/api to the api Service and everything else on that host to web.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: shop
spec:
ingressClassName: example-class
rules:
- host: shop.example.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: api
port:
number: 80
- path: /
pathType: Prefix
backend:
service:
name: web
port:
number: 80The /api path and the / path both match a request for /api/users. Kubernetes resolves this by preferring the longest matching path, so the request goes to api. A request for /index.html matches only /, so it goes to web.
Host-based routing
A rule's host is compared with the request's Host header. You can add several rules to one Ingress, each with its own host, and send each host to a different Service.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: two-hosts
spec:
ingressClassName: example-class
rules:
- host: shop.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web
port:
number: 80
- host: api.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: api
port:
number: 80Both hostnames must resolve in DNS to the address of the same controller. One external address then serves many sites.
A host may start with a wildcard label, such as *.example.com. It matches exactly one label, so it matches shop.example.com but not a.b.example.com or example.com. If a rule has no host, it matches requests for any host.
Path types in brief
Each path needs a pathType. The next chapter covers matching in detail, so only the essentials are listed here.
| pathType | Behavior |
|---|---|
| Exact | Matches the URL path exactly, with case sensitivity. |
| Prefix | Matches by path elements split on `/`. `/api` matches `/api` and `/api/users` but not `/apiary`. |
| ImplementationSpecific | The controller decides how to match. |
Common errors
- Wrong Service port. The
port.numbermust match a port the Service exposes, not the container'stargetPort. - Service in another namespace. The backend Service must be in the same namespace as the Ingress.
- Host mismatch. Requests sent by IP address, or with a different Host header, do not match a host rule. Test with
curl -H "Host: shop.example.com" http://<address>/api. - Path forwarded unchanged. By default the controller sends
/api/usersto the backend with the/apiprefix intact. If the app expects/users, you need a controller-specific rewrite setting, which is not portable.
Exercise
An Ingress has one rule for host shop.example.com with two paths: / (Prefix) to web and /api (Prefix) to api. Where does a request for http://shop.example.com/api/orders go, and why?
Show answer
It goes to the api Service. Both paths match, but the longest matching path (/api) wins over /.
Exercise
You want blog.example.com and shop.example.com served by different Services through one controller address. What do you add to the Ingress, and what must be true in DNS?
Show answer
Add one rule per host, each with its own host value and backend Service. Both hostnames must resolve in DNS to the controller's external address.