<p>I want to run Python code inside a pod. The pod is created by airflow that I don't control.</p>
<p>I want to somehow get the name of the pod I'm running in.</p>
<p>How can it be done?</p>

Felix
<p>You can tell kuberenetes to mount an env variable for you:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: gcr.io/google_containers/busybox
      command: [ &quot;/bin/sh&quot;, &quot;-c&quot;, &quot;env&quot; ]
      env:
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
</code></pre>
<p>and then in python you can access it like:</p>
<pre class="lang-py prettyprint-override"><code>import os
pod_name = os.environ['MY_POD_NAME']
</code></pre>
<hr />
<p>Or you can just open and read <code>/etc/hostname</code>:</p>
<pre class="lang-py prettyprint-override"><code>f = open('/etc/hostname')
pod_name = f.read()
f.close()
</code></pre>
