Connecting from K8S pod in GKE to a VM internal IP on Google Cloud Platform

3/28/2018

We have a requirement to connect from a POD in GKE to service running on a VM on it's internal IP address.

enter image description here

The K8s cluster and the VM are on different network so we setup VPC Peering between these nets:

enter image description here

As how to point to an external IP, we applied a service without a selector as discussed here:

https://kubernetes.io/docs/concepts/services-networking/service/#services-without-selectors

The POD should connect to the internal IP of the VM through this service, the service and endpoint description is:

kubectl describe svc vm-proxy

Name:              vm-proxy
Namespace:         test-environment
Labels:            <none>
Annotations:       <none>
Selector:          <none>
Type:              ClusterIP
IP:                10.59.251.146
Port:              <unset>  8080/TCP
TargetPort:        8080/TCP
Endpoints:         10.164.0.10:8080
Session Affinity:  None
Events:            <none>

Whereby the Endpoint, the internal IP of the VM is, en the Service IP is allocated by K8s.

The pod simply sets up an http connection to the IP of the Service, but connection is re-fused. (Connection timeout eventually).

The use case is pretty straightforward, and documented on k8s documentation, giving the example of connecting to a DB running on a VM. However it doesn't work in our case, and we are not sure if our setup is wrong or this is simply not possible, using an internal IP of a VM.

-- Christophe Bouhier
google-cloud-platform
kubernetes
virtual-machine
vpc

1 Answer

4/2/2018

I reproduced your issue and it worked fine for me. This is what I did:

  1. Create 2 networks (one of them (demo) on 172.16.0.0/16, the other one is my default network, set on 10.132.0.0/20).
  2. Set up VPC peering.
  3. Created a VM in demo network. It got assigned 172.16.0.2
  4. Created the service as you described (with the endpoint pointing to 172.16.0.2).
  5. curl from the pod to the service IP and got 200!

If the steps are right, but your configuration is not working, I'd like to know your network IP ranges. Both of them.

-- suren
Source: StackOverflow