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

3/30/2018

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.

-- Abhijit Sarkar
kubernetes

1 Answer

3/31/2018

The correct syntax is to use $(FOO), as is described in the v1.EnvVar value: 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.

-- mdaniel
Source: StackOverflow