ConfigMaps and Secrets: Injecting Configuration Without Rebuilding Images

Kubernetes configmaps and secrets: env vars vs mounted files, rolling pods on change, base64 vs encryption and external secret managers.

Executive Summary: ConfigMaps hold non-sensitive configuration and Secrets hold sensitive values, and both let one image run unchanged in every environment. Pods consume them as environment variables or as mounted files. Environment variables are read once at startup and never change; mounted files update in place after a delay, unless you used subPath. Secrets are only base64-encoded by default, so real protection comes from encryption at rest, tight RBAC and, for most production setups, syncing from an external secret manager rather than committing Secret manifests to Git.
Isometric YAML cards for a ConfigMap and a Secret with a padlock feeding configuration into a pod

A team updates a feature flag in a ConfigMap and waits. Nothing changes. They restart one pod, which picks up the new value, while the other four keep the old one. Elsewhere, a developer commits a Secret manifest to Git, reasoning that the values are “encrypted” because they look like random characters. They are base64, which anyone can decode in one command.

A ConfigMap is a Kubernetes object that stores key-value pairs or whole files of configuration. A Secret has the same shape but is intended for sensitive data: passwords, tokens, keys and certificates. Kubernetes handles Secrets somewhat more carefully, for example keeping them in memory on nodes rather than on disk, but neither object is magic. This is the Kubernetes version of the runtime configuration ideas from the Docker secrets article.

Creating them

apiVersion: v1
kind: ConfigMap
metadata:
  name: billing-config
  namespace: billing
data:
  LOG_LEVEL: info
  PAYMENT_PROVIDER: stripe
  app.yaml: |
    retries: 3
    timeouts:
      gateway: 5s
---
apiVersion: v1
kind: Secret
metadata:
  name: billing-secrets
  namespace: billing
type: Opaque
stringData:                 # plain text here; stored base64-encoded
  DATABASE_PASSWORD: change-me
  STRIPE_API_KEY: sk_test_example
kubectl create configmap billing-config --from-file=app.yaml --from-literal=LOG_LEVEL=info
kubectl create secret generic billing-secrets --from-literal=DATABASE_PASSWORD=change-me
kubectl create secret tls billing-tls --cert=tls.crt --key=tls.key

Both are limited to 1 MiB, which is plenty for configuration and certificates and a signal that large data belongs elsewhere. Besides Opaque, common Secret types include kubernetes.io/tls for certificates and kubernetes.io/dockerconfigjson for private registry credentials referenced by imagePullSecrets.

Consuming them: environment variables or files

spec:
  containers:
    - name: api
      image: ghcr.io/acme/billing-api:1.9.0
      envFrom:
        - configMapRef:
            name: billing-config            # every key becomes an environment variable
      env:
        - name: DATABASE_PASSWORD
          valueFrom:
            secretKeyRef:
              name: billing-secrets
              key: DATABASE_PASSWORD
      volumeMounts:
        - name: config
          mountPath: /etc/billing           # app.yaml appears as /etc/billing/app.yaml
          readOnly: true
        - name: secrets
          mountPath: /run/secrets/billing
          readOnly: true
  volumes:
    - name: config
      configMap:
        name: billing-config
        items:
          - key: app.yaml
            path: app.yaml
    - name: secrets
      secret:
        secretName: billing-secrets
        defaultMode: 0400
Environment variables Mounted files
Picked up Once, when the container starts At start, then refreshed by the kubelet
Updates Never; requires a restart Eventually, typically within a minute or two
Exposure risk Inherited by child processes, often printed in logs and crash reports Readable only by processes with file access; permissions controllable
Best for Simple flags read at startup Config files, certificates, secrets

One trap: a ConfigMap or Secret mounted with subPath, to place a single file into an existing directory, never receives updates. The kubelet updates mounted volumes by swapping a symlink, which subPath bypasses.

Rolling out configuration changes

Even when mounted files update, most applications read configuration once at startup. Rather than relying on file reloads, make a configuration change trigger a normal rolling update, so every pod restarts with the new values in a controlled way. Three common approaches:

  • Hash annotation. Add an annotation to the pod template containing a checksum of the configuration, for example checksum/config. Helm charts often do this with a template function. Any config change changes the pod template, which triggers a rollout.
  • Versioned names. Kustomize’s configMapGenerator appends a content hash to the ConfigMap name and updates references automatically. A changed config is a new object, so the Deployment changes too, and old pods keep the old config until they are replaced, which makes rollbacks clean.
  • Manual restart. kubectl rollout restart deployment/billing-api after applying the change, acceptable for small teams.

Marking ConfigMaps and Secrets immutable: true pairs well with versioned names: it prevents accidental edits and reduces load on the API server, because kubelets stop watching immutable objects for changes.

Secrets are encoded, not encrypted

The data field of a Secret holds base64, which is an encoding, not encryption. echo Y2hhbmdlLW1l | base64 -d prints change-me. Protecting Secrets takes several layers:

  • Encryption at rest. By default, Secrets are stored unencrypted in etcd. Configure the API server’s encryption so etcd holds ciphertext; Kubernetes 1.29, released in December 2023, made the KMS v2 provider generally available, which lets a cloud key management service hold the master key. Managed services such as EKS, GKE and AKS offer envelope encryption with their KMS as a setting; turn it on.
  • RBAC. Grant get, list and watch on Secrets narrowly. list returns the values of every Secret in a namespace. Remember too that anyone who can create pods in a namespace can mount any Secret in it, so pod creation rights are effectively Secret read rights.
  • Keep them out of Git. Plain Secret manifests in a repository are plain text with extra steps.

Secrets in a GitOps world

If manifests live in Git, Secrets need a different path. The common options:

  • External Secrets Operator syncs values from AWS Secrets Manager, Google Secret Manager, Azure Key Vault, HashiCorp Vault and others into Kubernetes Secrets. Git holds only a reference to the external secret.
  • Secrets Store CSI Driver mounts secrets from those same stores directly as files in pods, optionally without creating Kubernetes Secret objects at all.
  • Sealed Secrets or SOPS encrypt secret manifests so the ciphertext can be committed, and decrypt them inside the cluster or in the deployment pipeline.

For production, an external secret manager is usually the best long-term choice: one place to rotate, audit and control access, shared with non-Kubernetes systems.

Frequently Asked Questions

What is the difference between a ConfigMap and a Secret?

Both store key-value data for pods. ConfigMaps are meant for non-sensitive configuration. Secrets are meant for sensitive values and get extra handling, such as in-memory storage on nodes and separate RBAC, but they are only base64-encoded unless encryption at rest is enabled.

Do pods pick up ConfigMap changes automatically?

Environment variables never update; the pod must restart. Mounted files are refreshed by the kubelet after a short delay, except when mounted with subPath. Many applications still need a restart to re-read files, so trigger a rollout on configuration changes.

Are Kubernetes Secrets secure?

Not by default. They are base64-encoded and, unless configured otherwise, stored unencrypted in etcd. Enable encryption at rest, restrict access with RBAC, and consider syncing from an external secret manager.

How do I store Kubernetes Secrets in Git safely?

Do not commit plain Secret manifests. Use Sealed Secrets or SOPS to commit encrypted versions, or use External Secrets Operator so Git only contains references to secrets stored in a dedicated secret manager.

One image, many environments

ConfigMaps and Secrets are what let a single tested image run in development, staging and production. Prefer mounted files for anything sensitive or complex, make configuration changes roll out like code changes, and treat Secrets as a delivery mechanism that needs encryption, access control and a real secret store behind it. The official documentation is worth keeping open: the ConfigMap documentation and the Secret documentation. Related reading on this site: RBAC and ServiceAccounts and Helm vs Kustomize.

Originally posted on 19 March 2024 and post last modified on 19 March 2024

Configuration DevOps Kubernetes Secrets Management

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *