Expose port in minikube

In minikube, how to expose a service using nodeport ?

For example, I start a kubernetes cluster using the following command and create and expose a port like this:

$ minikube start
$ kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port=8080
$ kubectl expose deployment hello-minikube --type=NodePort
$ curl $(minikube service hello-minikube --url)
CLIENT VALUES:
client_address=192.168.99.1
command=GET
real path=/ ....

Now how to access the exposed service from the host? I guess the minikube node needs to be configured to expose this port as well.

129113 次浏览

minikube runs on something like 192.168.99.100. So you should be able to access it on the NodePort you exposed your service at. For eg, say your NodePort is 30080, then your service will be accessible as 192.168.99.100:30080.

To get the minikube ip, run the command minikube ip.

Update Sep 14 2017:

Here's a small example that works with minikube v0.16.0.

1) Run the commands below to create an nginx running on 8080 and a NodePort svc forwarding to it:

$ kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port=8080
deployment "hello-minikube" created
$ kubectl expose deployment hello-minikube --type=NodePort
service "hello-minikube" exposed

2) Find the nodeport used by the svc:

$ kubectl get svc hello-minikube
NAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
hello-minikube   10.0.0.76    <nodes>       8080:30341/TCP   4m

3) Find the minikube ip:

$ minikube ip
192.168.99.100

4) Talk to it with curl:

$ curl 192.168.99.100:30341
CLIENT VALUES:
client_address=172.17.0.1
command=GET
real path=/
...

I am not exactly sure what you are asking as it seems you already know about the minikube service <SERVICE_NAME> --url command which will give you a url where you can access the service. In order to open the exposed service, the minikube service <SERVICE_NAME> command can be used:

$ kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port=8080
deployment "hello-minikube" created
$ kubectl expose deployment hello-minikube --type=NodePort
service "hello-minikube" exposed
$ kubectl get svc
NAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
hello-minikube   10.0.0.102   <nodes>       8080/TCP   7s
kubernetes       10.0.0.1     <none>        443/TCP    13m


$ minikube service hello-minikube
Opening kubernetes service default/hello-minikube in default browser...

This command will open the specified service in your default browser.

There is also a --url option for printing the url of the service which is what gets opened in the browser:

$ minikube service hello-minikube --url
http://192.168.99.100:31167

As minikube is exposing access via nodeIP:nodePort and not on localhost:nodePort, you can get this working by using kubectl's port forwarding capability. For example, if you are running mongodb service:

kubectl port-forward svc/mongo 27017:27017

This would expose the service on localhost:27017, FWIW. Furthermore, you might want to figure out how to run this in background.

Just a note for anyone looking for connection refused answers: If your minikube does not run on "something like 192.168.99.100" you probably runned with another vm-driver like "none". In that case delete your minikube cluster and rebuild using the default. it 'll work....ish... I do not seem to be able to get the tunnel working...

I ran into a similar issue in 2022. Here are the commands I ran:

  1. kubectl create deployment deploymentName --image=dockerHubUsername/imageTag:imageVersion
  2. kubectl expose deployment deploymentName --type=LoadBalancer --port=8080
  3. minikube tunnel
  4. kubectl get services deploymentName this provides the external ip address needed to access the application. I access the app with 127.0.0.1:8080

Source