skip to Main Content

I want to create an nginx load balancer in kubernetes.

    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
                  

So I have created services and pods on cluster. And now I want to create an ingress to access my servies using rewrite.

  • http://ip-of-cubernetes (main mape)
  • http://ip-of-cubernetes/catalog (list prodcts)
  • http://ip-of-cubernetes/catalog/112 (single prodcut)

When I send this nginx yml file using kubectl command "kubectl apply -f ingress.yml" 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.
enter image description here

if I use following helm command in google management page console, it creates a loadbalancer in services tab.

 helm install nginx-ingress nginx-stable/nginx-ingress --set rbac.create=true 

enter image description here

So I want to create an nginx loadbalancer in services tab or I want to use rewrite rules in gce loadbalancers.

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.

2

Answers


  1. In this link one of the use case is rewrite request URI before sending it to application.

    There is also information about yaml deployment in ingress and in service that you can use as a guidance in your deployment.

    Login or Signup to reply.
  2. So you have installed the ingress controller which will manage the ingress objects.

    Here is official link you can refer for more details : https://cloud.google.com/community/tutorials/nginx-ingress-gke

    Make sure your ingress pointing to proper ingress class if you are using the Nginx or GCE any other controller :

    Example :

    apiVersion:  networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: foo-external
      annotations:
        kubernetes.io/ingress.class: "nginx"
    spec:
      rules:
    

    You can create the ingress object file

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ingress-resource
      annotations:
        kubernetes.io/ingress.class: "nginx"
        nginx.ingress.kubernetes.io/ssl-redirect: "false"
    spec:
      rules:
      - host: example.com
        http:
          paths:
          - pathType: Prefix
            path: /
            backend:
              service:
                name: service-1
                port:
                  number: 8080
    

    So if you have a single service you can use the above ingress. So any request at example.com will get redirected to service-1

    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: "nginx"
            nginx.ingress.kubernetes.io/ssl-redirect: "false"
        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
    

    Sharing here my other routing example which you can refer for rewriting the path if you required it.

    Ref : https://stackoverflow.com/a/75433607/5525824

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search