Use specific key from json file in configmap as Environment variable

6/2/2019

I'm running a k8s cluster where I have a configmap with a json file in it.

kubectl describe cm my-cmap
Name:         my-cmap
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
mydataJson:
----
{
   key1 : value1,
   key2 : value2
}

Is there a way to get key1 as an ENV variable in a pod ?

-- Dhanuj Dharmarajan
configmap
kubernetes

2 Answers

6/2/2019

You can specify configmap reference in the env section to set environment variables with the values from it. In the pod definition add:

     env:
    - name: ENV_NAME
      valueFrom:
        configMapKeyRef:
          name: my-cmap
          key: key1
-- Adam Otto
Source: StackOverflow

6/3/2019

If you are interested here you can find another post about "ConfigMap from file" or "ConfigMap from file with environment variables".

-- Hanx
Source: StackOverflow