Route traffic using ingress

8/11/2019

I had a working example of a project a year back, which is not working anymore.

It's basically related to change in the behavior of nginx.ingress.kubernetes.io/rewrite-target property mentioned here - https://github.com/kubernetes/ingress-nginx/tree/master/docs/examples/rewrite

I have 3 application and I want to route based on conditions.

  • /* to frontend-cluster-ip-service
  • /api/battleship/* to battleship-cluster-ip-service
  • /api/connect4/* to connect-four-cluster-ip-service

The working example that was working an year back was

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - http:
        paths:
          - path: /
            backend:
              serviceName: frontend-cluster-ip-service
              servicePort: 3000
          - path: /api/connect4/
            backend:
              serviceName: connect-four-cluster-ip-service
              servicePort: 8080
          - path: /api/battleship/
            backend:
              serviceName: battleship-cluster-ip-service
              servicePort: 8080

However, this is not working anymore and only routing to / , i.e to frontend-cluster-ip-service is working. Routing to other serives fails and I get 404.

Then I came to know about the change in nginx.ingress.kubernetes.io/rewrite-target.

I tried following then

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
    - http:
        paths:
          - path: /
            backend:
              serviceName: frontend-cluster-ip-service
              servicePort: 3000
          - path: /api/connect4(/|$)(.*)
            backend:
              serviceName: connect-four-cluster-ip-service
              servicePort: 8080
          - path: /api/battleship(/|$)(.*)
            backend:
              serviceName: battleship-cluster-ip-service
              servicePort: 8080

Now the routing to connect-four-cluster-ip-service and battleship-cluster-ip-service is working but frontend-cluster-ip-service is not working and few js files loads are showing error:

enter image description here

-- Dhananjay
kubernetes
kubernetes-ingress

1 Answer

8/11/2019

I had the same issue with a bit more complicated rewrite (it was only for one different path).

Making multiple Ingresses for each path worked for me but might not be the cleanest solution.

My ingress definition: https://github.com/FORTH-ICS-INSPIRE/artemis/blob/master/artemis-chart/templates/ingresses.yaml

-- Dimitrios Mavrommatis
Source: StackOverflow