Kubernetes: How to access a label value of the current running pod from code using K8's API?

10/31/2016

Imagine I got a Kubernetes cluster which has 3 pods and each pod has its own value for the label 'name' and what I actually want is each time I run/open my app, I'd see the name of pod (so its label's value) on console output. Of course the output/value may be different each time, based on which pod is being used.

So, Is there any way to access the value of a pod label of the application (pod) running from code?

My app is based on Java (Spring Boot) by the way.

Thanks.

-- Aziz
cluster-computing
java
kubernetes
spring

1 Answer

10/31/2016

You can use Kubernete's Downward API to Convey Pod Properties to the containers inside the pods without having to use Kubernete's REST API. From the docs:

It is sometimes useful for a container to have information about itself, but we want to be careful not to over-couple containers to Kubernetes. The downward API allows containers to consume information about themselves or the system and expose that information how they want it, without necessarily coupling to the Kubernetes client or REST API.

-- Hans Kristian
Source: StackOverflow