What Is a ConfigMap in Kubernetes?

ConfigMap in Kubernetes

A ConfigMap in Kubernetes is an API object that allows you to store configuration data separately from application code. It helps keep your configurations flexible, making it easier to manage settings without modifying container images or redeploying applications.

What Is a ConfigMap?

Think of a ConfigMap as a central place where your application settings—like database connection strings, API keys (non-sensitive ones), and environment variables—are stored. Instead of hardcoding these values inside your container, your application can fetch them dynamically from a ConfigMap at runtime.

Why Use a ConfigMap?

Managing configuration separately from code is a core principle of the Twelve-Factor App methodology. Here’s why ConfigMaps are useful:

How Does a ConfigMap Work?

A ConfigMap can store configuration data as:

Once created, ConfigMaps can be referenced by pods using environment variables, volume mounts, or command-line arguments.

Basic Example: Creating a ConfigMap

A simple ConfigMap storing database configuration settings:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  DATABASE_URL: "postgres://db.example.com:5432"
  LOG_LEVEL: "debug"

After creating the ConfigMap with kubectl apply -f configmap.yaml, applications can consume it.

Using a ConfigMap in a Pod

A pod can reference the ConfigMap via environment variables:

apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: my-app
    image: my-app-image
    env:
    - name: DATABASE_URL
      valueFrom:
        configMapKeyRef:
          name: app-config
          key: DATABASE_URL

Or as a mounted file:

apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: my-app
    image: my-app-image
    volumeMounts:
    - name: config-volume
      mountPath: "/etc/config"
  volumes:
  - name: config-volume
    configMap:
      name: app-config

Now, the application can read /etc/config/DATABASE_URL like a regular file.

Common Challenges with ConfigMaps

1. ConfigMap Updates and Rolling Deployments

ConfigMaps are not automatically updated in running pods. If a ConfigMap is modified, the associated pod won’t automatically detect the changes unless:

2. Size Limitations

3. Security Considerations

Best Practices for Using ConfigMaps

Popular Tools for Managing ConfigMaps

Further Reading