ConfigMap – Kubernetes Example with YAML

A ConfigMap stores configuration settings that your Kubernetes Pods consume.

In this tutorial, we’ll explain ConfigMaps and give you the complete example YAML to create and use a ConfigMap.

You can find more detail in our Ultimate ConfigMap Guide.

How does a ConfigMap work?

A ConfigMap is a dictionary of key-value pairs that store configuration settings for your applications.

First, create a ConfigMap in your cluster by tweaking our sample YAML to your needs.

Second, consume to ConfigMap in your Pods and use its values.

Example YAML Code

The simplest way to create a ConfigMap is to store a bunch of key-value strings in a ConfigMap YAML file and inject them as environment variables into your Pods.

After that, you can reference the environment variables in your applications using whatever methods is necessary for your programming language.

Creating a ConfigMap

This YAML creates a ConfigMap with the value database set to mongodb, and database_uri, and keys set to the values in the YAML example code.

Then, create the ConfigMap in the cluster using kubectl apply -f config-map.yaml.



Using a ConfigMap in Environment Variables

The key to adding your ConfigMap as environment variables to your pods is the envFrom property in your Pod’s YAML.

Set envFrom to a reference to the ConfigMap you’ve created.


After you’ve created the Pod, you’ll be able to access these environment variables.



Want example YAML for every Kubernetes object? Check out the book.