Reputation: 671
According to https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/#using-environment-variables-inside-of-your-config environment variables (defined in the config) can be used in the config like this:
...
spec:
containers:
env:
- name: GREETING
value: "Warm greetings to"
...
args: ["$(GREETING) $(HONORIFIC) $(NAME)"]
Can I use the variables everywhere in the config? (Probably only in side container.)
This seems not to work:
env:
- name: DBHOST
valueFrom:
configMapKeyRef:
key: db-host
name: db-config
- name: DB_URL
value: mysql://$(DBHOST)/my_database
In the container, echo $DB_URL
showed mysql://$(DBHOST)/my_database
, but I expected DBHOST to be replaced.
So does it work in all other places except in env
?
Maybe I have to look for another possibility to concatenate the values from the config map.
Upvotes: 1
Views: 424
Reputation: 159998
In the detailed API documentation for a Container object, some of the fields specifically call out
Variable references $(VAR_NAME) are expanded using the container's environment.
This is specifically documented for args
and command
. Similar text is also in the documentation for EnvVar's value
, with the caveat there that "references $(VAR_NAME) are expanded using the previous defined environment variables" (order of the env:
block matters).
In a couple of places you can also exec a command inside a container. This includes postStart
and preStop
hooks and probes. These ultimately become an ExecAction API object. The command there is documented to not run inside a shell and can't directly use the $(VAR_NAME)
syntax, but if you do run a shell explicitly, it should be able to use container-level environment variables. (If your command:
or Dockerfile ENTRYPOINT
sets environment variables, these will not be visible, similar to docker exec
.)
apiVersion: v1
kind: Pod
metadata: {name: for-example}
spec:
containers:
- name: for-example
env:
- name: VAR_NAME
value: a concrete string
- name: COPY_OF_VAR_NAME
value: '$(VAR_NAME)'
command:
- /bin/echo
- '$(VAR_NAME)'
args:
- '$(VAR_NAME), again'
livenessProbe:
exec:
command:
- /bin/sh
- -c
- echo $VAR_NAME gets expanded by the shell
lifecycle:
postStart:
exec:
command:
- /bin/sh
- -c
- echo $VAR_NAME gets expanded by the shell
Upvotes: 1