Updated on 2025-07-29 GMT+08:00

ConfigMaps

ConfigMaps are a type of resource that is used to store the configurations required by applications. It can store configuration data in key-value pairs or configuration files as 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 as 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 used as the value of the environment variable EXAMPLE_PROPERTY_1. After the container is started, the value of property_1 is referenced as the value of EXAMPLE_PROPERTY_1, which is 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 a 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 the volume, and the volume is mounted to the /tmp directory of the container. After the pod is created, two files property_1 and property_2 are generated in the /tmp directory of the container, and the values are Hello and World.

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 named vol-configmap.
      mountPath: "/tmp"
  imagePullSecrets:
  - name: default-secret
  volumes:
  - name: vol-configmap
    configMap:                      # Reference a ConfigMap.
      name: configmap-test