Search code examples
kuberneteskubernetes-helmkubernetes-ingressnginx-ingress

Ingress path redirection, helm chart


I have a service which is running in kubernetes, and has a path prefix /api. Now I want to use Ingress to access it through the host address example.com/service1/ because I have multiple services. But the problem is that ingress redirects all the requests from path service1/ with that prefix service1/, but I want it to redirect from example.com/service1/ to my service with just / (so if I request example.com/service1/api it will redirect to service with just /api). Can I achieve something like this? I'm writing Ingress configuration in the helm chart of the service. Ingress configuration in service chart file values.yaml looks like this:

...
ingress:
  enabled: true
  className: ""
  annotations: {}
  # kubernetes.io/ingress.class: nginx // this comment was created when generating helm chart
  hosts:
    - host: example.com
      paths:
        - path: /service1(/|$)(.*)
          pathType: ImplementationSpecific
          backend:
            serviceName: $name
            servicePort: http
  tls: []
  ...

And ingress.yaml inside templates/ folder is a default file that was generated by helm when I was creating a chart for the service. It just uses values from values.yaml to configure ingress. I didn't find anything, only this question which is basically saying that I need to add either prefix service1/ to my service or just use /api in the Ingress configuration. But is there solution suitable for my needs?


Solution

  • Based on the solution provided in the comments (method 1 example 2 in the Medium post ), a possible values.yaml file for Ingress might looks like below.

    ...
    ingress:
      enabled: true
      className: ""
      annotations:
        kubernetes.io/ingress.class: nginx
        nginx.ingress.kubernetes.io/rewrite-target: http://example.com/$2 
      hosts:
        - host: example.com
          paths:
            - path: /service1(/|$)(.*)
              backend:
                serviceName: $name
                servicePort: http
      tls: []
      ...