简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM