CodeBlanks Logo Icon CodeBlanks

Host and path

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: /api matches /api and /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

terminal
# buraya yaz

Sample solution

terminal
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

Expected 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.

Question
          - path: /saglik
            pathType: ____
Answer
          - 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

All lessons in Advanced