0
0
KubernetesHow-ToBeginner · 4 min read

How to Use Path Based Routing in Kubernetes Ingress

Use a kubernetes Ingress resource with rules that specify http.paths to route traffic based on URL paths. Each path under a host directs requests to a specific service.name and service.port.number. This enables directing different URL paths to different backend services.
📐

Syntax

An Ingress resource defines rules for routing external HTTP(S) traffic to Kubernetes services. For path based routing, use the rules field with a host and http.paths. Each path has a path string and a backend specifying the service.name and service.port.number.

The pathType defines how the path is matched (e.g., Prefix for prefix matching).

yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /app1
        pathType: Prefix
        backend:
          service:
            name: app1-service
            port:
              number: 80
      - path: /app2
        pathType: Prefix
        backend:
          service:
            name: app2-service
            port:
              number: 80
💻

Example

This example shows an Ingress that routes requests with path /app1 to app1-service and requests with path /app2 to app2-service. Both services listen on port 80.

yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: path-routing-example
spec:
  rules:
  - host: mysite.example.com
    http:
      paths:
      - path: /app1
        pathType: Prefix
        backend:
          service:
            name: app1-service
            port:
              number: 80
      - path: /app2
        pathType: Prefix
        backend:
          service:
            name: app2-service
            port:
              number: 80
Output
Ingress resource "path-routing-example" created
⚠️

Common Pitfalls

  • Missing or incorrect pathType: Always specify pathType (e.g., Prefix or Exact) to avoid unexpected routing.
  • Overlapping paths: Paths are matched in order; overlapping prefixes can cause routing to the wrong service.
  • Service name or port typos: Ensure the backend service name and port exist and are correct.
  • Ingress controller not installed: Path based routing requires a running Ingress controller like NGINX or Traefik.
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: bad-ingress
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /app
        # Missing pathType causes error or fallback
        backend:
          service:
            name: app-service
            port:
              number: 80

---

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: good-ingress
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /app
        pathType: Prefix
        backend:
          service:
            name: app-service
            port:
              number: 80
📊

Quick Reference

  • pathType: Use Prefix for matching URL prefixes, Exact for exact matches.
  • Backend: Specify service.name and service.port.number.
  • Host: Define the domain name for routing rules.
  • Ingress Controller: Must be installed and running to apply Ingress rules.

Key Takeaways

Define path based routing in Ingress using the rules.http.paths section with path and backend service details.
Always specify pathType (Prefix or Exact) to control how paths are matched.
Ensure your backend services exist and ports are correct to avoid routing failures.
Install and configure an Ingress controller to enable Ingress resource functionality.
Avoid overlapping or ambiguous paths to prevent unexpected routing behavior.