kubernetesnginxkubernetes-ingresstraefikk3s

Kubernetes Ingress Path Rewrite


I'm learning Kubernetes, using K3S. Due to some project restrictions, I am using Traefik ingress. (Previously I was using the nginx ingress.)

I'm trying to deploy a few pods behind the ingress, using a "fanout" approach. For example, I have to containers, CX and CY, each with its own http endpoints, defined without a prefix, e.g. /api, /data, /, etc. Both containers will run at the same host. The default kubernetes behavior is to forward the whole path to the container, so if I have CX at /CX, then when I ping url/CX - the container sees path /CX - which means I would have to redefine all paths in the container to be /CX/api, /CX/data, etc.

When I was using nginx, I had access to:

ingress:
  enabled: true
  className: "nginx"
  #className: "traefik"
  annotations:
      nginx.ingress.kubernetes.io/use-regex: "true"
      nginx.ingress.kubernetes.io/rewrite-target: /$2
  hosts:
    - host:
      paths:
        - path: /postgres(/|$)(.*)
          pathType: ImplementationSpecific

Which would rewrite the path. Traefik has middleware for this (but is difficult to get working.)

Am I missing a fundamental design principle for http services designed for Kubernetes? This seems like such a basic issue to me, but support for path rewrite is fairly limited. Am I supposed to be designing my containers, pods, or services differently?


Solution

  • You have 2 option.

    1. Use your ingress controllers features to rewrite your path. in this case traefik middleware.

    2. Define new DNS record for every APP you have like CX.yourdomain.com and use those address as ingress hosts directly without path rewrites.