Search code examples
kubernetesgoogle-cloud-platformgoogle-kubernetes-enginestackdrivergoogle-cloud-stackdriver

Fetch the zone of the master node from a pod in container engine


For a multi zoned, multi cluster setup. Is it possible to dynamically expose or retrieve the zone in which the master node is running, from a pod? Since this is needed to correctly push our metrics to stackdriver, in order to run a horizontal pod autoscaler based on them.

I can hardcode it in the individual deployments but I would like to avoid that.

I've tried looking in the compute internal metadata endpoint, and in the reference variables kubernetes has, but none seem to expose the zone of the master.


Solution

  • There is an unofficial way to determine what zone (or region) the master is running in by parsing the kube-env metadata entry on a node. kube-env is a key-value store where keys are all uppercase, followed by a colon, which is followed by the value. If you look for the key ZONE the value will be the cluster location (e.g. the master zone for multi-zonal clusters with a single zone master or the master region for regional clusters).

    Note that this isn't a supported API, so it may disappear in future versions of GKE, but for now it's a simple way to fetch the cluster location from any node.