Dynamic Configuration in Kubernetes Using ConfigMap (with Examples)

Updated: January 31, 2024 By: Guest Contributor Post a comment

Introduction

As cloud-native applications evolve to be more flexible and scalable, Kubernetes has become the standard for orchestrating containerized applications. One of the powerful features of Kubernetes is the ability to dynamically configure applications via ConfigMaps. In this tutorial, we’ll explore what ConfigMaps are, why they are useful, and how to use them within Kubernetes, complete with examples.

The Fundamentals of ConfigMap

A ConfigMap is an API object used to store non-confidential data in key-value pairs. ConfigMaps allow you to separate your configurations from your application code, which makes your applications easily portable and configurable without the need for re-creation or redeployment of container images.

Creating a ConfigMap

First, let’s create a basic ConfigMap. Save the following YAML in a file called simple-configmap.yml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: simple-config
data:
  myKey: myValue

Create the ConfigMap with the following command:

kubectl create -f simple-configmap.yml

Output:

configmap/simple-config created

Accessing ConfigMap Values in Pods

Here’s how you can inject the value of myKey from the ConfigMap into a Pod:

apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: example-container
    image: busybox
    command: ['sh', '-c', 'echo $MY_VAR && sleep 3600']
    env:
    - name: MY_VAR
      valueFrom:
        configMapKeyRef:
          name: simple-config
          key: myKey

Create the Pod using:

kubectl create -f pod-example.yml

Output:

pod/example-pod created

To see the output of the command, check the logs of the Pod:

kubectl logs example-pod

Output:

myValue

Using ConfigMaps with Environment Variables

ConfigMaps can also be used to set environment variables for a container within a Pod. You can set individual variables, or inject all data from a ConfigMap as environment variables:

apiVersion: v1
kind: Pod
metadata:
  name: env-var-example
spec:
  containers:
  - name: env-container
    image: busybox
    command: ["sh", "-c", "env"]
    envFrom:
    - configMapRef:
        name: simple-config

Create the pod and view the environment variables:

kubectl create -f env-var-example.yml
kubectl exec env-var-example -- env

Output:

MYKEY=myValue

Updating ConfigMaps

To update a ConfigMap, you can use the kubectl edit command:

kubectl edit configmap simple-config

This will open the ConfigMap in your default editor, allowing you to make changes. Note that changing a ConfigMap doesn’t automatically update the containers using it. Pods have to be restarted to pick up the new configuration.

Advanced Examples: ConfigMaps and Volumes

ConfigMaps can also be mounted as volumes. This is useful if you have configuration files that should be consumed by your application. The following manifests show how to set up a Pod with a volume containing files from a ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: config-volume-map
data:
  config.json: |
    {
        "key": "value"
    }
---
apiVersion: v1
kind: Pod
metadata:
  name: volume-example
spec:
  containers:
  - name: example-container
    image: busybox
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config
  volumes:
  - name: config-volume
    configMap:
      name: config-volume-map

Create these objects and then view the contents of the file:

kubectl create -f volume-configmap.yml
kubectl exec volume-example -- cat /etc/config/config.json

Output:

{
  "key": "value"
}

Conclusion

ConfigMaps in Kubernetes provide a flexible way to decouple configuration from application code. By using ConfigMaps, developers and operators can easily manage, update, and inject configurations into Pods, enhancing both the portability and configurability of Kubernetes applications.