<p>I want to create an nginx load balancer in kubernetes.</p>
<pre><code>    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ecom-ingress
    spec:
      defaultBackend:
        service:
          name: ecom-ui-service
          port:
            number: 80
      rules:
      - http:
          paths:
          - path: /identity
            pathType: ImplementationSpecific
            backend:
              service:
                name: ecom-identity-service
                port:
                  number: 80
          - path: /catalog
            pathType: ImplementationSpecific
            backend:
              service:
                name: ecom-catalog-service
                port:
                  number: 80
                  
</code></pre>
<p>So I have created services and pods on cluster. And now I want to create an ingress to access my servies using rewrite.</p>
<ul>
<li>http://ip-of-cubernetes    (main mape)</li>
<li>http://ip-of-cubernetes/catalog    (list prodcts)</li>
<li>http://ip-of-cubernetes/catalog/112    (single prodcut)</li>
</ul>
<p>When I send this nginx yml file using kubectl command &quot;kubectl apply -f ingress.yml&quot; it creates a gce load balancer in ingress tab of google page like following. And I can not access the catalog endpoint. It returns nginx 404 page.
<a href="https://i.stack.imgur.com/KuMd2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KuMd2.png" alt="enter image description here" /></a></p>
<p>if I use following helm command in google management page console, it creates a loadbalancer in services tab.</p>
<pre><code> helm install nginx-ingress nginx-stable/nginx-ingress --set rbac.create=true 
</code></pre>
<p><a href="https://i.stack.imgur.com/UxfIp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UxfIp.png" alt="enter image description here" /></a></p>
<p>So I want to create an nginx loadbalancer in services tab or I want to use rewrite rules in gce loadbalancers.</p>
<p>How can I specify my ingress yml file for this purpose? And I want to use the rewrite for use my services to use query strings etc.</p>

barteloma
<p>So you have installed the ingress controller which will manage the ingress objects.</p>
<p>Here is official link you can refer for more details : <a href="https://cloud.google.com/community/tutorials/nginx-ingress-gke" rel="nofollow noreferrer">https://cloud.google.com/community/tutorials/nginx-ingress-gke</a></p>
<p>Make sure your ingress pointing to proper <strong>ingress class</strong> if you are using the <strong>Nginx</strong> or GCE any other controller :</p>
<p><strong>Example</strong> :</p>
<pre><code>apiVersion:  networking.k8s.io/v1
kind: Ingress
metadata:
  name: foo-external
  annotations:
    kubernetes.io/ingress.class: &quot;nginx&quot;
spec:
  rules:
</code></pre>
<p>You can create the <strong>ingress</strong> <strong>object</strong> file</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-resource
  annotations:
    kubernetes.io/ingress.class: &quot;nginx&quot;
    nginx.ingress.kubernetes.io/ssl-redirect: &quot;false&quot;
spec:
  rules:
  - host: example.com
    http:
      paths:
      - pathType: Prefix
        path: /
        backend:
          service:
            name: service-1
            port:
              number: 8080
</code></pre>
<p>So if you have a <strong>single service</strong> you can use the above ingress. So any request at <strong>example.com</strong> will get redirected to <strong>service-1</strong></p>
<pre><code>http://ip-of-cubernetes (main page)
http://ip-of-cubernetes/catalog (list prodcts)
http://ip-of-cubernetes/catalog/112 (single product)


    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ingress-resource
      annotations:
        kubernetes.io/ingress.class: &quot;nginx&quot;
        nginx.ingress.kubernetes.io/ssl-redirect: &quot;false&quot;
    spec:
      rules:
      - host: example.com
        http:
          paths:
          - pathType: Prefix
            path: /
            backend:
              service:
                name: service-1
                port:
                  number: 8080
          - pathType: Prefix
            path: /catalog
            backend:
              service:
                name: service-2
                port:
                  number: 8080
</code></pre>
<p>Sharing here my other routing example which you can refer for rewriting the path if you required it.</p>
<p>Ref : <a href="https://stackoverflow.com/a/75433607/5525824">https://stackoverflow.com/a/75433607/5525824</a></p>
