I have a deployment configured with 5 replicas. I want to know inside each running container the name of the pod replica.
When I execute:
kubectl get pods
NAME READY STATUS RESTARTS AGE
test-581957695-cbjtm 1/1 Running 3 1d
test-581957695-dnv8s 1/1 Running 1 1d
test-581957695-fv467 1/1 Running 1 1d
test-581957695-m74lc 1/1 Running 0 1d
test-581957695-s6cx0 1/1 Running 1 1d
Is it possible to get the name "test-581957695-cbjtm" from inside the container?
Thank you.
Another option is to use Kubernetes Downward API. Using the API, you can expose pod/container information inside your container via a VolumeFile or Environment Variables. Currently, these are the pieces of information you can expose:
- The node’s name
- The Pod’s name
- The Pod’s namespace
- The Pod’s IP address
- The Pod’s service account name
- A Container’s CPU limit
- A container’s CPU request
- A Container’s memory limit
- A Container’s memory request
In addition, the following information is available through DownwardAPIVolumeFiles,
- The Pod’s labels
- The Pod’s annotations
For more information please see, https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#the-downward-api
You can use env vars to expose pod information to container.