Configmap – Handling Configurations in Kubernetes.

Reading Time: 4 minutes

NOTE: cm stands for configmap wherever it is used in this article

Kubernetes is a container orchestration tool which has two types of object to insert configuration into containers – Configmap and Secrets .It allows separating config options into a separate object called ConfigMap, which is a map containing key/value pairs with the values ranging from short literals to full config files.

An application doesn’t need to read the ConfigMap directly or even know that it exists. The contents of the map are instead passed to containers as either environment variables or as files in a volume

But before proceeding let us take a look into a scenario to get the concept crystal clear.

Suppose you are running some container image in K8s but it requires few configuration to get it to run . For example a database image requires a DB ( Hostname , User , password ) to initialize it. It can be passed as environment variables in the container and the configuration files can be stored in the cm and used while startup.

1.1 – Working of cm

Basic syntax to create cm is

kubectl create cm [configmap_name] [attribute] [source]

Now as per the source the attribute can be

  • --from file (if the source is a file/directory)
  • --from-literal (if the source is a key-value pair)

Different Ways to Create Configmaps

1- To create ConfigMap from Literal Values

kubectl create configmap [cm_name] --from-literal [key1]=[value1] --from-literal [key2]=[value]2
kubectl create configmap my-config --from-literal environment=development --from-literal username=rishivant

output- configmap/my-config created

Use case:

a – In literal values, you can use a key for same data .

b – This method can be used with applications which require one or two key values for same data as their config. It will be helpful to pass the it using the literal .

2- Creating ConfigMap from a YAML File

kubectl create configmap [configmap_name] --from-file [path/to/yaml/file]
kubectl create configmap myconfig --from-file ~Documents/file/file.yaml

Use case:

a – This method is widely used in case of copying whole configuration for a larger application.

b – While using this we can mount the configs to change the entrypoint of the application.

3- To create ConfigMap through file and multiple files

kubectl create configmap [configmap_name] --from-file [path/to/file]

Lets create from multiple files

kubectl create configmap [configmap_name] --from-file [path to file1] --from-file [path to file2] --from-file [path tofile3]

Also you can create cm from directories using the below command

kubectl create configmap [configmap_name] --from-file [path to directory]

Use Case:

a – Usually working with application which require multiple configuration at a time.

b – This can be used to pass key values for different forms of data.

Ways to list , describe and view your configmaps

1-To list your configmap you can use the below command

kubectl get configmaps   #This is for default namespace

kubectl get congifmaps -n mynamepace #This is for mynamespace 

output : -

rishivant@rishivant-Vostro-3590:~/Documents/Blogs$ kubectl get configmaps -n els
NAME               DATA   AGE
kube-root-ca.crt   1      4d23h
pro-configmap      1      3d


2- To describe your configmap use the below command

rishivant@rishivant-Vostro-3590:~/Documents/Blogs$ kubectl describe cm my-config 
Name:         my-config
Namespace:    default
Labels:       <none>
Annotations:  <none>



Events:  <none>

#cm is shortform for configmap .You can use it in all your command

3- Describing your configmap in your terminal in yaml format you can use the below command

rishivant@rishivant-Vostro-3590:~/Documents/Blogs$ kubectl get cm my-config -o yaml

apiVersion: v1
  environment: development
  username: rishivant
kind: ConfigMap
  creationTimestamp: "2021-12-08T08:37:20Z"
  name: my-config
  namespace: default
  resourceVersion: "563546"
  uid: 7e389ccb-3515-4524-b699-a2800e334dc3

Now lets look at a small demo of creating it through one of the above method and using it in a container.

So first we will create a cm yaml file named as spider.yaml as shown below.

apiVersion: v1
kind: ConfigMap
   name: spiders
  legs: "eight"
  mouth: "one"

Then run the below command to create the Configmap.

kubectl create -f spider.yaml #This command is used to create the kind of object as specified in the file .

Output -  configmap/spiders created

Next we will use it in our pod which will run nginx image in the container .

So lets create a pod using this spiderpod.yaml file

apiVersion: v1
kind: Pod
  name: spiderpod
  - image: nginx
    name: spider-config
    - configMapRef:
        name: spiders   #here you will specify the name of your configmap as above in metadata

Now create the pod as well by running the below command:

kubectl create -f spiderpod.yaml

Once the pod is created you can find the environment variables in it. You will see that the cm has stored those environment. It can be used by the application any time it needs.

Use case of Configmap

1 – For supplying some environment variables in the container.

2 – To set some configuration for an application to run

3 – If you want to provide non-confidential data into your application , you can use the cm.

4- You can mount the cm as volume for changing the entrypoint of the container.


So this blog was all about configmap and how we can use it for configuring our pods .Using this in your project will be good as well as best practise This will help you configure your application at pod level. It can even give you isolation with the pod as for changing the configuration as and when needed.

For more information you can refer to the below links.


Written by 

Rishivant is a enthusiastic devops learner at Knoldus. He believes in going 1% up everyday and showcases his learning in his work.