Skip to Content
Mengty Learns
Kubernetes

ConfigMaps and Secrets in Kubernetes

Store non-sensitive config in ConfigMaps, understand why Secrets are only base64 by default, and configure encryption at rest or external secret managers.

how-tointermediateTested with kubernetes 1.36 (docs), kubectl 1.35.3Updated Nov 27, 2026

TL;DR

Use ConfigMaps for non-sensitive settings and Secrets for credentials—but remember Kubernetes Secrets are only base64-encoded by default, not encrypted. Enable etcd encryption at rest (EncryptionConfiguration + KMS), prefer volume mounts over env vars for rotation, and use an external secret manager so raw secrets never touch Git.

Why it exists

Hardcoding environment URLs, feature flags, or database passwords inside container images forces a full image rebuild for every environment change and leaks credentials to anyone who can pull the image. Separating configuration from the image lets one immutable container run across dev, staging, and production.

Mental model

ConfigMap and Secret are key-value API objects stored in etcd. kube-apiserver base64-encodes Secret.data for transport safety (arbitrary binary bytes in JSON/YAML), not confidentiality. Anyone with kubectl get secret -o yaml RBAC permission can decode it in one command unless you restrict RBAC and encrypt etcd at rest.

Both ConfigMaps and Secrets live in etcd; kubelet injects them into Pods as env vars or mounted files.

Do it

Create a ConfigMap and Secret declaratively (never commit real Secret values to Git—use placeholders or an external secret sync):

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  LOG_LEVEL: "info"
  API_TIMEOUT_MS: "3000"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: ghcr.io/example/web:1.4.2
          envFrom:
            - configMapRef:
                name: app-config
          volumeMounts:
            - name: db-creds
              mountPath: /etc/secrets/db
              readOnly: true
      volumes:
        - name: db-creds
          secret:
            secretName: db-credentials

Verify Secret creation and inspection commands (--dry-run=client does not require a live cluster):

# Preview Secret YAML without touching a cluster (not run against live cluster)
kubectl create secret generic db-credentials \
  --from-literal=username=appuser \
  --dry-run=client -o yaml
Gotcha:

Mounting a Secret or ConfigMap with subPath disables automatic kubelet updates—the file stays stale until the Pod is recreated. Also, env variables are read only at container startup, so updating a Secret requires a Pod rollout (e.g., via checksum annotation or Reloader).

External secret managers

In team and GitOps setups, committing encrypted or raw Secret YAML is risky. Instead:

  • External Secrets Operator (ESO): reads secrets from AWS Secrets Manager, GCP Secret Manager, Azure Key Vault, or HashiCorp Vault and reconciles Kubernetes Secret objects in-cluster.
  • Secrets Store CSI Driver: mounts secrets directly from the external vault into Pod tmpfs without creating a Kubernetes Secret in etcd at all.

Gotchas

  • Base64 is not encryption: echo "c2VjcmV0" | base64 -d reveals the value immediately.
  • etcd encryption at rest is off by default in vanilla Kubernetes; enable EncryptionConfiguration with kms (v2) on kube-apiserver.
  • RBAC over-permissioning: granting get/list on secrets cluster-wide lets any compromised Pod or CI job read every namespace's credentials.
  • Immutable ConfigMaps/Secrets: setting immutable: true prevents accidental edits and reduces kube-apiserver watch load on large clusters.

Production checklist

  • Enable EncryptionConfiguration (KMS v2) on kube-apiserver for secrets in etcd
  • Restrict secrets RBAC to least-privilege namespaces and specific resource names
  • Keep raw Secret manifests out of Git (use ESO, CSI driver, or SealedSecrets/SOPS)
  • Prefer volumeMounts (without subPath) or trigger rollouts on config hash changes
  • Avoid printing env or mounted secret files in application logs or crash dumps

Check yourself

Check Yourself

1. Why does Kubernetes base64-encode values in Secret.data by default?

2. You update a ConfigMap key that is injected into a Deployment via envFrom. When does the running container see the new value?

Sources