When using a nginx kubernetes routing LoadBalancer with path redirects, why can I not access my service correctly?

4/30/2020

I am using AKS with Helm v2.2 to try deploying a chart that utilizes an nginx LoadBalancer Pod to control all ingress into my services via a single ip address. This is very much in the experimental phase but I have proven that when I use the following Helm ingress configuration for my .net core webapi service:

ingress:
  enabled: true
  annotations:
    kubernetes.io/ingress.class: nginx
  hosts:
    - host:
      paths:
        - /

That I can indeed then visit my exposed api and see the swagger ui at

http://[My External IP]/index.html 

what I then want to do is place several services behind the same LoadBalancer (as you are intended to) so my expectations were that I could then change the above service configuration to something like this:

ingress:
  enabled: true
  annotations:
    kubernetes.io/ingress.class: nginx
  hosts:
    - host:
      paths:
        - /serviceA

Which should then mean I can access the same service via the now updated url:

http://[My External IP]/serviceA/index.html

Is this what I should be expecting to work? Do I need to use any sort of re-write system as so far I get errors back from this url saying that it cannot find certain missing resources. Any attempts at using the re-write annotation have not resulted in helping me here either. Could someone help me out and point out what I may be doing wrong? With the new url path I end up with the following types of errors on what appears to be the files that the index.html page is trying to load suggesting it is half working but needs some re-writing or something?

Failed to load resource: the server responded with a status of 404 ()

As a result of the Helm chart template engine the following ingress yaml file is created:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: myrelease-release-manager
  labels:
    app.kubernetes.io/name: release-manager
    helm.sh/chart: release-manager-0.1.0
    app.kubernetes.io/instance: release-name
    app.kubernetes.io/version: "1.0"
    app.kubernetes.io/managed-by: Tiller
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /serviceA
    nginx.ingress.kubernetes.io/ssl-redirect: "false"

spec:
  rules:
    - host:
      http:
        paths:
          - path: /serviceA
            backend:
              serviceName: myrelease-release-manager
              servicePort: 80

As a result of this ingress file I want to visit this service when I go to my external ip address with the path /serviceA/index.html.

-- The Senator
azure-aks
kubernetes
kubernetes-helm
nginx-ingress

1 Answer

5/4/2020

Close, you need to update the rewrite target to /$2

nginx.ingress.kubernetes.io/rewrite-target: /$2

Rewrites

/serviceB/foo -> /foo

/serviceA/foo -> /foo

But each one will be directed to the services for that path

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: myrelease-release-manager
  labels:
    app.kubernetes.io/name: release-manager
    helm.sh/chart: release-manager-0.1.0
    app.kubernetes.io/instance: release-name
    app.kubernetes.io/version: "1.0"
    app.kubernetes.io/managed-by: Tiller
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
 spec:
   rules:
   - http:
       paths:
       - backend:
           serviceName: serviceB
           servicePort: 80
         path: /serviceB(/|$)(.*)
       - backend:
           serviceName: serviceA
           servicePort: 80
         path: /serviceA(/|$)(.*)
-- strongjz
Source: StackOverflow