Difficulty: intermediate
Estimated Time: 20-30 minutes

BoxBoat Logo

ConfigMaps are a way to decouple configuration from application source code and Kubernetes Pods / Deployment files. If you are familiar with 12-Factor App principles, then this addresses factor 3: "Store config in the environment."

ConfigMaps allow you to independently store data that your application needs in a Kubernetes object. ConfigMaps are decoupled from Pods and Deployments. In addition, ConfigMaps can be consumed by multiple Pods and Deployments, which can be extremely useful. As an example, if you have multiple micro-servies that need access to the same configuration parameter, all it takes is a single ConfigMap. This used to be accomplished with a Config-Server micro-service, or supplying each micro-service with the same environment variable, which made updates difficult.

We will go through the process of creating ConfigMaps from literal values, files, and directories. We'll supply these values to Pods as environment variables, and finally create a simple Redis Deployment configured via ConfigMaps.

Congratulations, you have completed the ConfigMap lab! Next up, we'll tackle Kubernetes Secrets.


Step 1 of 7

Create ConfigMaps from the Command Line

ConfigMaps are Kubernetes objects that store configuration external to source code. Creating a ConfigMap is easy:

kubectl create configmap <configmap-name> <data>

The <data> used to populate the ConfigMap can be from multiple different formats. For testing purposes, the easiest way to supply this is with literals.

Literals refer to data you enter in on the command line. Here's the simplest way to create a ConfigMap:

kubectl create configmap my-config --from-literal=course="Kubernetes Fundamentals"

Now that we've created the ConfigMap, we can view it with:

kubectl get configmaps

The output should look like this:

NAME          DATA      AGE
my-config     1         20s

We can inspect the contents with:

kubectl describe configmap my-config

We can get an even better view with:

kubectl get configmap my-config -o yaml

You can also provide multiple values to a ConfigMap:

kubectl create configmap multi-config --from-literal=name.first=David --from-literal=name.last=Widen --from-literal=age=28 --from-literal=hometown="Glen Cove"

And get a better view with

kubectl get configmap multi-config -o yaml