Updated on 2026-07-07 GMT+08:00

ConfigMaps

ConfigMaps are a type of resource used to store the configurations required by applications. They can store configuration data in key-value pairs or configuration files.

ConfigMaps allow you to decouple environment-specific configurations from container images, so that different environments can have their own unique configurations.

Creating a ConfigMap

In the following example, a ConfigMap named configmap-test is created. The ConfigMap configuration data is defined in the data field.

apiVersion: v1
kind: ConfigMap
metadata:
  name: configmap-test
data:                     # Configuration data
  property_1: Hello
  property_2: World

Referencing a ConfigMap in an Environment Variable

ConfigMaps are usually referenced as environment variables or as configuration files in volumes.

In the following example, property_1 of configmap-test is referenced as the environment variable EXAMPLE_PROPERTY_1. Once the container starts, EXAMPLE_PROPERTY_1 holds the value of property_1, Hello.

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - image: nginx:alpine
    name: container-0
    resources:
      limits:
        cpu: 100m
        memory: 200Mi
      requests:
        cpu: 100m
        memory: 200Mi
    env:
    - name: EXAMPLE_PROPERTY_1
      valueFrom:
        configMapKeyRef:          # Reference a ConfigMap.
          name: configmap-test
          key: property_1
  imagePullSecrets:
  - name: default-secret

Referencing a ConfigMap in a Volume

Referencing a ConfigMap in a volume is when you save the data as configuration files in the volume. Each piece of data is saved as an individual file. The key is the file name, and the key value is the file content.

In the following example, a volume named vol-configmap is created, the ConfigMap named configmap-test is referenced in this volume, and the volume is mounted to the /tmp directory of the container. After the pod is created, there are two files property_1 and property_2 in this /tmp directory, and the key values are Hello and World, respectively.

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - image: nginx:alpine
    name: container-0
    resources:
      limits:
        cpu: 100m
        memory: 200Mi
      requests:
        cpu: 100m
        memory: 200Mi
    volumeMounts:
    - name: vol-configmap           # Mount the volume vol-configmap.
      mountPath: "/tmp"
  imagePullSecrets:
  - name: default-secret
  volumes:
  - name: vol-configmap
    configMap:                      # Reference a ConfigMap.
      name: configmap-test