Saiyam Pathak avatar
By Saiyam Pathak
Field CTO


Delve into the world of Kubernetes ConfigMaps. This lesson will help you learn how to create ConfigMaps and use them within a Kubernetes pod.

As mentioned in our intro to secrets and configmaps lesson, ConfigMaps are a key component in Kubernetes, allowing you to decouple your containerized image and provide different configurations to run in different environments.

Creating a Kubernetes ConfigMap

Creating a ConfigMap in Kubernetes is a straightforward process. You can use the kubectl create configmap command to create a ConfigMap. There are various options you can explore, such as creating it from a file, from a file with a key, from a literal, or from an environment file.

For instance, to create a ConfigMap from a file, you can use the command kubectl create configmap test --from-file=file.prop. The test here is the name of the file. You can view the data in the ConfigMap using the command kubectl describe configmap test.

If you have multiple key values in a file and you want them as separate data, you can use an environment file. Run the command kubectl create configmap test2 --from-file=env.prop. Now, if you use the command kubectl describe configmap test2, you can see that we have different data, environments, locations, and versions.

You can also create a ConfigMap using a literal. For example, use the command `kubectl create configmap demo --from-literal=name=saiyam`. Then run the command `kubectl get configmap`, and you will see the creation of a ConfigMap from a literal.

Using a Kubernetes Pod in a ConfigMap

Now that we have a ConfigMap, let's learn how to use it in a pod. For this, you need to have a basic YAML file. In that file, you will have apiVersion, kind, metadata with the labels, and a spec section containing the container with the image as busybox.

In the container, it'll list out all the environment variables. In the environment section, we are naming the environment variable HELLO, and we are taking its value from the ConfigMap key reference. So the name of the ConfigMap will be demo, and the key that we want to refer to is name.

To see if that happens, run the command kubectl create -f demo.yaml. By running the command ubectl logs demo, you can see that HELLO=saiyam is printed.

Mounting a ConfigMap as a Volume Inside a Pod

Another way of using a ConfigMap is by mounting it as a volume inside a pod. For this, you have a YAML file, apiVersion, kind, metadata, and namespace, which has the following data.

In the container section, you have volumes. In the volume, we specify the ConfigMap that we want to mount inside the container and the volume`s mount path. So let's create the ConfigMap by using the command kubectl create -f cm-for-volume.yaml. Next, let's create the pod using the command kubectl create -f demo2.yaml. Now, let`s see the logs using the command kubectl logs cm-volume. You can see that file1 and file2 are present over there.


ConfigMaps in Kubernetes are versatile and can be used in various ways. You can mount a ConfigMap as a volume, use it as an environment variable, create it using different YAML files, or environment files, and you can also create a ConfigMap using a literal. This lesson has provided you with the knowledge to start building and using ConfigMaps in your Kubernetes environment.

Don't stop now, check out your next lesson