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.

Image for post
Image for post

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

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

Image for post
Image for post
nginx in k8s exposed via ngrok

…. 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


You should see the dashboard

Image for post
Image for post
ngrok dashboard

Currently working with Kafka, Databases, Azure, Kubernetes and related open source projects | Confluent Community Catalyst (for Kafka)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store