Skip to content

How to Configure Kubernetes Ingress Path-Based Routing

DodaTech 2 min read

In this tutorial, you'll learn about How to Configure Kubernetes Ingress Path. We cover key concepts, practical examples, and best practices.

The Problem

You have multiple backend services (API, web app, admin panel) running in the same cluster but they all need to be accessible on the same domain. A single LoadBalancer service can only route to one backend.

Quick Fix

Step 1: Create the backend services

Ensure each backend has a ClusterIP service:

kubectl create service clusterip api-service --tcp=80:3000
kubectl create service clusterip web-service --tcp=80:8080

Step 2: Define the Ingress with path rules

Create an Ingress that routes /api/* to the API service and /app/* to the web service:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: multi-path-ingress
spec:
  ingressClassName: nginx
  rules:
    - host: myapp.example.com
      http:
        paths:
          - path: /api
            pathType: Prefix
            backend:
              service:
                name: api-service
                port:
                  number: 80
          - path: /app
            pathType: Prefix
            backend:
              service:
                name: web-service
                port:
                  number: 80

Step 3: Apply the Ingress

Create the Ingress resource:

kubectl apply -f ingress.yaml
ingress.networking.k8s.io/multi-path-ingress created

Step 4: Verify the Ingress rules

Check the routing configuration:

kubectl describe ingress multi-path-ingress
Name:             multi-path-ingress
Rules:
  Host                Path  Backends
  ----                ----  --------
  myapp.example.com
                      /api   api-service:80 (10.1.0.2:3000)
                      /app   web-service:80 (10.1.0.3:8080)

Step 5: Test the routing

Send requests to verify each path reaches the correct service:

curl http://myapp.example.com/api/users
curl http://myapp.example.com/app/dashboard

Step 6: Use path rewriting

If the backend does not expect the prefix, add rewrite annotations:

metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
    - host: myapp.example.com
      http:
        paths:
          - path: /api(/|$)(.*)
            pathType: ImplementationSpecific
            backend:
              service:
                name: api-service
                port:
                  number: 80

Alternative Solutions

Use different Ingress controllers for different paths

Run multiple Ingress controllers with different ingressClassName:

metadata:
  name: app-ingress
spec:
  ingressClassName: traefik

Use subdomain-based routing instead of paths

Route by subdomain for simpler service isolation:

spec:
  rules:
    - host: api.example.com
      http:
        paths:
          - backend:
              service:
                name: api-service

Common Mistakes to Avoid

Using PathType: Exact when you need Prefix. Exact matches require the URL to end without a trailing slash, causing 404 errors for /api/users when /api is exact.

Forgetting the rewrite-target annotation. Most backends do not expect the path prefix. Add the nginx rewrite annotation to strip it.

Not specifying ingressClassName. If your cluster has multiple Ingress controllers, the wrong one might pick up the Ingress rule.

Pro Tips

Use the nginx canary annotation for gradual rollouts. Route a percentage of traffic to a new version: nginx.ingress.kubernetes.io/canary-weight: "10".

Use custom snippet annotations for advanced routing. Add custom nginx configuration with nginx.ingress.kubernetes.io/server-snippet for complex rules.

Use certificate-manager with Ingress for automatic TLS. Annotate your Ingress with cert-manager.io/cluster-issuer: letsencrypt-prod for automatic HTTPS.

Prevention

  • Use PathType: Prefix for clean prefix-based routing.
  • Add rewrite-target annotations when backends expect root-relative paths.
  • Test each path with curl after applying Ingress changes.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro