Routing an internal Kubernetes IP address to the host system

12,169

Solution 1

You can add a route to the k8 internal network from localhost

Add a route to the internal network using the minikube ip address

$ sudo ip route add 172.17.0.0/16 via $(minikube ip)  # linux
$ sudo route -n add 172.17.0.0/16 $(minikube ip) # OSX

your subnet mask could be found using kubectl get service command

Test the route by deploying a test container and connect to it from localhost

$ kubectl run monolith --image=kelseyhightower/monolith:1.0.0 --port=80
$ IP=$(kubectl get pod  -l run=monolith -o jsonpath='{.items[0].status.podIP }')
$ curl http://$IP
{"message":"Hello"}

You can also add a route to K8 master

sudo route -n add 10.0.0.0/24 $(minikube ip)

This is only useful for local development, you should use NodePort or LoadBalancer for exposing pods in production.

Solution 2

If I understand correctly: You are trying to expose a server from within minikube to your host network. This can be done a few ways:

The first is to create a NodePort Service for your server/pod. You can then run minikube service list to get the url for your service:

$ minikube service list
|-------------|----------------------|-----------------------------|
|  NAMESPACE  |         NAME         |             URL             |
|-------------|----------------------|-----------------------------|
| default     | kubernetes           | No node port                |
| default     | <your-service>       | http://192.168.99.100:<port>|
| kube-system | kube-dns             | No node port                |
| kube-system | kubernetes-dashboard | http://192.168.99.100:30000 |
|-------------|----------------------|-----------------------------|

The second is to use kubectl proxy and proxy the port you want to your local machine. This method does not require you to create a service, it should work with your current configuration.

 kubectl proxy --port=<port-you-want-access-on-server>

This will then make the proxied port available at localhost:port

If you are just trying to get the IP address of a pod, this command should work (from How to know a Pod's own IP address from a container in the Pod?):

kubectl get pod $POD_NAME --template={{.status.podIP}}

Also if you just need to access minikube's internal network you can use:

minikube ssh

Which will drop you into minikube's VM

Share:
12,169
keyboardsamurai
Author by

keyboardsamurai

Updated on June 13, 2022

Comments

  • keyboardsamurai
    keyboardsamurai almost 2 years

    While running Minikube, I want to connect to a server that has the annoying habit of announcing itself to a service registry with its internal IP address from inside its pod.

    However for legacy reasons I have to connect to this registry first and retrieve that server's ip address from it. The only way to access this server from my dev machine, it seems to me, is bridging to the internal network, so I can access the networking of the Minikube. Is there an easy way to do this?

  • Peter
    Peter over 6 years
    This doen't work any more with the minikube 0.24 because the subnetmask of the kubernetes overlay network has changed - 10.0.0.0/8 I guess.
  • Peter
    Peter over 6 years
    It's 10.96.0.0/12 see constant.go