How do I create an Endpoint for an external service running on the same localhost outside of K8s cluster?

1/12/2020

I am using Docker for Windows (docker-desktop) which ships with a small single node kubernetes instance. I have a scenario where my pods needs to communicate with some external services running on the same localhost(windwos 10 machine), but outside of the k8s cluster.

I know that I can use kubernetes.docker.internal from within the cluster to reach my node/localhost. But unfortunately the pods has some default connection string within the image which I don't want to change - say the pods are by default trying to connect to a dns string - "my-server". So in my scenario, I want to define a K8s service with name "my-server" which has an Endpoint reference to kubernetes.docker.internal so that the kube-proxy will route that correctly to my localhost which is my windows 10 machine.

Is this somehow possible? I have already checked this solution, but it talks about external services running on some other node or cloud. I am also considering the local machine hostname as an ExternalName, but this is not entirely reliable in dns resolution for my usecase. So I really want to use the kubernetes.docker.internal as a service endpoint. Any thoughts?

-- Vipin P
docker
docker-for-windows
kube-proxy
kubernetes
kubernetes-service

1 Answer

1/17/2020

I know that I can use kubernetes.docker.internal from within the cluster to reach my node/localhost.

If you're sure you can reach your windows machine via this address, there are some solutions.

Kubernetes allows you to modify /etc/hosts file in any Pod. I assume kubernetes.docker.internal resolves to some IP. If you know this IP you can add it to /etc/hosts file used by containers in a Pod. This way can map any domain name you want to this IP. For details, please refer to this article in kubernetes documentation.


If for some reason IP cannot be used in this case, there is one more solution. You can modify the default behaviour of your core-dns by using rewrite plugin. It's nicely described in this article.

-- mario
Source: StackOverflow