0
0
Kubernetesdevops~5 mins

Path-based routing in Kubernetes - Commands & Configuration

Choose your learning style9 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.