<p>Tried to get the pod name inside the Tomcat container at startup. Already exposed the pod's full name as an environment variable using the <code>Kubernetes Downward API</code> as below in <code>search.yaml</code> file ( only a portion of the file attached).</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
name: search
namespace: dev
labels:
    app: search
spec:
replicas: 1
selector:
    matchLabels:
    app: search
template:
    metadata:
    labels:
        app: search
    spec:
    hostname: search-host
    imagePullSecrets:
    - name: regcred
    containers:
    - name: search-container
        image: docker.test.net/search:develop-202104070845
        env:
        - name: MY_POD_NAME
        valueFrom:
            fieldRef:
            fieldPath: metadata.name
        ports:
        - containerPort: 8080
        resources:
        requests:
            memory: &quot;2048Mi&quot;
            cpu: &quot;1&quot;
        limits:
            memory: &quot;2048Mi&quot;
            cpu: &quot;2&quot;
        env:
        - name: SERVER_GROUP
        value: &quot;SEARCH&quot;
        - name: MIN_TOMCAT_MEMORY
        value: &quot;512M&quot;
        - name: MAX_TOMCAT_MEMORY
        value: &quot;5596M&quot;
        - name: DOCKER_TIME_ZONE
        value: &quot;Asia/Colombo&quot;
        - name: AVAILABILITY_ZONE
        value: &quot;CMB&quot;
</code></pre>
<p>After running the pod this environment variable is available in docker level.</p>
<p>Pod details</p>
<pre><code>NAME                        READY    STATUS    RESTARTS      AGE
search-56c9544d58-bqrxv      1/1     Running      0          4s
</code></pre>
<p>Pod environment variable for pod name</p>
<pre><code>POD_NAME=search-56c9544d58-bqrxv
</code></pre>
<p>When accessed this as below in Tomcat container's java code in a jar called <code>BootsTrap.jar</code> and it returned as <code>null</code>.</p>
<pre><code>String dockerPodName = System.getenv( &quot;POD_NAME&quot; );
</code></pre>
<p>Is it because the pod is not up and running before the tomcat container initialized or accessing the environment variable in Java is incorrect or is there another way of accessing pod's environment variable through java.</p>

Gayan Mettananda
<p>You are setting <code>MY_POD_NAME</code> as environment variable, but do the lookup for <code>POD_NAME</code>. Use the same name in the Java code and the deployment.</p>
<p>Note: Your YAML seems to have wrong indentation, I assume that this is just a copy-paste artifact. If that is not the case, it could lead to rejected changes of the deployment since the YAML is invalid.</p>
