Host and path
- Docker & Kubernetes course
- Advanced
- Lesson 4/70
- 4 min
Learn host and path-based routing.
Host and path
A single Ingress can route by both domain name and path. pathType is the matching style:
Prefix:/apimatches/apiand/api/urunler(by path segment).Exact: only exactly/api.
If several paths match, the longest prefix wins.
rules:
- host: magaza.example.com
http:
paths:
- path: /api
pathType: Prefix
backend: {service: {name: api, port: {number: 8000}}}
- path: /
pathType: Prefix
backend: {service: {name: web, port: {number: 80}}}
- host: panel.example.com
http:
paths:
- path: /
pathType: Prefix
backend: {service: {name: panel, port: {number: 80}}}Trying it locally
To test without a real DNS record, the request is sent to the controller's IP and the domain name is given with the Host header; or a line is added to /etc/hosts:
curl -H "Host: magaza.example.com" http://$(minikube ip)/api/saglik
echo "$(minikube ip) magaza.example.com panel.example.com" | sudo tee -a /etc/hosts
curl http://panel.example.com/Practice A two-path Ingress
Write an Ingress's rules section (starting with rules:): host magaza.example.com; /api (Prefix) goes to port 8000 of the api service, / (Prefix) to port 80 of the web service.
Starter code
# buraya yazSample solution
rules:
- host: magaza.example.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: api
port:
number: 8000
- path: /
pathType: Prefix
backend:
service:
name: web
port:
number: 80Expected output
ingress.networking.k8s.io/magaza configured
$ curl -H "Host: magaza.example.com" http://192.168.49.2/api/saglik
{"durum":"ok"}
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 With the rules above, where does a request to magaza.example.com/api/urunler go?
- api:8000 Correct answer
- web:80
- panel:80
- 404
Explanation: /api wins because it's longer than the / prefix.
2 Match only the exact path.
- path: /saglik
pathType: ____
- path: /saglik
pathType: Exact
Explanation: Exact is an exact match.
3 Match.
| Term | Meaning |
|---|---|
Prefix |
Path segment prefix |
Exact |
Exact match |
longest prefix |
Wins on overlap |
Host header |
Testing without DNS |