1

This might look like a duplicate but it isn't as the solution in the linked thread doesn't work for me.

I have Ingress configured to dispatch requests to different pods based on the path

Desired behavior:

public_ip/app1 -> pod1_ip:container1_port/
public_ip/app2 -> pod2_ip:container2_port/
public_ip/app3 -> pod3_ip:container3_port/

Actual behavior:

public_ip/app1 -> pod1_ip:container1_port/app1
public_ip/app2 -> pod2_ip:container2_port/app2
public_ip/app3 -> pod3_ip:container3_port/app3

So we get 404's on app1, app2, app3

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: some_name
  annotations:
    cert-manager.io/cluster-issuer: letsencrypt-prod
    acme.cert-manager.io/http01-edit-in-place: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  ingressClassName: "nginx"
  tls:
  - hosts:
    - some.host
    secretName: tls-cafe-ingress
  rules:
  - host: some.host
    http:
      paths:
      - path: /app1(/|$)(.*)
        backend:
          serviceName: app1
          servicePort: 1234
      - path: /app2(/|$)(.*)
        backend:
          serviceName: app2
          servicePort: 2345
      - path: /app3(/|$)(.*)
        backend:
          serviceName: app3
          servicePort: 3456

The problem is that Ingress igores the path specifications once there are regular expressions in it. This can be seen by checking the logs of Ingress:

k logs -n nginx-ingress ingress-pod-name

Here we can see hat nginx has requests to /appX in the log and tries to serve them form the local html folder, in other words, the path defined in the yaml are ignored.

If regexes are removed from the path it works but then the path is sent downstream to the target pod which breaks the application

Romeo Kienzler
  • 3,373
  • 3
  • 36
  • 58
  • 1
    can you elaborate more on " breaking of application" , I guess you are having liveness probes intact in your application, to take care of health of application – Tushar Mahajan Dec 11 '20 at 07:52
  • Can you please provide some more details: your k8s version, ingress type & version? Are there any specific config that I need to know before reproducing this? Can you also remove the `'..use-regex: "true"'` annotation and check if it works? – acid_fuji Dec 11 '20 at 12:49
  • Removing '..use-regex: "true"' doesn't make any different. Kube is v1.18.12+IKS (running in the IBM Kubernetes Free Tier. Nginx is 1.19.3, taken from https://helm.nginx.com/stable - we have a guess: maybe we have to take Ingress from https://github.com/kubernetes/ingress-nginx as only the "official" on supports those annotations..not sure, but we'll give it a try... – Romeo Kienzler Dec 13 '20 at 09:27
  • I've tested your ingress manifest and it works fine for me (i'm using the community nginx ingress controller). What is happening for you is that you are using nginxinc and trying to use annotation for community nginx which won`t work. What you can do is to switch to community nginx or change the way you annotate rewrite (see [here](https://github.com/nginxinc/kubernetes-ingress/tree/v1.9.1/examples/rewrites)). – acid_fuji Dec 14 '20 at 10:54
  • 1
    Thanks a lot. Now it works. Please feel free to put add an answer so I can accept and upvote – Romeo Kienzler Dec 15 '20 at 17:22
  • @RomeoKienzler apologize for late response. I have placed an answer. – acid_fuji Dec 21 '20 at 10:08
  • 1
    @thomas apologize for the late response as well, it's now accepted, again thanks so much – Romeo Kienzler Jan 06 '21 at 11:14

1 Answers1

1

There are two popular K8s Ingress controller that uses Nginx:

The key difference that apply in this case is the use of annotation. For the community ingress controller you use:

nginx.ingress.kubernetes.io/<annotation_type>

And for nginxinc uses:

nginx.org/<annoation_type> 

To check more about differences please visit this document.

acid_fuji
  • 6,287
  • 7
  • 22