Hi All,
Is it possible to use ConfigMap created using --from-file without mount option in a pod?
I have created this configmap-
$ cat amitcm.properties
msg="hello world"
msg1="welcome to this world"
$
$ k create cm amitcm --from-file amitcm.properties
configmap/amitcm created
$
$ k describe cm amitcm
Name: amitcm
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
amitcm.properties:
----
msg="hello world"
msg1="welcome to this world"
Events: <none>
$
now i would like to use the key/value pair as environment variable in my pod, can this be done without using mount option ?
I would like to run this pod which writes the values of environment variables in a log file-
$cat pod.yml
apiVersion: v1
kind: Pod
metadata:
labels:
name: amitpod
spec:
containers:
- image: inboxamitraj/amitubuntu
name: amitpod-c
envFrom:
- configMapRef:
name: amitcm
command: ["/bin/sh"]
args: ["-c", "while true; do echo $msg $msg1>>log.txt; sleep 1;done"]
Cluster information:
Kubernetes version: v1.21.0
Cloud being used:bare-metal
Installation method: kubeadm
Host OS: ubuntu
CNI and version: weave-net 2.8.1
CRI and version: docker 18
Thank you
Thank you @protosam
when we create configmaps from files, the filename becomes the key, now I find it very difficult
to use it as in environement variable.
eg. if my filename is cm.properties, the environement variable in my pod would be - cm.properties
but the problem is - bash does not understand a “.” dot in environement variable.
I am curious, why it is designed this way?
I think it’s because the environment variables are behooven to Bash/Shell programming languages.
1 Like
Hi @protosam,
I managed to figure it out using --from-env-file. if we create configmap this way, the key/value pair inside file are exactly created as Environment variables in the pod.
kubectl create configmap game-config-env-file \
--from-env-file=configure-pod-container/configmap/game-env-file.properties
here is my setup
$ cat cm.properties
msg=hello
msg1=world
$
$k create cm amitcm --from-env-file=cm.properties
$ cat pod.yml
apiVersion: v1
kind: Pod
metadata:
labels:
name: amitpod
spec:
containers:
- image: inboxamitraj/amitubuntu
name: amitpod-c
envFrom:
- configMapRef:
name: amitcm
command: ["/bin/sh"]
args: ["-c", "while true; do echo $msg $msg1>>log.txt; sleep 1;done"]
$
$ k apply -f pod.yml
$ k exec -it amitpod -- tail -f log.txt
hello world
hello world
hello world
hello world
hello world
hello world
Thank you so much for your guidance
Did you see that you can mount configmap data as files as well?
yes, exactly.
what I found is --from-file option is better if you are passing the values in file directly to your application.
–from-env-file is better if you would like your pods to use environment variables in its shell.
Thank you