Expose Kubernetes services with ngrok
Very often, there is a need to expose Kubernetes apps running in minikube
to the public internet. This post just provides a manual, yet quick and dirty hack for this using ngrok
(it’s probably been done before, but here it goes anyway)
We’ll use a simple nginx
app to test things out i.e. we’ll expose an nginx server (running as a single replica Kubernetes Deployment) as a publicly accessible URL.
Test app (nginx)
Start by creating the nginx Deployment
kubectl apply -f https://raw.githubusercontent.com/abhirockzz/ngrok-kubernetes/master/nginx-deployment.yaml
Expose it inside the cluster by creating a corresponding Service
(typeClusterIP
)
kubectl apply -f https://raw.githubusercontent.com/abhirockzz/ngrok-kubernetes/master/nginx-service.yaml
Enter ngrok..
To expose the nginx Service
we just created, we can create a ngrok deployment which will run the ngrok process with an HTTP tunnel to the nginx Service
(using the Service
name)
Create the ngrok Deployment
kubectl apply -f https://raw.githubusercontent.com/abhirockzz/ngrok-kubernetes/master/ngrok-deployment.yaml
Now you need to extract the ngrok URL. The below command does a couple of things
- gets the Pod for the
ngrok
Deployment - uses the HTTP endpoint inside of the ngrok Pod to get the details (using
kubectl exec
inside the running Pod)
kubectl exec $(kubectl get pods -l=app=ngrok -o=jsonpath='{.items[0].metadata.name}') -- curl http://localhost:4040/api/tunnels
Output will be similar to what you see below — refer to the public_url
field to grab the ngrok URL accessible via public internet (in this example, it’s https://b42658ec.ngrok.io
)
Access the URL and it should lead you to ngnix
home page
Optionally….
…. if you want to access the ngrok dashboard
Expose it using aService
(type NodePort
)
kubectl apply -f https://raw.githubusercontent.com/abhirockzz/ngrok-kubernetes/master/ngrok-service.yaml
Check the random port
kubectl get svc ngrok-service -o=jsonpath='{.spec.ports[?(@.port==4040)].nodePort}'//e.g. 30552
Get the Minikube IP
$ minikube ip192.168.99.100
Open it in your browser
http://<minikube-ip>:<service-node-port>/statuse.g. http://192.168.99.100:30552/status
You should see the dashboard
That’s all there is to it. The code (just a bunch of YAMLs) is on GitHub
Cheers!