Stanko
Stanko

Reputation: 4465

How can I get the deployment name from within my container?

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:

--- 
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! && 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

What am I missing?

Update:

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?

Upvotes: 2

Views: 3983

Answers (2)

Logamani Gunasekaran
Logamani Gunasekaran

Reputation: 154

I'm not seeing any direct solution to get the deployment name from container. The workaround that I'm using is with the help of pod labels,

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app1-deployment
spec:
  template:
    metadata:
      labels:
        app: app1-deployment
    spec:
      containers:
        - name: container1
          image: nginx
          env:
            - name: DEPLOYMENT_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.labels['app']


...
...

I'm using app as label name but deployment-name could also be better naming convention for this

Upvotes: 1

Jose Armesto
Jose Armesto

Reputation: 13749

You are using the wrong indentation and structure for Deployment objects.

Both the command key and the env key are part of the container key.

This is the right format

--- 
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! && 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

Remember that you can validate your Kubernetes manifests using this online validator, or locally using kubeval.

Referring to the main part of the question, you can get the object that created the Pod, but most likely that will be the ReplicaSet, not the Deployment.

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.

Typically, labels are used in the PodSpec of the Deployment object to add metadata.

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

Upvotes: 6

Related Questions