Abhijit Sarkar
Abhijit Sarkar

Reputation: 24618

Kubernetes: How to refer to one environment variable from another?

I've a Deployment object where I expose the POD ID using the Downward API. That works fine. However, I want to set up another env variable, log path, with reference to the POD ID. But, setting that variable value to /var/log/mycompany/${POD_ID}/logs isn't working, no logs are created in the container. I can make the entrypoint script or the app aware of the POD ID, and build up the log path, but I'd rather not do that.

Upvotes: 53

Views: 40387

Answers (4)

mdaniel
mdaniel

Reputation: 33231

The correct syntax is to use $(FOO), as is described in the the documentation; the syntax you have used is "shell" syntax, which isn't the way kubernetes interpolates variables. So:

containers:
- env:
  - name: POD_ID
    valueFrom: # etc etc
  - name: LOG_PATH
    value: /var/log/mycompany/$(POD_ID)/logs

Also please note that, as mentioned in the Docs, the variable to expand must be defined before the variable referencing it.

Upvotes: 103

Rafiq
Rafiq

Reputation: 11535

you can also add a secret first then use newly created secret into your countless deployment files to share same environment variable with value:

kubectl create secret generic jwt-secret --from-literal=JWT_KEY=my_awesome_jwt_secret_code
spec:
      containers:
        - name: auth
          image: lord/auth
          env:
            - name: MONGO_URI
              value: "mongodb://auth-mongo-srv:27017/auth"
            - name: JWT_KEY
              valueFrom:
                secretKeyRef:
                  name: jwt-secret
                  key: JWT_KEY
 process.env.MONGO_URI
 process.env.JWT_KEY

Upvotes: 0

craftsmannadeem
craftsmannadeem

Reputation: 2963

Here is an example which reads metadata.name

          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: LOG_FILE_NAME
            value: "/app/log/$(POD_NAME).log"

Here is another example, which reads metadata.namespace

          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: REACT_APP_DB_URI
            value: "http://api-$(POD_NAMESPACE).org.com"

One more example, demonstrating how to read from metadata.labels

          - name: SRVC_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.labels['app']
          - name: LOG_FILE_NAME
            value: '/app/log/$(SRVC_NAME).log'

The key is dependent variable should defined later

Refer this for more details

Upvotes: 5

Jonathan Lynch
Jonathan Lynch

Reputation: 587

I'd just like to add to this question, a caveat we ran into the other day. According to the documentation:

Variable references $(VAR_NAME) are expanded using the previous defined environment variables in the container and any service environment variables. If a variable cannot be resolved, the reference in the input string will be unchanged.

Emphasis mine. If you have

  - name: POD_ID
    valueFrom: # etc etc
  - name: LOG_PATH
    value: /var/log/mycompany/$(POD_ID)/logs

it will work, but if you have

  - name: LOG_PATH
    value: /var/log/mycompany/$(POD_ID)/logs
  - name: POD_ID
    valueFrom: # etc etc

it will not. If you're using a templating engine to generate your specs, beware.

Upvotes: 14

Related Questions