<p>I have 3 services in 3 different namespaces I want my ingress rules to map to these backends, on path based routes.
Can someone please guide on the same.
I am using nginx ingress inside azure Kubernetes cluster.</p>

Abhishek Singh
<p>A basic example with an assumption that your <code>nginx ingress</code> is working correctly inside your <code>AKS</code> would be following:</p>
<p>List of <code>Pods</code> with their <code>Services</code>:</p>
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th>Pod</th>
<th>Namespace</th>
<th>Service name</th>
</tr>
</thead>
<tbody>
<tr>
<td>nginx</td>
<td>alpha</td>
<td>alpha-nginx</td>
</tr>
<tr>
<td>nginx</td>
<td>beta</td>
<td>beta-nginx</td>
</tr>
<tr>
<td>nginx</td>
<td>omega</td>
<td>omega-nginx</td>
</tr>
</tbody>
</table>
</div><hr />
<p><code>Ingress</code> definition for this particular setup:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: alpha-ingress
  namespace: alpha
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: nginx
  rules:
  - host: &quot;kubernetes.kruk.lan&quot; 
    http:
      paths:
      - path: /alpha(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: alpha-nginx
            port:
              number: 80
--- 
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: beta-ingress
  namespace: beta
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: nginx
  rules:
  - host: &quot;kubernetes.kruk.lan&quot; 
    http:
      paths:
      - path: /beta(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: beta-nginx
            port:
              number: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: omega-ingress
  namespace: omega
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: nginx
  rules:
  - host: &quot;kubernetes.kruk.lan&quot; 
    http:
      paths:
      - path: /omega(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: omega-nginx
            port:
              number: 80
</code></pre>
<p>In this example <code>Ingress</code> will analyze and rewrite the requests for the <strong>same domain name</strong> to send the traffic to different namespaces i.e. <code>alpha</code>, <code>beta</code>, <code>omega</code>.</p>
<p>When you've have finalized your <code>Ingress</code> resource, you can use <code>curl</code> to validate your configuration.</p>
<pre class="lang-bash prettyprint-override"><code>curl kubernetes.kruk.lan/alpha | grep -i &quot;&lt;h1&gt;&quot;                                    
&lt;h1&gt;Welcome to nginx from ALPHA namespace!&lt;/h1&gt;
</code></pre>
<pre class="lang-bash prettyprint-override"><code>curl kubernetes.kruk.lan/beta | grep -i &quot;&lt;h1&gt;&quot;                                    
&lt;h1&gt;Welcome to nginx from BETA namespace!&lt;/h1&gt;
</code></pre>
<pre class="lang-bash prettyprint-override"><code>curl kubernetes.kruk.lan/omega | grep -i &quot;&lt;h1&gt;&quot;                                    
&lt;h1&gt;Welcome to nginx from OMEGA namespace!&lt;/h1&gt;
</code></pre>
<p>I'd encourage you to check following docs on rewrites:</p>
<ul>
<li><em><a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="nofollow noreferrer">Kubernetes.github.io: Ingress-nginx: Examples: Rewrite</a></em></li>
</ul>
<hr />
<p>PS: <code>Pods</code> are default <code>nginx</code> containers/images with added text to <code>/usr/share/nginx/html/index.html</code></p>
