0
0
Kubernetesdevops~10 mins

Host-based routing in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Host-based routing
Client sends HTTP request
Ingress Controller receives request
Check Host header in request
Host matches site1
Route to service1
Service handles request
The ingress controller receives a request and routes it to different services based on the Host header in the HTTP request.
Execution Sample
Kubernetes
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
spec:
  rules:
  - host: site1.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: service1
            port:
              number: 80
  - host: site2.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: service2
            port:
              number: 80
This ingress routes requests to service1 or service2 depending on the Host header.
Process Table
StepRequest HostIngress Rule MatchedService Routed ToAction
1site1.example.comhost: site1.example.comservice1Request routed to service1
2site2.example.comhost: site2.example.comservice2Request routed to service2
3unknown.example.comno matching hostnoneRequest rejected or default backend used
💡 Routing stops after matching host or no match found
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3
Request Hostnonesite1.example.comsite2.example.comunknown.example.com
Matched Rulenonehost: site1.example.comhost: site2.example.comnone
Routed Servicenoneservice1service2none
Key Moments - 2 Insights
Why does a request with an unknown host not get routed to any service?
Because the ingress rules only match specific hosts (see execution_table row 3). If no host matches, the request is rejected or sent to a default backend if configured.
Can multiple hosts route to the same service?
Yes, you can define multiple host rules pointing to the same service. Each host is checked independently as shown in the concept flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which service handles the request when the host is site2.example.com?
Aservice2
Bnone
Cservice1
Ddefault backend
💡 Hint
Check execution_table row 2 under 'Routed Service'
At which step does the ingress controller find no matching host rule?
AStep 1
BStep 3
CStep 2
DNone
💡 Hint
See execution_table row 3 'Ingress Rule Matched' column
If a new host rule for site3.example.com routing to service3 is added, what changes in the variable tracker?
AMatched Rule will always be none
BA new row for service3 is added
CRequest Host variable will include site3.example.com after a new request
DRouted Service will be empty
💡 Hint
Variable tracker shows Request Host changes per step; adding a new host means new requests can have that host
Concept Snapshot
Host-based routing in Kubernetes Ingress:
- Routes HTTP requests based on the Host header
- Define multiple host rules in ingress spec
- Each host routes to a specific backend service
- Requests with unmatched hosts are rejected or use default backend
- Enables hosting multiple sites on one ingress controller
Full Transcript
Host-based routing in Kubernetes means the ingress controller looks at the Host header in incoming HTTP requests. It compares this host to the rules defined in the ingress resource. If the host matches a rule, the request is sent to the backend service specified for that host. If no host matches, the request is rejected or sent to a default backend if configured. This allows one ingress controller to serve multiple websites or services by routing requests based on their hostnames.