I would like to run a pod on one of my IoT devices.
Each one of those devices contains an environment variable I want this pod to use.
Is there any way to inject this env variable into the pod using build-in templating of helm
/kubectl
?
I was trying the following on my deployment.yaml
file:
env:
- name: XXX
value: $ENV_FROM_HOST
but when executing the pod and trying the get XXX
value, I get the string "$ENV_FROM_HOST" instead of its value from the host:
$ echo $XXX
$ENV_FROM_HOST
Thanks.
CodePudding user response:
It's not possible to directly pass the host's env vars to the pods. I often do that by creating a ConfigMap.
Create a ConfigMap with
from-lireral
option:kubectl create configmap testcm --from-literal=hostname=$HOSTNAME
Refer to that in the Pod's manifest:
- name: TEST valueFrom: configMapKeyRef: name: testcm key: hostname
This will inject the host's $HOSTNAME into the Pod's $TEST.
If it's sensitive information, you can use Secrets instead of using ConfigMap.
CodePudding user response:
Speaking about Kubernetes, one of the assumption is that pod is only sharing resources between containers in the pod:
A Pod (as in a pod of whales or pea pod) is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers. A Pod's contents are always co-located and co-scheduled, and run in a shared context.
That means a pod is isolated from the host that is running on (node), so you can't just straight read an environment variable from the node as you want.
However, some workarounds are coming to my mind.
If the environment variable is the same for all nodes:
If you are using CI/CD, then you can implement the following code. In deployment definition -
deployment.yaml
env: - name: XXX value: ENV_TO_CHANGE
The command:
sed "s/ENV_TO_CHANGE/{YOUR_VALUE}/g" deployment.yaml | kubectl apply -f -
It won't edit existing file; it will output the edited
deployment.yaml
file and it will pipe tokubectl apply
command
If the environment variables is different for each node:
- Set your environment variables as node labels and then inject them into pods - some solutions are presented in this thread.
- (NOT RECOMMENDED) - save an environment variable on the node as the file in a specific directory, then read this on the pod using
hostPath
volume. Keep in mind thathostPath
presents many security risks and it's better to avoid it.