<p>I'm trying to set the deployment name as environment variable using the downward API but my container keeps crashing without any logging. I'm using the busybox to print the environment variables. I've had success using a Pod but no luck with a Deployment: This is my YAML:</p>

<pre><code>--- 
apiVersion: apps/v1
kind: Deployment
metadata: 
  labels: 
    app: test-d
  name: test-deploy
spec: 
  replicas: 1
  selector: 
    matchLabels: 
      app: test-d
  template: 
    metadata: 
      labels: 
        app: test-d
    spec: 
      command: 
        - sh
        - "-c"
        - "echo Hello Kubernetes, I am $MY_DEPLOY_NAME in $MY_CLUSTER_NAME and $MY_NAMESPACE! &amp;&amp; sleep 3600"
      containers: 
        - 
          image: busybox
          name: test-d-container
      env: 
        - 
          name: MY_DEPLOY_NAME
          valueFrom: 
            fieldRef: 
              fieldPath: metadata.name
        - 
          name: MY_NAMESPACE
          valueFrom: 
            fieldRef: 
              fieldPath: metadata.namespace
        - 
          name: MY_CLUSTER_NAME
          value: production
</code></pre>

<p>What am I missing?</p>

<p><strong>Update:</strong></p>

<p>It is clear that my indentation was messed up, thank you for pointing that out but the main part of my question is still not clear to me. How do I get the deployment name from within my container?</p>

<p>You are using the wrong indentation and structure for Deployment objects. </p>

<p>Both the <code>command</code> key and the <code>env</code> key are part of the <code>container</code> key.</p>

<p>This is the right format</p>

<pre><code>--- 
apiVersion: apps/v1
kind: Deployment
metadata: 
  labels: 
    app: test-d
  name: test-deploy
spec: 
  replicas: 1
  selector: 
    matchLabels: 
      app: test-d
  template: 
    metadata: 
      labels: 
        app: test-d
    spec: 
      containers: 
        - image: busybox
          name: test-d-container
          command: 
            - sh
            - "-c"
            - "echo Hello Kubernetes, I am $MY_DEPLOY_NAME in $MY_CLUSTER_NAME and $MY_NAMESPACE! &amp;&amp; sleep 3600"
          env: 
            - 
              name: MY_DEPLOY_NAME
              valueFrom: 
                fieldRef: 
                  fieldPath: metadata.name
            - 
              name: MY_NAMESPACE
              valueFrom: 
                fieldRef: 
                  fieldPath: metadata.namespace
            - 
              name: MY_CLUSTER_NAME
              value: production
</code></pre>

<p>Remember that you can validate your Kubernetes manifests using <a href="https://kubeyaml.com/" rel="nofollow noreferrer">this online validator</a>, or locally <a href="https://github.com/instrumenta/kubeval" rel="nofollow noreferrer">using kubeval</a>.</p>

<p>Referring to the main part of the question, <a href="https://stackoverflow.com/questions/42274229/kubernetes-deployment-name-from-within-a-pod">you can get the object that created the Pod</a>, but most likely that will be the ReplicaSet, not the Deployment.</p>

<p>The Pod name is normally generated by Kubernetes, you don't know it before hand, that's why there is a mechanism to get the name. But that is not the case for Deployments: you know the name of Deployments when creating them. I don't think there is a mechanism to get the Deployment name dynamically.</p>

<p>Typically, labels are used in the PodSpec of the Deployment object to add metadata.</p>

<p>You could also try to parse it, since the pod name (which you have) has always this format: deploymentName-replicaSetName-randomAlphanumericChars.</p>
