Video Thumbnail for Lesson
4.10: ConfigMap

ConfigMap

In Kubernetes, a ConfigMap is used to store non-confidential data in key-value pairs.

Pods can consume ConfigMaps as environment variables, command-line arguments, or as configuration files in a volume.

Official docs: https://kubernetes.io/docs/concepts/configuration/configmap/

Hands-On: Working with ConfigMaps

We will create a pair of ConfigMaps and show how to consume them within a Pod.

1. Create a Namespace for the Examples

First, we'll create a namespace for these examples and set it as the default.

# task 01-create-namespace
# - Create a namespace for these examples and set as default.
kubectl apply -f Namespace.yaml
kubens 04--configmap

2. Create a ConfigMap with File-like Keys

For applications that consume their configuration from files, we can define a ConfigMap where the keys are filenames and the values are multiline strings containing the files content.

# ConfigMap.file-like-keys.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: file-like-keys
data:
  conf.yml: |
    name: YourAppName
    version: 1.0.0
    author: YourName
# task 02-apply-file-like
# - Apply the ConfigMap with file-like keys.
kubectl apply -f ConfigMap.file-like-keys.yaml

3. Create a ConfigMap with Property-like Keys

For applications that consume their configuration via environment variables, we can define a ConfigMap where each key represents one of those environment variables.

# ConfigMap.property-like-keys.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: property-like-keys
data:
  NAME: YourAppName
  VERSION: 1.0.0
  AUTHOR: YourName
# task 03-apply-property-like
# - Apply the ConfigMap with property-like keys.
kubectl apply -f ConfigMap.property-like-keys.yaml

4. Deploy a pod consuming these ConfigMaps

We can then create a Pod specification that consumes these two configmaps.

apiVersion: v1
kind: Pod
metadata:
  name: configmap-example
spec:
  containers:
    - name: nginx
      image: nginx:1.26.0
      volumeMounts:
        - name: configmap-file-like-keys
          mountPath: /etc/config
      envFrom:
        - configMapRef:
            name: property-like-keys
  volumes:
    - name: configmap-file-like-keys
      configMap:
        name: file-like-keys

For the file-like ConfigMap, we add it as a volume and mount it at a particular path in the container filesystem.

For the property-like ConfigMap, we use envFrom to load the values as environment variables. We could also pull out specific keys using:

env:
  - name: SOME_OTHER_NAME
    valueFrom:
      configMapKeyRef:
        name: property-like-keys
        key: NAME

5. Verify the Configuration

To verify the file-like ConfigMap was mounted properly we can use kubectl exec to show the contents of the file inside the container.

❯ kubectl exec configmap-example -c nginx -- cat /etc/config/conf.yml
name: YourAppName
version: 1.0.0
author: YourName

To verify the property-like ConfigMao was loaded properly as environment variables we can use kubectl exec to print the environment variables within the container.

❯ kubectl exec configmap-example -c nginx -- printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=configmap-example
NGINX_VERSION=1.26.0
NJS_VERSION=0.8.4
NJS_RELEASE=2~bookworm
PKG_RELEASE=1~bookworm
VERSION=1.0.0
AUTHOR=YourName
NAME=YourAppName
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
HOME=/root

7. Delete the Namespace to Clean Up

Finally, clean up by deleting the namespace, which will also delete all resources within it.

# task 07-delete-namespace
# - Delete the namespace to clean up.
kubectl delete -f Namespace.yaml