Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Last active May 1, 2022 07:47
Show Gist options
  • Save gilangvperdana/2912ced3f543f7dc7f909eebd103684e to your computer and use it in GitHub Desktop.
Save gilangvperdana/2912ced3f543f7dc7f909eebd103684e to your computer and use it in GitHub Desktop.
Expose Service with NodePort to Private Network Subnets Cluster on Minikube

Expose Service to Private Network with Minikube

You must have gotten a client where the client wants to see the results of the application work on Kubernetes, this we can use Nginx Proxy to reverse the IP service that is above Minikube to the IP private network around us. At least, if we can access it on our private network subnet, we can use the router to reverse private ip to public ip again.

Use Case

I want to expose my ReactJS apps to Private Network Subnet on Minikube. So user can access my ReactJS apps on Minikube with Private Network

I have an ReactJS apps run on Minikube with port 3000
I want to access on my_private_ip:80

Environment

Ubuntu 20.04LTS
Minikube with Kubernetes Cluster Running
Nginx on Host
Some Apps on Minikube
Kubectl

Lab

Make sure your Kubernetes Cluster on Minikube has been Running. Make sure your apps (Deployment) has been running on Kubernetes Cluster Minikube. Make sure you have install Nginx on your Hosts OS (Ubuntu). Don't forget you must activated minikube tunnel .

Expose to NodePort your Cluster IP Service

minikube tunnel
kubectl expose deployment client-depl --type=NodePort --port=3000

List your service NP

minikube ip
kubectl get service client-depl
Then write your minikube ip & Service node port. You can write like this:
minikube_ip:client_depl_nodeport
for my case, i have 192.168.49.2:30683

Configure Server Block Nginx

I want to access on my_private_ip:80 so i must edit my Nginx Server Block with Proxy_Pass.
nano /etc/nginx/sites-enabled/default
server {
    listen 80;
    server_name _;
location / {
       proxy_pass http://192.168.49.2:30683;
       proxy_http_version 1.1; 
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "upgrade";
    }
}

This is standard server block for non Header HTTP Framework

server {
    listen 80;
    server_name _;
location / {
       proxy_pass http://192.168.49.2:30683;
       proxy_http_version 1.1; 
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "upgrade";
    }
}

Access

See on your ubuntu network :
ifconfig
Access on your_private_ip:80

Expose with Ngrok

snap install ngrok
ngrok config add-authtoken random_auth_token
ngrok http 80
  • Access
access on Forwarding ngrok URL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment