Kubernetes ConfigMap

A ConfigMap stores non-sensitive configuration data as key-value pairs in Kubernetes, decoupling application configuration from container images.

A ConfigMap is a Kubernetes object that stores non-sensitive configuration data as key-value pairs. Its purpose is to decouple configuration from container images: instead of baking environment-specific settings (database URLs, feature flags, log levels) into the image, you inject them at runtime via a ConfigMap. This means the same image can run in development, staging, and production with different configurations.

ConfigMap vs Secret

Aspect ConfigMap Secret
Data type Non-sensitive (log levels, URLs, feature flags) Sensitive (passwords, tokens, TLS certificates)
Storage encoding Plain text in etcd Base64-encoded in etcd (not encrypted by default)
Mount in Pod As env var or volume file As env var or volume file
Recommended for Any config that is safe to read in cluster Credentials that should have restricted access
RBAC visibility Readable by anyone with get configmaps Readable by anyone with get secrets (requires separate RBAC policy to restrict)

Creating a ConfigMap

From a manifest:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
  namespace: default
data:
  APP_ENV: production
  LOG_LEVEL: info
  DB_HOST: postgres-svc.default.svc.cluster.local
  DB_PORT: "5432"
  application.properties: |
    server.port=8080
    spring.datasource.url=jdbc:postgresql://postgres-svc:5432/appdb
    management.endpoints.web.exposure.include=health,info

From the command line:

kubectl create configmap app-config \
  --from-literal=APP_ENV=production \
  --from-literal=LOG_LEVEL=info

# From a file
kubectl create configmap app-config --from-file=application.properties

# View the ConfigMap
kubectl get configmap app-config -o yaml

Consuming a ConfigMap in a Pod

As Environment Variables

apiVersion: v1
kind: Pod
metadata:
  name: spring-app
spec:
  containers:
    - name: spring-app
      image: eclipse-temurin:21-jre
      command: ["java", "-jar", "/app/app.jar"]
      env:
        - name: APP_ENV
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: APP_ENV
        - name: LOG_LEVEL
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: LOG_LEVEL
      # Or inject all keys at once:
      envFrom:
        - configMapRef:
            name: app-config

As a Volume (File Mount)

Useful when the application reads a configuration file from disk (Spring Boot application.properties, nginx.conf, etc.):

spec:
  volumes:
    - name: config-volume
      configMap:
        name: app-config
        items:
          - key: application.properties
            path: application.properties
  containers:
    - name: spring-app
      image: eclipse-temurin:21-jre
      command: ["java", "-jar", "/app/app.jar",
                "--spring.config.location=/config/application.properties"]
      volumeMounts:
        - name: config-volume
          mountPath: /config
          readOnly: true

Live Updates

When a ConfigMap is mounted as a volume, Kubernetes periodically syncs the mounted files to reflect ConfigMap changes (typically within 60 seconds, controlled by kubelet's --sync-frequency). Environment variables injected at pod startup are not updated live — the Pod must be restarted to pick up changes.

# Update a ConfigMap in place
kubectl edit configmap app-config

# Force Pods to pick up the new config (if using envFrom)
kubectl rollout restart deployment/spring-app

Common Mistakes

  • Storing secrets in ConfigMaps. Passwords, API keys, and TLS certificates belong in Secret objects, which have separate RBAC controls and can be encrypted at rest. A ConfigMap has no special protection.
  • Referencing a non-existent ConfigMap key. If configMapKeyRef.key does not exist in the ConfigMap, the Pod will fail to start with CreateContainerConfigError. Validate key names carefully.
  • Expecting immediate live updates when using envFrom. Environment variables are set at container startup. Changes to a ConfigMap do not propagate to running containers. You must restart the Pod.
  • Putting large binary data in ConfigMaps. ConfigMaps are designed for text configuration. Binary data belongs in a Secret (using binaryData) or an external store.
  • Not using namespaces. A ConfigMap is namespace-scoped. A Pod in namespace-a cannot directly reference a ConfigMap in namespace-b.

Best Practices

  • Use envFrom: configMapRef to inject all keys at once and reduce manifest verbosity, but be aware it pollutes the container's environment with every key in the ConfigMap.
  • Mount configuration files as volumes for applications that read config files from disk; this supports live reload without a Pod restart for frameworks that watch the filesystem.
  • Keep ConfigMaps small and single-purpose (one ConfigMap per application or component).
  • Store ConfigMap YAML in Git alongside your application manifests for version control and auditability.
  • Use a ConfigMap only for non-sensitive data; use Secret for anything that must be access-controlled.

Interview Questions

1. What is a ConfigMap and why is it useful?

A ConfigMap stores non-sensitive configuration data separately from container images. It allows the same image to run in different environments (dev/staging/prod) by injecting environment-specific values (URLs, flags, log levels) at Pod creation time, avoiding the need to rebuild images for config changes.

2. What are the two main ways to consume a ConfigMap in a Pod?

The first way is as environment variables using env.valueFrom.configMapKeyRef (for individual keys) or envFrom.configMapRef (for all keys). The second way is as a mounted volume, where each key in the ConfigMap becomes a file in a directory inside the container. Volume mounts support live updates; environment variables do not.

3. What is the difference between a ConfigMap and a Secret?

Both store key-value data. A ConfigMap is for non-sensitive configuration; its data is stored as plain text in etcd. A Secret is for sensitive data; its data is base64-encoded in etcd. The encoding is not encryption — it only prevents accidental display. Secrets can be encrypted at rest using EncryptionConfiguration. Secrets also have distinct RBAC permissions, making it easier to restrict who can read them.

4. Are changes to a ConfigMap immediately visible inside a running Pod?

It depends on how the ConfigMap is consumed. If mounted as a volume, the files are synced periodically (within ~60 seconds). If consumed as environment variables (via env or envFrom), the variables are set at container startup and do not change until the container is restarted.

5. What happens if a Pod references a ConfigMap key that does not exist?

If the ConfigMap itself does not exist when the Pod starts, or if the specified key is absent, the Pod fails to start with a CreateContainerConfigError status. You can make the reference optional by setting optional: true on the configMapKeyRef, in which case the environment variable is simply not set.

6. Can a ConfigMap be shared across multiple Pods?

Yes. Multiple Pods in the same namespace can reference the same ConfigMap. Changes to the ConfigMap (for volume-mounted configs) propagate to all Pods that mount it, making it an efficient way to manage shared configuration.

7. How would you update the configuration for a running Deployment without downtime?

Update the ConfigMap with kubectl edit configmap <name> or kubectl apply. If the ConfigMap is mounted as a volume, the files update within ~60 seconds and applications that watch the filesystem (e.g., using Spring Cloud Config's file watcher) pick up changes automatically. If environment variables are used, trigger a rolling restart with kubectl rollout restart deployment/<name>.

Summary

  • A ConfigMap decouples configuration from container images, enabling the same image to run in different environments.
  • Data can be injected as environment variables or as files via volume mounts; volume mounts support periodic live updates.
  • Use ConfigMap for non-sensitive data only; use Secret for passwords, tokens, and certificates.
  • A ConfigMap is namespace-scoped; Pods and ConfigMaps must be in the same namespace.
  • Store ConfigMap manifests in Git to track configuration changes alongside code changes.

Continue Learning

Previous: Service | Next: Secret

Was this guide helpful?

CodeWithVenu is free to read, with no paywall. If this guide saved you time or helped you prepare, a small voluntary contribution helps me write more practical tutorials, architecture guides, and interview-prep content.