How to expose Minikube cluster to internet

I know minikube should be used for local only, but i'd like to create a test environment for my applications.
In order to do that, I wish to expose my applications running inside the minikube cluster to external access (from any device on public internet - like a 4G smartphone).

note : I run minikube with --driver=docker

kubectl get services

NAME      TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
web8080   NodePort   10.99.39.162   <none>        8080:31613/TCP   3d1h

minikube ip

192.168.49.2

One way to do it is as follows :

firewall-cmd --add-port=8081/tcp
kubectl port-forward --address 0.0.0.0 services/web8080 8081:8080

then I can access it using :

curl localhost:8081      (directly from the machine running the cluster inside a VM)
curl 192.168.x.xx:8081   (from my Mac in same network - this is the private ip of the machine running the cluster inside a VM)
curl 84.xxx.xxx.xxx:8081 (from a phone connected in 4G - this is the public ip exposed by my router)

I don't want to use this solution because kubectl port-forward is weak and need to be run every time the port-forwarding is no longer active.

How can I achieve this ?

(EDITED) - USING LOADBALANCER

when using LoadBalancer type and minikube tunnel, I can expose the service only inside the machine running the cluster.

kubectl get services

NAME         TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)          AGE
my-service   LoadBalancer   10.111.61.218   10.111.61.218   8080:31831/TCP   3d3h

curl 10.111.61.218:8080 (inside the machine running the cluster) is working
but curl 192.168.x.xx:8080 (from my Mac on same LAN) is not working

Thanks


Minikube as a development tool for a single node Kubernetes cluster provides inherent isolation layer between Kubernetes and the external devices (being specific the inbound traffic to your cluster from LAN/WAN).

Different --drivers are allowing for flexibility when it comes to the place where your Kubernetes cluster will be spawned and how it will behave network wise.

A side note (workaround)!

As your minikube already resides in a VM and uses --driver=docker you could try to use --driver=none (you will be able to curl VM_IP:NodePort from the LAN). It will spawn your Kubernetes cluster directly on the VM.

Consider checking it's documentation as there are some certain limitations/disadvantages:

  • Minikube.sigs.k8s.io: Docs: Drivers: None

As this setup is already basing on the VM (with unknown hypervisor) and the cluster is intended to be exposed outside of your LAN, I suggest you going with the production-ready setup. This will inherently eliminate the connectivity issues you are facing. Kubernetes cluster will be provisioned directly on a VM and not in the Docker container.

Explaining the --driver=docker used: It will spawn a container on a host system with Kubernetes inside of it. Inside of this container, Docker will be used once again to spawn the necessary Pods to run the Kubernetes cluster.

As for the tools to provision your Kubernetes cluster you will need to chose the option that suits your needs the most. Some of them are the following:

  • Kubeadm
  • Kubespray
  • MicroK8S

After you created your Kubernetes cluster on a VM you could forward the traffic from your router directly to your VM.


Additional resources that you might find useful:

  • Stackoverflow.com: Questions Expose Kubernetes cluster to the Internet (Virtualbox with minikube)

curl $(minikube ip):$NODE_PORT : Now we can test that the app is exposed outside of the cluster using curl, the IP of the Node and the externally exposed port.

For you : curl 192.168.49.2:31613


Use nginx reverse-proxy https://www.zepworks.com/posts/access-minikube-remotely-kvm/

install nginx, then in nginx.conf add this

stream {
    server {
        listen 8081;
        proxy_pass  192.168.49.2:8080;
    }
}

restart nginx