· Travis Rodgers · Kubernetes  · 2 min read

How to Create an IngressRoute With Multiple Paths

When working with Traefik as an ingress controller in Kubernetes, configuring an IngressRoute with multiple paths (or matches) is a common requirement. However, the official documentation doesn’t always provide clear examples for this specific use case.

In this guide, I’ll walk you through creating an IngressRoute with multiple paths and compare it with a standard Kubernetes Ingress for better understanding.

Understanding the Traefik IngressRoute

Traefik uses IngressRoute CRDs (Custom Resource Definitions) instead of the standard Kubernetes Ingress resource. This provides greater flexibility, allowing more advanced routing capabilities.

Example: Traefik IngressRoute with Multiple Paths

Below is an example of an IngressRoute that routes traffic to different services based on the request path:

apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
  name: my-ingressroute
  namespace: default
spec:
  entryPoints:
    - websecure # Change to "web" if using HTTP
  routes:
    - match: Host(`example.com`) && PathPrefix(`/api`)
      kind: Rule
      services:
        - name: api-service
          port: 80
    - match: Host(`example.com`) && PathPrefix(`/app`)
      kind: Rule
      services:
        - name: app-service
          port: 80
    - match: Host(`example.com`) && PathPrefix(`/auth`)
      kind: Rule
      services:
        - name: auth-service
          port: 80

Breakdown:

  • entryPoints: Specifies whether traffic is handled over HTTP (web) or HTTPS (websecure).
  • routes: Defines multiple path-based rules for routing traffic to different services.
  • match conditions: Each rule matches a combination of Host and PathPrefix, ensuring traffic is directed correctly.
  • services: Each rule forwards traffic to a different backend service.

Standard Kubernetes Ingress Example

If you are using the default Kubernetes Ingress instead of Traefik’s custom resource, you can achieve similar functionality like this:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
    - host: example.com
      http:
        paths:
          - path: /api
            pathType: Prefix
            backend:
              service:
                name: api-service
                port:
                  number: 80
          - path: /app
            pathType: Prefix
            backend:
              service:
                name: app-service
                port:
                  number: 80
          - path: /auth
            pathType: Prefix
            backend:
              service:
                name: auth-service
                port:
                  number: 80

Key Differences Between IngressRoute and Standard Ingress:

FeatureTraefik IngressRouteStandard Kubernetes Ingress
Resource TypeCustom Resource (CRD)Built-in Kubernetes Resource
Path MatchingUses match rulesUses pathType with Prefix/Exact
Middleware SupportNative and flexibleRequires annotations
Advanced RoutingSupports weighted routing, retries, circuit breakersBasic routing only

Conclusion

If you’re using Traefik as your ingress controller, IngressRoute provides greater flexibility and control over your routing rules. However, if you’re working in a more standard Kubernetes environment without CRDs, the built-in Ingress resource works just fine.

Both approaches allow you to efficiently route traffic based on URL paths, making it easy to manage multiple services under a single domain.

    Share:

    Related Posts

    View All Posts »