Knowledge

ConfigMaps and Secrets: separating configuration from your application image on Kubernetes

Updated 2026-11-22 · Present

  • A ConfigMap holds ordinary configuration and a Secret holds sensitive data; both separate configuration from the image so one image can run in many environments.
  • They can reach a Pod as environment variables or as mounted files; environment variables are read only when the Pod starts.
  • A Secret is only base64-encoded, not securely encrypted: use strict RBAC and consider enabling encryption at rest.

Introduction

A familiar principle when packaging applications: the same image must run in both test and production, and only the configuration differs. If you bake a database address or a password into the image, you must rebuild per environment and accidentally spread secrets everywhere the image is copied. Kubernetes offers two objects to separate this: ConfigMap and Secret. This article follows on from updating and rolling back applications, because a configuration change is also a kind of change that needs careful management.

ConfigMap: non-sensitive configuration

A ConfigMap is a set of key-value pairs (or the contents of whole files). The maximum size of one ConfigMap is 1 MiB. Create one with a command:

kubectl -n thu-nghiem create configmap app-config \
  --from-literal=LOG_LEVEL=info \
  --from-literal=APP_MODE=production
kubectl -n thu-nghiem get configmap app-config -o yaml

Or declare it in YAML:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
  namespace: thu-nghiem
data:
  LOG_LEVEL: "info"
  APP_MODE: "production"
  app.properties: |
    cache.ttl=60
    feature.new-ui=false

Secret: sensitive data

A Secret is for passwords, tokens, TLS keys and so on. The syntax is similar to a ConfigMap:

kubectl -n thu-nghiem create secret generic db-secret \
  --from-literal=DB_PASSWORD='change-this-password'

The sample value above is only an illustration; never put a real password in a file in Git or in a shared command history. In the Secret's data field, values are stored as base64:

kubectl -n thu-nghiem get secret db-secret -o jsonpath='{.data.DB_PASSWORD}' | base64 -d

This shows a point to remember: base64 is only a representation, not encryption. Anyone who can read the Secret can decode it immediately. Therefore:

Getting configuration into a Pod

There are two main ways, and you can use both in one Deployment:

spec:
  containers:
    - name: web
      image: nginx:1.27
      envFrom:
        - configMapRef:
            name: app-config
      env:
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: DB_PASSWORD
      volumeMounts:
        - name: cau-hinh
          mountPath: /etc/app
          readOnly: true
  volumes:
    - name: cau-hinh
      configMap:
        name: app-config
        items:
          - key: app.properties
            path: app.properties

How to apply new configuration

Because Pods do not restart on their own when a ConfigMap changes, the common approach is:

kubectl -n thu-nghiem apply -f app-config.yaml
kubectl -n thu-nghiem rollout restart deployment/web
kubectl -n thu-nghiem rollout status deployment/web

This way the new configuration goes through the same rolling mechanism and can be rolled back like a normal update. Another approach is to put a version or hash in the ConfigMap name and edit the Deployment to point to the new name; then every configuration change leaves a revision in the history. For configuration that never changes, set immutable: true on the ConfigMap or Secret to prevent accidental edits and reduce load on the API server; to change it, you must create a new object.

Common mistakes

Summary

ConfigMaps handle ordinary configuration, Secrets handle sensitive data, and both let one image travel across environments. Remember that a Secret is not secure by itself: it needs access control, encryption at rest and a management process. When configuration changes, use rollout restart so Pods pick up the new values in a controlled way.

References