How to access a kubernetes service in GCP by name from a vm?

6/18/2019

I have a load balancing kubernetes service hosted in gcp called myService in the namespace myNamespace. I've given it a tag of 'myKubeTag'. I also have a vm running which itself is running a docker container that wants to communicate with the myService. I've created a firewall rule to allow tcp traffic between the two.

I can give the IP address of the service to the vm to connect, what I'm wondering is, how do I set up a name proxy, so that the vm can just reference it by name?

The workload and service are launched from helm, and look like this:

apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: {{ template "app.prefix" . }}
spec:
  selector:
    matchLabels:
      name: {{ template "app.prefix" . }}
  template:
    metadata:
      labels:
        name: {{ template "app.prefix" . }}
    spec:
      nodeSelector:
        target_workload: {{ .Values.prefix }}-myApp
      containers:
        - name: myApp
          image: {{ .Values.myApp.image }}
      tolerations:
        - key: "target_workload"
          operator: "Equal"
          value: {{ .Values.prefix }}-myApp
          effect: "NoSchedule"

---

apiVersion: v1
kind: Service
metadata:
  name: {{ template "app.prefix" . }}
spec:
  ports:
    - name: http
      protocol: TCP
      port: 8080
      targetPort: 8080
  type: LoadBalancer
  selector:
    role: {{ template "app.prefix" . }}

I've ready the following: https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/

which makes me think that the host name would be myService.myNamespace.sxc.cluster-domain.myCluster, however the container running on my VM is throwing an Unknown host exception.

-- Andy
google-cloud-platform
hostname
kubernetes
networking

1 Answer

6/19/2019

Once you use Service type: LoadBalancer inside GCP it automatically assigns you an ExternalIP:

$ kubectl get svc
NAME         TYPE           CLUSTER-IP     EXTERNAL-IP    PORT(S)        AGE
kubernetes   ClusterIP      10.12.0.1      <none>         443/TCP        162m
nginx-svc    LoadBalancer   10.12.15.100   35.232.130.5   80:30826/TCP   8m27s

In this example domains looks like the following 5.130.232.35.bc.googleusercontent.com

I've checked that by doing a nslookup from inside the nginx pod

$ kubectl exec nginx-deployment-85c9bf4fd7-h5tt9 nslookup 35.232.130.5
Server:         10.12.0.10
Address:        10.12.0.10#53

Non-authoritative answer:
5.130.232.35.in-addr.arpa       name = 5.130.232.35.bc.googleusercontent.com.

Keep in mind there is no nslookup inside default nginx image so you will need to install it.

Hope this is what you were looking for.

-- Crou
Source: StackOverflow