<p>How can I to source the Kubernetes deployment/job name that spawned the current pod from within the pod?</p>

<p>In many cases the hostname of the Pod equals to the name of the Pod (you can access that by the HOSTNAME environment variable). However that's not a reliable method of determining the Pod's identity.</p>

<p>You will want to you use the <a href="https://kubernetes.io/docs/user-guide/downward-api/" rel="noreferrer">Downward API</a> which allows you to expose metadata as environment variables and/or files on a volume.</p>

<p>The name and namespace of a Pod can be exposed as environment variables (fields: <code>metadata.name</code> and <code>metadata.namespace</code>) but the information about the creator of a Pod (which is the <em>annotation</em> <code>kubernetes.io/created-by</code>) can only be exposed as a file.</p>

<p>Example:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: busybox
  labels: {app: busybox}
spec:
  selector: {matchLabels: {app: busybox}}
  template:
    metadata: {labels: {app: busybox}}
    spec:
      containers:
      - name: busybox
        image: busybox
        command:
        - "sh"
        - "-c"
        - |
          echo "I am $MY_POD_NAME in the namespace $MY_POD_NAMESPACE"
          echo
          grep ".*" /etc/podinfo/*
          while :; do sleep 3600; done
        env:
        - name: MY_POD_NAME
          valueFrom: {fieldRef: {fieldPath: metadata.name}}
        - name: MY_POD_NAMESPACE
          valueFrom: {fieldRef: {fieldPath: metadata.namespace}}
        volumeMounts:
        - name: podinfo
          mountPath: /etc/podinfo/
      volumes:
        - name: podinfo
          downwardAPI:
            items:
              - path: "labels"
                fieldRef: {fieldPath: metadata.labels}
              - path: "annotations"
                fieldRef: {fieldPath: metadata.annotations}
</code></pre>

<p>Too see the output:</p>

<pre><code>$ kubectl logs `kubectl get pod -l app=busybox -o name | cut -d / -f2`
</code></pre>

<p>Output:</p>

<pre><code>I am busybox-1704453464-m1b9h in the namespace default

/etc/podinfo/annotations:kubernetes.io/config.seen="2017-02-16T16:46:57.831347234Z"
/etc/podinfo/annotations:kubernetes.io/config.source="api"
/etc/podinfo/annotations:kubernetes.io/created-by="{\"kind\":\"SerializedReference\",\"apiVersion\":\"v1\",\"reference\":{\"kind\":\"ReplicaSet\",\"namespace\":\"default\",\"name\":\"busybox-1704453464\",\"uid\":\"87b86370-f467-11e6-8d47-525400247352\",\"apiVersion\":\"extensions\",\"resourceVersion\":\"191157\"}}\n"
/etc/podinfo/annotations:kubernetes.io/limit-ranger="LimitRanger plugin set: cpu request for container busybox"
/etc/podinfo/labels:app="busybox"
/etc/podinfo/labels:pod-template-hash="1704453464"
</code></pre>
