Skip to content

ConfigMaps and Secrets — getting configuration into Pods

Use ConfigMaps and Secrets safely — env vars vs files, immutable maps, secret types, RBAC, and the limits you need to know before storing real credentials.

ConfigMaps and Secrets are how you keep configuration out of container images. They look almost identical in the API — key/value maps with a few flags — but they behave differently in practice, and Secrets in particular have edges that catch people: base64 is not encryption, “stringData” hides the difference, and projected mounts update on a schedule, not instantly. This article covers what each is for, the patterns that work in production, and the encryption-at-rest reality check most teams skip.

How to verify

What’s in the cluster and what each workload is consuming:

kubectl get cm,secret -A
kubectl describe deployment/web | grep -A2 -E 'Mounts|Environment'
kubectl get secret app-db -o jsonpath='{.data}' | jq 'keys'

describe on the Deployment surfaces every ConfigMap/Secret reference (env, envFrom, volume mounts). jq 'keys' on the secret data shows the field names without dumping values. To confirm encryption at rest is on:

sudo grep -A5 'encryption-provider-config' /etc/kubernetes/manifests/kube-apiserver.yaml || \
  echo "encryption-provider-config NOT configured"

What’s happening

A ConfigMap is a namespaced object with a data map (string-to-string) and an optional binaryData map (string-to-base64). A Secret is the same shape, but data is base64-encoded by convention, the type field can carry semantics (Opaque, kubernetes.io/tls, kubernetes.io/dockerconfigjson, kubernetes.io/service-account-token), and the API server will redact values in logs by default. Both are stored in etcd. Neither is encrypted on disk unless you configure EncryptionConfiguration on the kube-apiserver — base64 is encoding, not encryption.

Pods consume ConfigMaps and Secrets three ways: as environment variables (env.valueFrom.configMapKeyRef, env.valueFrom.secretKeyRef), as bulk env (envFrom.configMapRef, envFrom.secretRef), or as files (volumes.configMap, volumes.secret). Files are mounted via a projected emptyDir that the kubelet refreshes on a cycle (default 60s); env vars are baked in at Pod start and only change when the Pod restarts.

ConfigMaps and Secrets have a 1 MiB per-object limit (etcd default). Marking either immutable: true lets the kubelet skip re-fetching on every poll, which is meaningful at scale — but you can no longer edit them in place, only delete+create.

The procedure

  1. Create a ConfigMap from literals and files. The two forms cover most cases:

    kubectl create configmap app-config \
      --from-literal=LOG_LEVEL=info \
      --from-literal=MAX_CONN=200 \
      --from-file=nginx.conf=./nginx.conf
  2. Mount the ConfigMap as both env and a file in the same Pod:

    spec:
      containers:
        - name: web
          image: registry.example.com/web:1.4.2
          envFrom:
            - configMapRef: { name: app-config }
          volumeMounts:
            - name: nginx-conf
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf
      volumes:
        - name: nginx-conf
          configMap:
            name: app-config
            items: [{ key: nginx.conf, path: nginx.conf }]
  3. Create a Secret — use stringData so you don’t have to base64 by hand. The API server encodes it for you and reads it back as base64 from data.

    apiVersion: v1
    kind: Secret
    metadata:
      name: app-db
    type: Opaque
    stringData:
      DATABASE_URL: postgres://app:strong@db:5432/app
      SESSION_KEY: 9c1f0c92e6...
  4. Reference it from a Pod. The convention is: never put database credentials in a ConfigMap, never put TLS certs in a ConfigMap, always use a Secret with the right type.

    spec:
      containers:
        - name: web
          envFrom:
            - secretRef: { name: app-db }
  5. Turn on encryption at rest for Secrets in etcd. Generate a 32-byte key, write an EncryptionConfiguration, and add --encryption-provider-config=<path> to the kube-apiserver:

    apiVersion: apiserver.config.k8s.io/v1
    kind: EncryptionConfiguration
    resources:
      - resources: ['secrets']
        providers:
          - aescbc:
              keys:
                - name: key1
                  secret: <base64-of-32-random-bytes>
          - identity: {}

    Then re-encrypt existing secrets by re-writing them: kubectl get secret -A -o json | kubectl replace -f -.

  6. Mark high-traffic ConfigMaps immutable to reduce kubelet load:

    apiVersion: v1
    kind: ConfigMap
    metadata: { name: feature-flags }
    immutable: true
    data: { FEATURE_X: 'on' }
  7. Restrict who can read Secrets with RBAC. The default view and edit ClusterRoles can read Secrets — too permissive for most teams; create a tighter role and bind it deliberately.

    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata: { namespace: app, name: secrets-reader }
    rules:
      - apiGroups: [""]
        resources: ["secrets"]
        resourceNames: ["app-db", "app-tls"]
        verbs: ["get"]

Common pitfalls

  • Base64 is not encryption — anyone with get secrets permission on the namespace can read the value; treat RBAC as the actual access control.
  • Env-var consumption does not hot-reload — changing a ConfigMap referenced via envFrom requires a Pod restart (a Deployment rollout); file mounts do refresh on the kubelet sync interval.
  • The 1 MiB object limit is real — large config files (a whole nginx tree, big TLS bundles) need to be split or moved to a CSI store; the API server rejects oversized objects.
  • subPath mounts don’t get the auto-update behaviour of full directory mounts — the file is materialized once at Pod start.
  • Without EncryptionConfiguration, an etcd backup is a plain-text dump of every Secret — back up etcd somewhere as protected as the database itself.

We operate ConfigMap and Secret hygiene for clients with encryption at rest, RBAC scoped per workload, and an external secret store (Vault, SOPS, cloud KMS) for the things that should never sit in etcd in the first place. See /en/services/managed-operations/.