Create and use ConfigMap in Kubernetes With Diagram

Create ConfigMap from YAML file

Create and use configmap from single values

kubectl create configmap cm1 --from-literal=myval1=10000

That would create a configmap with a key myval1 = 10000

Of course, you can use multiple --from-literal blocks in a single command.

Use a single value from configmap in pod yaml

Create and use multiple environment variables from file

If you plan to use multiple environment values, prepare a file my-env-vars.whatever like this:


Then, you can create a configmap like this:

kubectl create configmap cm3 --from-env-file=./my-env-vars.whatever

Then you can refer to all the variables as environment variables in pod by creating a YAM definition like this

Import configmap values as volume

For example, I have a configmap named cm3 like this:

In a pod definition, I mount the configmap as a volume at has mountPath as /etc/config2

apiVersion: v1
kind: Pod
  creationTimestamp: null
  name: pod3-player
  - name: local-config
      name: cm3
  - image: nginx
    name: pod3-player
    - name: local-config
      mountPath: /etc/config2

Now, if I view the content of the folder /etc/config2 inside the container in the pod, I can see a list of files which names are the keys in the configmap:

kubectl exec -it pod3-player -- ls /etc/config2

And if I view the content of a file, for example, health, I will get the value

kubectl exec -it pod3-player -- cat /etc/config2/health

Leave a Comment