maantarng
maantarng

Reputation: 171

Kubernetes ingress nginx route to same path (different service; different port)

I have some simple deployments, pods, services and nginx ingress in Kubernetes. I want to use ingress to route to the services (cluster-ip).

However, there are 2 services for 2 pods with the same path (i.e /abc/def). After I applied the ingress.yaml file, I got an error message saying "nginx: [emerg] duplicate location "/abc/def/" in /tmp/nginx/nginx-cfg728288520:2893".

May I know how to let ingress accepts the same paths with different service and different port?

Here is the ingress.yaml file:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
  name: ingress-nginx-default
  namespace: default
spec:
  rules:
  - host: 
    http:
      paths:
      - path: /abc/def/
        pathType: Prefix
        backend:
         service:
           name: service1
           port:
             number: 8090     
      - path: /abc/def
        backend:
          service:
            name: service2
            port:
             number: 8068
        pathType: Prefix
  ingressClassName: nginx

Upvotes: 0

Views: 1085

Answers (1)

Hemanth Kumar
Hemanth Kumar

Reputation: 3774

"nginx: [emerg] duplicate location "/abc/def/" in /tmp/nginx/nginx-cfg728288520:2893". This error indicates the same host with two same paths which is a duplicate location.

You can use simple fanout based on path or name based virtual hosting. In order to do this you need to have two hosts that need to be mentioned in the ingress.

based on your example you'd most likely want to have something like foo.bar.com and bar.foo.com. Here's the example from the Kubernetes docs:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: name-virtual-host-ingress
spec:
  rules:
    - host: foo.bar.com
      http:
        paths: #path_name
          - backend:
              serviceName: service1
              servicePort: 80
    - host: bar.foo.com
      http:
        paths: #path_name
          - backend:
              serviceName: service2
              servicePort: 80

Upvotes: 1

Related Questions