Bird
Raised Fist0
Kubernetesdevops~5 mins

Path-based routing in Kubernetes - Commands & Configuration

Choose your learning style10 modes available
Introduction
Path-based routing lets you send web traffic to different backend services based on the URL path. This helps run multiple apps on the same domain but different paths without conflicts.
When you want to run a blog and a shop on the same website but separate their traffic.
When you have multiple microservices and want to route requests to each based on URL paths.
When you want to share one IP address but serve different apps on different URL paths.
When you want to simplify DNS by using one domain for many services.
When you want to control traffic flow easily without changing client URLs.
Config File - ingress.yaml
ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /blog(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: blog-service
            port:
              number: 80
      - path: /shop(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: shop-service
            port:
              number: 80

This Ingress resource routes traffic based on URL paths under the host example.com.

  • /blog paths go to the blog-service.
  • /shop paths go to the shop-service.
  • The annotation rewrites the URL path so backend services receive the correct path.
Commands
This command creates the Ingress resource in Kubernetes to enable path-based routing as defined in the file.
Terminal
kubectl apply -f ingress.yaml
Expected OutputExpected
ingress.networking.k8s.io/example-ingress created
This command checks that the Ingress resource was created and shows its details including assigned IP or hostname.
Terminal
kubectl get ingress example-ingress
Expected OutputExpected
NAME CLASS HOSTS ADDRESS PORTS AGE example-ingress <none> example.com 192.168.1.100 80 10s
This command shows detailed information about the Ingress, including rules and backend services for each path.
Terminal
kubectl describe ingress example-ingress
Expected OutputExpected
Name: example-ingress Namespace: default Address: 192.168.1.100 Default backend: <none> Rules: Host Path Backends ---- ---- -------- example.com /blog blog-service:80 (10.1.1.10:8080) /shop shop-service:80 (10.1.1.20:8080) Annotations: nginx.ingress.kubernetes.io/rewrite-target: /$2 Events: <none>
Key Concept

If you remember nothing else from this pattern, remember: path-based routing sends web requests to different services based on the URL path part.

Common Mistakes
Using incorrect path syntax without specifying pathType.
Kubernetes requires pathType to understand how to match paths; missing or wrong pathType causes routing failures.
Always specify pathType: Prefix or Exact for each path in the Ingress.
Not adding rewrite-target annotation when backend expects trimmed paths.
Backend services may receive full paths and fail if they expect only the subpath, causing errors.
Add nginx.ingress.kubernetes.io/rewrite-target annotation to adjust the path sent to backend.
Forgetting to create backend services before applying Ingress.
Ingress routes traffic to services; if services don't exist, routing fails and requests return errors.
Create and verify backend services before applying Ingress rules.
Summary
Create an Ingress resource with rules that match URL paths to backend services.
Apply the Ingress with kubectl and verify it is created and has an IP address.
Use annotations to rewrite paths if backend services expect trimmed URLs.

Practice

(1/5)
1. What does path-based routing in Kubernetes Ingress do?
easy
A. It sends traffic to different services based on URL paths.
B. It balances traffic evenly between all nodes.
C. It encrypts traffic between pods.
D. It schedules pods on specific nodes.

Solution

  1. Step 1: Understand Ingress role

    Kubernetes Ingress manages external access to services, often HTTP.
  2. Step 2: Identify path-based routing function

    Path-based routing directs requests to services depending on the URL path.
  3. Final Answer:

    It sends traffic to different services based on URL paths. -> Option A
  4. Quick Check:

    Path-based routing = directing traffic by URL path [OK]
Hint: Remember: path-based routing matches URL paths to services [OK]
Common Mistakes:
  • Confusing path-based routing with load balancing
  • Thinking it schedules pods
  • Assuming it encrypts traffic
2. Which is the correct way to specify a path in a Kubernetes Ingress rule for path-based routing?
easy
A. url: /app urlType: Prefix
B. route: /app routeType: Exact
C. servicePath: /app serviceType: Exact
D. path: /app pathType: Prefix

Solution

  1. Step 1: Recall Ingress rule syntax

    Ingress rules use path and pathType keys to define routing.
  2. Step 2: Match correct keys and values

    Correct keys are path and pathType, with values like '/app' and 'Prefix'.
  3. Final Answer:

    path: /app pathType: Prefix -> Option D
  4. Quick Check:

    Use path and pathType keys for routing [OK]
Hint: Look for 'path' and 'pathType' keys in Ingress rules [OK]
Common Mistakes:
  • Using wrong keys like 'route' or 'url'
  • Mixing up pathType values
  • Missing pathType field
3. Given this Ingress snippet, which service will receive traffic for URL path '/shop/cart'?
rules:
- http:
    paths:
    - path: /shop
      pathType: Prefix
      backend:
        service:
          name: shop-service
          port:
            number: 80
    - path: /blog
      pathType: Prefix
      backend:
        service:
          name: blog-service
          port:
            number: 80
medium
A. blog-service
B. shop-service
C. default backend
D. No service matches

Solution

  1. Step 1: Check path matching rules

    Path '/shop/cart' starts with '/shop', matching the first path rule with pathType Prefix.
  2. Step 2: Identify backend service

    The matching path routes to 'shop-service' on port 80.
  3. Final Answer:

    shop-service -> Option B
  4. Quick Check:

    Prefix match '/shop' routes to shop-service [OK]
Hint: Prefix path matches any URL starting with it [OK]
Common Mistakes:
  • Choosing blog-service for /shop/cart
  • Assuming no match for subpaths
  • Confusing default backend with path rules
4. You wrote this Ingress rule but traffic to '/api' is not routed correctly:
paths:
- path: api
  pathType: Prefix
  backend:
    service:
      name: api-service
      port:
        number: 80
What is the likely error?
medium
A. Incorrect pathType value
B. Service name is invalid
C. Missing leading slash in path value
D. Port number must be a string

Solution

  1. Step 1: Check path syntax

    Paths must start with a leading slash '/' to match URLs correctly.
  2. Step 2: Identify missing slash

    The path is 'api' instead of '/api', so it won't match requests starting with '/api'.
  3. Final Answer:

    Missing leading slash in path value -> Option C
  4. Quick Check:

    Paths need leading '/' for correct matching [OK]
Hint: Always start path with '/' in Ingress rules [OK]
Common Mistakes:
  • Omitting leading slash in path
  • Using wrong pathType values
  • Assuming port number must be string
5. You want to route traffic so that: - '/app' goes to 'app-service' - '/app/admin' goes to 'admin-service' Which Ingress path rules correctly implement this path-based routing?
hard
A. - path: /app/admin pathType: Prefix backend: service: name: admin-service port: number: 80 - path: /app pathType: Prefix backend: service: name: app-service port: number: 80
B. - path: /app pathType: Prefix backend: service: name: app-service port: number: 80 - path: /app/admin pathType: Prefix backend: service: name: admin-service port: number: 80
C. - path: /app pathType: Exact backend: service: name: app-service port: number: 80 - path: /app/admin pathType: Exact backend: service: name: admin-service port: number: 80
D. - path: /app/admin pathType: Exact backend: service: name: admin-service port: number: 80 - path: /app pathType: Exact backend: service: name: app-service port: number: 80

Solution

  1. Step 1: Understand path matching order

    Ingress matches paths in order; more specific paths must come first to avoid being shadowed.
  2. Step 2: Check rule order and pathType

    Placing '/app/admin' before '/app' with pathType Prefix ensures '/app/admin' routes to admin-service, others to app-service.
  3. Final Answer:

    Place '/app/admin' rule before '/app' rule with Prefix pathType -> Option A
  4. Quick Check:

    Order specific paths first for correct routing [OK]
Hint: List longer paths before shorter ones in Ingress rules [OK]
Common Mistakes:
  • Placing general path before specific path
  • Using Exact pathType causing no subpath matches
  • Ignoring rule order importance
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard