same path with different service names in ingress resources

12/12/2019

I'm having some issues when using a path to point to different service names, my ingress resource looks as below

nginx-static service is a nginx container which has static content... I have to load this static content while calling service-1, since both nginx-static and service-1 but I cannot keep the sme same host path.... Please suggest how to correct the below ingress resources...

kindly note static content has lot of files(csv,js,html,directories, files etc)

kind: Ingress
metadata:
  name: myingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
     - http:
        paths:
          - path: "/"
            backend:
              serviceName: nginx-static
              servicePort: 80
          - path: "/"
            backend:
              serviceName: service1
              servicePort: 8989
          - path: "/test1"
            backend:
              serviceName: service2
              servicePort: 9001

Any expert help is appreciated!!!

-- magic
kubernetes
kubernetes-ingress
nginx-ingress

2 Answers

12/12/2019

You cannot have the same path pointing to different backend resources. You have to put either your static files or the service into a different path, and rewrite the URL, for instance:

rewrite annotation:

metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default

paths:

- backend:
     serviceName: nginx-statix
     servicePort: 80
  path: /static(/|$)(.*)
- backend:
  path: /
     serviceName: service1

With this, your static content will be exposed under /static/, and all /static/name will be rewritten as /name by the ingress.

More info here: https://kubernetes.github.io/ingress-nginx/examples/rewrite/

-- Burak Serdar
Source: StackOverflow

12/24/2019

Unfortunately, requirements from the initial questions aren't' clear and there were no additional clarifications given. However, I'd like to elaborate on Burak Serdar's answer and add , that Kubernetes Ingress allows you listing the same path ad port for a multiple services under condition that you are listing these for different hosts.

foo.bar.com --|                 |-> foo.bar.com nginx-static:80
              | 178.91.123.132  |
bar.foo.com --|                 |-> bar.foo.com service1:8989

you can achieve that scenario with the following config:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: name-virtual-host-ingress
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - backend:
          serviceName: nginx-static
          servicePort: 80
  - host: bar.foo.com
    http:
      paths:
      - backend:
          serviceName: service2
          servicePort: 8989

However that would work only if you can split your web site between two hosts.

Hope that helps. You can check more on Ingress on K8s documentation.

-- Nick
Source: StackOverflow