Kubernetes Config Maps

A ConfigMap is a Kubernetes object that stores configuration data in a key/value store.

apiVersion: v1
kind: ConfigMap
metadata:
  name: config-map-demo
data:
  greetingOne: Hey there!
  greetingTwo: Howdy partner!

A ConfigMap can be referenced in a Pod spec and used to configure an application running in a container.

ConfigMap Data as Environment Variables

The following example shows how we can define an environment variable for a container and then set its value using data from a ConfigMap.

apiVersion: v1
kind: Pod
metadata:
 name: config-map-demo-pod
 labels:
  app: demo-app
spec:
 containers: 
 - name: demo-app-container 
   image: busybox 
   command: ['sh', '-c', 'echo $GREETING & sleep 3600']
   env:
     - name: GREETING
       valueFrom:
         configMapKeyRef:
           name: config-map-demo
           key: greetingOne

valueFrom.configMapRef.name is the name of the ConfigMap we want to reference. valueFrom.configMapRef.key is the key we want to retrieve a value for. In the example above it will lookup the value associated with key greetingOne. This value is then assigned to environment variable GREETING and used in the container command.

After creating the ConfigMap and Pod above, we can see that on start up the container printed the value retrieved from the ConfigMap.

ConfigMap Data as a Mounted Volume

The following example shows how we can mount a ConfigMap as a mounted volume, then use the mounted volume to access ConfigMap data inside the container.

apiVersion: v1
kind: Pod
metadata:
 name: config-map-pod-volume-demo
 labels:
  app: demo-app
spec:
 containers: 
 - name: demo-app-container 
   image: busybox 
   command: ['sh', '-c', 'echo $(cat /etc/config/greetingTwo) & sleep 3600']
   volumeMounts:
     - mountPath: /etc/config
       name: config-volume
 volumes:
   - name: config-volume
     configMap:
       name: config-map-demo

volumeMounts.mountPath is the path on the containers file system where the  ConfigMap data will be mounted. Each key in the ConfigMap we created earlier will become a file in /etc/config. Each key file will contain the value data from the ConfigMap.

In command we access data from the mounted volume. Using cat we can get the contents of the file with key greetingTwo.  On startup the container will lookup the contents of file greetingTwo and write it to the console.

volumes.name is the name of the volume and is used to tie a volume to a volumeMount.  volumes.configMap.name is the name of the ConfigMap we want to mount.

After creating the Pod we can exec into the running container and inspect the mounted volume.

In /etc/config we can see two files, greetingOne and greetingTwo . These files are created for each of the keys defined in the ConfigMap created earlier. When we cat each file we see that they contain the key value defined in the ConfigMap.

Inspecting the container logs we can see the expected value Howdy partner! is logged for the key value greetingTwo specified earlier.

The sample code for this post is available here.