I am currently working on a project where I need to set a bigger number (approx. 20) of environment variables.
Now these variable should be shared with multiple containers inside of a pod. I am using a Configmap to define the values. Now my problem is this. I have to set every Environment variable seperately and also have to do this for every container which just is pretty ugly and alot of unnecessary code. Basically it's looking something like this
kind: Pod
apiVersion: v1
...
containers:
- name: container1
image: my-image:latest
env:
- name: VAR1
valueFrom:
configMapKeyRef:
name: my-config
key: my-var1
...
- name: container2
image: some-other-image:stable
env:
- name: VAR1
valueFrom:
configMapKeyRef:
name: my-config
key: my-var1
...
What I want is to automatically add all the values from my configMap as and environment variable to the container. My first approach is to mount a configMap as volume and then run a shell script on startup. Similar to this
configMap
kind: ConfigMap
apiVersion: v1
...
data:
envCat.sh: |
#!/bin/sh
export $(grep -v '^#' /etc/pod-config/envVars | xargs)
cat
envVars
MY_VAR1="HELLO WORLD!!"
MY_VAR2="I AM HERE"
Pod
kind: Pod
apiVersion: v1
...
spec:
volumes:
- name: config-volume:
configMap:
name: my-config
containers:
- name: container1
image: my-image:latest
volumeMounts:
- name: config-volume
mountPath: /etc/pod-config
command:
- /etc/pod-config/envCat.sh
Unfortunately the export command works just fine when I run it manually in the container but running the shell script or running /bin/sh /etc/pod-config/envCat.sh is not working.
Any suggestions to achieve what I want to do?
You should be using envFrom to load all key:value pairs as environment variables inside the container