I am trying to pass env variable to my pod from configmap. I have the following setup.
I have a file test-config.txt with 2 env variables
a_sample_env=b
c_sample_env=d
I create a configmap as follows:
kubectl create configmap test-config --from-file test-config.txt
My pod definition is as follows:
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- name: mycontainer
image: redis
envFrom:
- configMapRef:
name: test-config
But my application doesn't receive the 2 env variables in the test-config.txt file. I logged into the pod using kubectl exec and get empty values for the env variables.
root@test-pod:/data# echo $c_sample_env
root@test-pod:/data# echo $a_sample_env
Can anybody point out why the environment variables are not available in the pod?
You can simply create the secret using --from-literal
flag instead of --from-file
kubectl create cm test-config --from-literal=a_sample_env=b --from-literal=c_sample_env=d
Create the pod
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: test-pod
name: test-pod
spec:
containers:
- image: redis
imagePullPolicy: IfNotPresent
name: test-pod
resources: {}
envFrom:
- configMapRef:
name: test-config
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
Exec to the pod and check the envs
root@test-pod:/data# env | grep sample
c_sample_env=d
a_sample_env=b
you should create configmap as below
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
a_sample_env: b
c_sample_env: d
if you create configmap using below command
kubectl create configmap test-config --from-file test-config.txt
then you can mount test-config as volume inside container. You will have to create a wrapper/launch script to export all k:v pair from that file as env variable during startup