ConfigMaps are Kubernetes objects that are dictionaries or maps holding a set of key-value pairs. For example, if there is a Pod and it needs to have some pre configured environment variables for the container, we can keep the Pod configuration/manifest simple by keeping all the environment variable related information separately as a ConfigMap, and we would then relate the Pod with the ConfigMap so that the Pod takes it's configuration from the ConfigMap. Likewise configMaps can also be used to store data as volumes in containers, similar to environment variables, we shall see both the cases.

ConfigMaps can also be thought of like 'Secrets' which are again Kubernetes objects holding key value pairs, however the difference is that Secrets contain encrypted data and are more secure, where as ConfigMaps contain unencrypted data and are often used to store data that is not that sensitive.

Let’s check the list of configmap(s) aka cm in the default namespace, in a newly launched Kubernetes cluster. There shouldn’t be any.

Let’s check the list of configmaps in all the namespaces. There should be one in the kube-public namespace and a few in the kube-system namespace.

Before creating a ConfigMap, let’s check the apiVersion value for it

Let’s define a ConfigMap configuration as follows, with 3 key value pairs.

To create the ConfigMap

To check the ConfigMap, it shows DATA 3 because we have created 3 key value pairs.

To view more details about the ConfigMap, it shows all the key value value pairs under the Data section.

Let’s define a Pod where we can use this ConfigMap, to import environment variables into the container.

To create the Pod.

To check the Pod’s status.

To login to the container and to print all it’s environment variables, there should be few predefined environment variables, in addition to what we imported (those with prefix liked) from the ConfigMap. Note that printenv is the command to print all the environment variables.

To filter just the environment variables what we imported from the ConfigMap. Note that grep is a utility to filter output using search patterns.

We are done with case1, where in we were able to import environment variables into the container using a ConfigMap. Let’s now focus on case2, to import data onto a volume attached to the container, from the ConfigMap.

We shall use the same ConfigMap cm17 for this case. Let’s define a new Pod configuration as follows.

To create the Pod

To check the Pod’s status

To login to the container

Let’s view the contents inside the /tmp/apache directory. Note that this directory was specified as the mountPath in the container. We should see 3 files, each corresponding to a key value pair, the file’s name would be the key, and the file’s content would be the value.

To see the contents of the files we can use the cat command, note that printf ‘\n’ is used to print a newline character (one extra line), so that the output looks good, and that semicolon ‘;’ is used to separate one command from another, it’s used to give multiple commands at once. We should now be able to see the corresponding values inside each file.

Let’s do the clean up now,

To exit out of the container

To delete the Pods we created

To delete the ConfigMap

--end-of-post--