6
votes

I am using Kubernetes Engine on the Google Cloud Platform. I have a pod running a process in a Docker scratch container. I also have a load balancer service that gives me access to the pod from the outside world.

The process running in the pod needs to know what its external IP address is. How can I get this?

Prior to using Kubernetes Engine I was using Compute Engine and could find the external IP address by the following:

curl -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip

Are there any internal tools I can use that would be available to my process? Or would I need the process to call an external site that can mirror back the IP address?

1

1 Answers

3
votes

Every Pod (unless configured not to do so) has valid kubernetes credentials in /var/run/secrets/kubernetes.io/servicetoken/token as described here so the answer is to use the kubernetes API to ask the Service in front of the Pod(s) for its status:loadBalancer:ingress:ip: as described here which I have every reason to believe GKE will keep up-to-date with any changes to the load balancer. The kubernetes API is always(?) located at https://kubernetes (that's normally enough, or https://kubernetes.default.svc.cluster.local is its full name), so there should be very little configuration the Pod would need in order to carry out the lookup.

The asterisk to that response is that one must provide the name of the Service to the Pod(s) of the Service sitting in front of it, because (for the most part) there is no way for the Pod to know how many Services point to it.