How to get the id/name of the container in which my web application is running to serve the current user request

3/23/2018

We are using Dockers and Kubernets to containerize my .Net web application and scale up to 8-9 replicas. So we installed Dockers (in my Win 10 machine) and used Linux containers.

Orchestration is done by Kubernets. I would like to get the name/id of the container in which my web application is running to serve the current user request.

Could not find a way to get the id/name of the container from my .Net Web application. Please share me your thoughts on this.

Regards, Hari

-- Hari
google-kubernetes-engine
kubernetes
kubernetes-health-check
kubernetes-helm
kubernetes-ingress

1 Answer

3/23/2018

You can expose Pod fields as environment variables, the field metadata.name will contain the name of the Pod.

Documentation and example: https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#use-container-fields-as-values-for-environment-variables

Edit: You can also use System.Environment.MachineName as @vahdet commented earlier. The hostname of the container is the pod id.

-- Matt-y-er
Source: StackOverflow