Search code examples
kubernetesconfigurationkubectl

Env variables from configmap not available inside pod


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?


Solution

  • 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