Reputation: 3322
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?
Upvotes: 3
Views: 3069
Reputation: 6841
Here are examples augmenting the original answer (upvoted) of using spec.containers[0].envFrom
in Pods and other k8s/ocp/okd objects:
# kind: Template
# [..]
# objects:
# - kind: DeploymentConfig
# kind: Deployment
kind: Pod
apiVersion: v1
metadata:
name: my-ubuntu
spec:
containers:
- name: my-ubuntu
image: ubuntu:latest
envFrom:
- configMapRef:
name: example-configmap
kind: ConfigMap
apiVersion: v1
metadata:
name: example-configmap
namespace: my-namespace
data:
NUM_CPUS: "32"
API_HOST: "localhost:6379"
LOGGING_LEVEL: "error"
Upvotes: 0
Reputation: 17689
You should be using envFrom to load all key:value pairs as environment variables inside the container
Upvotes: 7