0
0
Kubernetesdevops~10 mins

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

Choose your learning style9 modes available
Process Flow - Path-based routing
Incoming HTTP Request
Check URL Path
Route to Service1
Response
Incoming requests are checked for their URL path and routed to different services based on the path prefix.
Execution Sample
Kubernetes
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
spec:
  rules:
  - http:
      paths:
      - path: /app1
        pathType: Prefix
        backend:
          service:
            name: service1
            port:
              number: 80
      - path: /app2
        pathType: Prefix
        backend:
          service:
            name: service2
            port:
              number: 80
Ingress resource routes requests with path /app1 to service1 and /app2 to service2.
Process Table
StepIncoming Request PathPath Match CheckService Routed ToAction
1/app1/homeMatches /app1 prefixservice1Request routed to service1
2/app2/dashboardMatches /app2 prefixservice2Request routed to service2
3/app3/infoNo matching path prefixdefault backend or 404Request routed to default or rejected
💡 Routing stops after matching the first path prefix or falls back to default backend.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3
request_pathnone/app1/home/app2/dashboard/app3/info
matched_pathnone/app1/app2none
routed_servicenoneservice1service2default backend or 404
Key Moments - 2 Insights
Why does a request to /app3/info not get routed to service1 or service2?
Because /app3 does not match any defined path prefixes (/app1 or /app2) in the ingress rules, so it goes to the default backend or returns 404 as shown in execution_table row 3.
What happens if two paths overlap, like /app and /app1?
The ingress controller matches the first path prefix that fits the request path. Order matters, so the more specific path should be listed first to avoid unexpected routing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which service handles the request path '/app2/dashboard'?
Aservice1
Bservice2
Cdefault backend
DNo service
💡 Hint
Check execution_table row 2 under 'Service Routed To'
At which step does the routing fall back to the default backend or 404?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at execution_table row 3 'Path Match Check' and 'Service Routed To'
If a new path /app is added before /app1, which service will handle '/app1/home' requests?
Aservice for /app
Bdefault backend
Cservice1
DNo service
💡 Hint
Path matching uses first match order; adding /app before /app1 changes routing as per key_moments explanation
Concept Snapshot
Path-based routing in Kubernetes Ingress:
- Define multiple paths under rules.http.paths
- Use pathType: Prefix for prefix matching
- Requests routed to backend service matching first path prefix
- Order of paths matters for overlapping prefixes
- Unmatched paths go to default backend or 404
Full Transcript
Path-based routing in Kubernetes Ingress works by checking the incoming HTTP request's URL path. The ingress controller compares the path against defined prefixes in the ingress rules. When a match is found, the request is routed to the corresponding backend service. If no path matches, the request goes to the default backend or returns a 404 error. The order of path definitions is important because the first matching prefix is used for routing. This allows different services to handle different parts of a website or application based on URL paths.