<p>I want to deploy two version of my app on the same cluster. To do that I used namespace to separates them and each app have it's own ingress redirecting to it's own service. I use controller in my ingress.</p>

<p>To sum the architecture looks like this:</p>

<ul>
<li>cluster

<ul>
<li>namespace1

<ul>
<li>app1</li>
<li>service1</li>
<li>ingress1</li>
</ul></li>
<li>namespace

<ul>
<li>app2</li>
<li>service2</li>
<li>ingress2</li>
</ul></li>
</ul></li>
</ul>

<p>My problem is that when i'm using the external ip of the nginx-controller of the ingress2 it hits my app1</p>

<p>I'm using helm to deploy my app</p>

<p>Ingress.yaml</p>

<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: "{{ .Release.Name }}-ingress"
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    kubernetes.io/ingress.class: "nginx"
spec:
  tls:
    - hosts:
      - {{ .Values.host }}
      secretName: {{ .Release.Namespace }}-cert-secret
  rules:
  - http:
    - path: /api($|/)(.*)
      backend:
        serviceName: "{{ .Release.Name }}-api-service"
        servicePort: {{ .Values.api.service.port.api }}
</code></pre>

<p>service.yaml</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: "{{ .Release.Name }}-api-service"
spec:
  selector:
    app: "{{ .Release.Name }}-api-deployment"
  ports:
    - port: {{ .Values.api.service.port.api }}
      targetPort: {{ .Values.api.deployment.port.api }}
      name: 'api'
</code></pre>

<p>deployment.yaml</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: "{{ .Release.Name }}-api-deployment"
spec:
  replicas: 1
  selector:
    matchLabels:
      app: "{{ .Release.Name }}-api-deployment"
  template:
    metadata:
      labels:
        app: "{{ .Release.Name }}-api-deployment"
    spec:
      containers:
      - name: "{{ .Release.Name }}-api-deployment-container"
        imagePullPolicy: "{{ .Values.api.image.pullPolicy }}"
        image: "{{ .Values.api.image.repository }}:{{ .Values.api.image.tag }}"
        command: ["/bin/sh"]
        args:
        - "-c"
        - "node /app/server/app.js"
        env:
        - name: API_PORT
          value: {{ .Values.api.deployment.port.api | quote }}
</code></pre>

<p>values.yaml</p>

<pre><code>api:
  image:
    repository: xxx
    tag: xxx
    pullPoliciy: Always
  deployment:
    port:
      api: 8080
    ressources:
      requests:
        memory: "1024Mi"
        cpu: "1000m"
  service:
    port:
      api: 80
    type: LoadBalancer
</code></pre>

<p>To deploy my app i run:</p>

<ul>
<li><code>helm install -n namespace1 release1 .</code></li>
<li><code>helm install -n namespace2 release2 .</code></li>
</ul>

<p>kubectl -n namespace1 get svc</p>

<pre><code>NAME                                       TYPE           CLUSTER-IP      EXTERNAL-IP                                                               PORT(S)                                       AGE
nginx-ingress-1581005515-controller        LoadBalancer   10.100.20.183   a661e982f48fb11ea9e440eacdf86-1089217384.eu-west-3.elb.amazonaws.com   80:32256/TCP,443:32480/TCP                    37m
nginx-ingress-1581005515-default-backend   ClusterIP      10.100.199.97   &lt;none&gt;                                                                    80/TCP                                        37m
release1-api-service                       LoadBalancer   10.100.87.210   af6944a7b48fb11eaa3100ae77b6d-585994672.eu-west-3.elb.amazonaws.com    80:31436/TCP,8545:32715/TCP,30300:30643/TCP   33m
</code></pre>

<p>kubectl -n namespace2 get svc</p>

<pre><code>NAME                                       TYPE           CLUSTER-IP       EXTERNAL-IP                                                               PORT(S)                                       AGE
nginx-ingress-1580982483-controller        LoadBalancer   10.100.177.215   ac7d0091648c511ea9e440eacdf86-762232273.eu-west-3.elb.amazonaws.com    80:32617/TCP,443:30459/TCP                    7h6m
nginx-ingress-1580982483-default-backend   ClusterIP      10.100.53.245    &lt;none&gt;                                                                    80/TCP                                        7h6m
release2-api-service                       LoadBalancer   10.100.108.190   a4605dedc490111ea9e440eacdf86-2005327771.eu-west-3.elb.amazonaws.com   80:32680/TCP,8545:32126/TCP,30300:30135/TCP   36s
</code></pre>

<p>When I hit the nginx-controller of the namespace2 it should hit app2 deployed in the release2 but instead it hits app1.
When I hit the nginx-controller of the namespace1, as expected it hit app1.</p>

<p>Just for infos the order is important, it's always the first deployed app that is always hit</p>

<p>Why does the second load balancer isn't redirecting to my second application ?</p>

Noé
<p>The problem is that I was using the same "nginx" class for both ingress. Both nginx controller was serving the same class "nginx".</p>

<p>Here is the wiki of how to use mutilple nginx ingress controller: <a href="https://kubernetes.github.io/ingress-nginx/user-guide/multiple-ingress/" rel="nofollow noreferrer">https://kubernetes.github.io/ingress-nginx/user-guide/multiple-ingress/</a></p>

<p>I end up defining my ingress class like this:
<code>kubernetes.io/ingress.class: nginx-{{ .Release.Namespace }}</code></p>

<p>And deploying my nginx controller like this: <code>install -n $namespace nginx-$namespace stable/nginx-ingress --set "controller.ingressClass=nginx-${namespace}"</code></p>

<p><em>If you're not using helm to deploy your nginx-controller what you need to modify is the nginx ingress class</em></p>
