Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing an internal Kubernetes IP address to the host system

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?

like image 540
keyboardsamurai Avatar asked Feb 16 '17 08:02

keyboardsamurai


People also ask

How do I get the IP for Kubernetes host?

To find the cluster IP address of a Kubernetes pod, use the kubectl get pod command on your local machine, with the option -o wide . This option will list more information, including the node the pod resides on, and the pod's cluster IP. The IP column will contain the internal cluster IP address for each pod.

How does routing work in Kubernetes?

If the service IP is used, the kube-proxy performs load-balancing and DNAT, translates the destination IP to the remote Pod's IP. The routing table on the node determines where the packets should be routed. – If the destination is a local Pod on the same node, the packet is forwarded directly to the Pod's interface.

How do I route traffic in Kubernetes?

you need to examine the allocated port after creating the service and on most hosts, you need to open the relevant port in firewall after the service creation. The final and the most recommended approach to routing traffic to your Kubernetes service is 'ClusterIP' service type.


2 Answers

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.

like image 80
Derrick J Wippler Avatar answered Oct 22 '22 12:10

Derrick J Wippler


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

like image 32
aaron-prindle Avatar answered Oct 22 '22 12:10

aaron-prindle