Hi All, I have a doubt of using config map created from a file in the pod as en . . .

Chhandak Dey:
Hi All,

I have a doubt of using config map created from a file in the pod as environment variables. Below are the steps what I have done,

Step 1=> I have created a text file config.txt
-> echo $‘key1=value1\nkey2=value2’

Step 2 => Created config map

→ k create cm keyvalcfgmap --from-file=config.txt
=> yaml view of the cm keyvalcfgmap
apiVersion: v1
data:
config.txt: |
key1=value1
key2=value2
kind: ConfigMap
metadata:
creationTimestamp: “2022-01-17T13:21:57Z”
name: keyvalcfgmap
namespace: default
resourceVersion: “127717”
uid: 6ca5e263-b9d4-405d-a40c-e0780de2bace

Step 3 => Create a pod

1 apiVersion: v1
2 kind: Pod
3 metadata:
4 creationTimestamp: null
5 labels:
6 run: nginx
7 name: nginx
8 namespace: default
9 spec:
10 containers:
11 - image: nginx
12 name: nginx
13 resources: {}
14 envFrom:
15 - configMapRef:
16 name: keyvalcfgmap
17 dnsPolicy: ClusterFirst
18 restartPolicy: Never
19 status: {}

However when I check the environment variables inside the container, it is coming like -

PKG_RELEASE=1~bullseye
config.txt=key1=value1
key2=value2

KUBERNETES_PORT=tcp://10.8.0.1:443

Can anyone please check and tell me what I am missing?

unnivkn:
Hi @Chhandak Dey please remove config.txt from configMap & try again.


image.png

Chhandak Dey:
The cm is created from the text file. I don’t want to change the cm. Is there anything we can do with Pod’s yaml?

nagaraj mogaroy:
Hi @Chhandak Dey, try using - k create cm keyvalcfgmap --from-env-file=config.txt to create the configmap instead. Below is the yaml of the cm thus created:
k get cm keyvalcfgmap -o yaml
apiVersion: v1
data:
key1: value1
key2: value2
kind: ConfigMap
metadata:
creationTimestamp: “2022-01-18T08:06:48Z”
name: keyvalcfgmap
namespace: default
resourceVersion: “899703”
uid: 0adf227e-33b3-4d28-ae15-27f6729a26ee

And the envionment variables inside the container:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=nginx
TERM=xterm
key1=value1
key2=value2

unnivkn:
Hi @Chhandak Dey do you think the text file you created is correct ? please correct it, if you don’t want to edit the yaml. https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/

Chhandak Dey:
@nagaraj mogaroy and @unnivkn Thank you so much!