How can I read env variables from command section in YAML file? (in Kubernetes)

11/28/2019

I echoed all my env variables and they're there, but when I try to read them from command section, they are empty.

          command:
            - /bin/sh
            - -c
            - >
              if [ ! -f /data/db/admin-user.lock ]; then
                echo "KUBERNETES LOG $HOSTNAME - no Admin-user.lock file found yet" >> /data/db/configlog.txt;
                env >> /data/db/configlog.txt;
                while (! mongo --eval "db.adminCommand('ping')"); do sleep 10; echo "KUBERNETES LOG $HOSTNAME - waiting another 10 seconds for mongo to start" >> /data/db/configlog.txt; done;
                touch /data/db/admin-user.lock
                echo "KUBERNETES LOG $HOSTNAME - try create user with user=$MONGODB_CREATE_USER and pass=$MONGODB_CREATE_PASSWORD..." >> /data/db/configlog.txt;
-- Wilson López
kubernetes

2 Answers

11/29/2019

refer the below sample

apiVersion: v1
kind: Pod
metadata:
  name: env-arg-demo
  labels:
    purpose: env-arg-demo
spec:
  containers:
  - name: env-arg-demo
    image: debian
    env:
    - name: MESSAGE
      value: "hello world"
    command: ["/bin/echo"]
    args: ["$(MESSAGE)"]
-- P Ekambaram
Source: StackOverflow

11/28/2019

You can use this for your command or args field.

Use environment variables to define arguments

Note: The environment variable appears in parentheses, "$(VAR)". This is required for the variable to be expanded in the command or args field.

env:
- name: ARGUMENT
  value: {{ argument_1 }}
args: ["$(ARGUMENT)"]

Where {{ argument_1 }} is an environment variable.

-- georgeos
Source: StackOverflow