How to expose nginx in Minikube to outside

9/29/2021

I have deployed Nginx in minikube in AWS EC2 Instance using the below commands:

kubectl create deployment --image=nginx nginx-app
kubectl expose deployment nginx-app --port=80 --name=nginx-http --type=NodePort

It is available on NodePort: 31568
I have added this port in Security Group and able to access it on browser in another laptop with http://EC2-PublicIP:31568

I have many microservices which are to be exposed to outside.

Is there any way to deploy an API Gateway (Nginx or Ingress) and expose on a port and should be able to access other microservices like

http://EC2-PublicIp:31568/helloworld          
http://EC2-PublicIp:31568/mainpage           
http://EC2-PublicIp:31568/editorspage  
       
etc

I have tried adding metallb, accordingly, an IP is allocated to ingress (load balancer type). I was able to access all the microservices specified in ingress.yaml only inside the EC2 Instance.

This should be accessible from the outside. If yes, then how to configure it?

Any help is appreciated.

-- Sri Durga
kubernetes
kubernetes-ingress
minikube
nginx
nginx-ingress

1 Answer

9/29/2021

You should be able to do so since you are using Nginx.
the configuration should like something like:

Annotation comes to help

The "trick" is to set annotations to support regexp & rewrite-target with:

  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
    #
    # the value can be set to  `/` or `$1` or `$2` and so on
    nginx.ingress.kubernetes.io/rewrite-target: "/$1"

This is the "important" annotation - the rewrite one

# Without a rewrite any request will return 404
nginx.ingress.kubernetes.io/rewrite-target: "/$1"

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress
  annotations:
    #
    # This is the expected line
    # 
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: "/$1"
spec:
  rules:
  - host: test.com # <- Set your host
    http:
      paths:
        #
        # List of desired paths
        # 
      - path: /path1
        backend:
          serviceName: nginx-http
          servicePort: 80
      - path: /path2/[A-Z0-9]{3}
        backend:
          serviceName: nginx-http
          servicePort: 80
-- CodeWizard
Source: StackOverflow