ConfigMaps & Secrets

Difficulty: Intermediate

Question

What is the difference between a ConfigMap and a Secret in Kubernetes, how do Pods consume them, and are Secrets actually secure?

Answer

A ConfigMap stores non-sensitive configuration (feature flags, URLs, config files) and a Secret stores sensitive data (passwords, API keys, TLS certificates). Both decouple configuration from the container image and can be injected as environment variables or mounted as files. The key catch: Secret values are only base64-encoded, not encrypted, so their security depends on RBAC, encryption at rest and how you handle them.

How Pods consume them

  • Environment variables: env.valueFrom.configMapKeyRef / secretKeyRef for single keys, or envFrom to import all keys.
  • Volume mounts: each key becomes a file, for example /etc/config/app.yaml. Secret volumes are backed by tmpfs, so they never touch the node's disk.
  • Command-line args built from env vars.

Update behaviour differs. Env vars are read once at container start, so a change needs a Pod restart (kubectl rollout restart deployment/api). Mounted volumes are refreshed by the kubelet after a short delay, unless you used subPath, which never updates.

Side-by-side comparison

AspectConfigMapSecret
Intended dataNon-sensitive configCredentials, tokens, certificates
Storage formatPlain textBase64-encoded in the API
Size limit1 MiB1 MiB
Volume backingRegular volumetmpfs (in memory)
Special typesNonekubernetes.io/tls, dockerconfigjson, basic-auth and more
Encryption at restPossible but uncommonShould be enabled via EncryptionConfiguration or KMS

Are Secrets really secret?

Out of the box, not very. Anyone with get on Secrets in a namespace can read them, and anyone who can create a Pod in that namespace can mount them. Base64 is reversible with base64 -d. To harden them:

  1. Enable encryption at rest in etcd with a KMS provider (AWS KMS, GCP KMS, Vault). Managed clusters like EKS and GKE offer this as a setting.
  2. Lock down RBAC: give apps no list or watch on Secrets, and avoid * verbs.
  3. Never commit Secret YAML to Git. Use Sealed Secrets, SOPS, or the External Secrets Operator to sync from AWS Secrets Manager, GCP Secret Manager or HashiCorp Vault. The Secrets Store CSI driver can mount them directly without creating a Secret object.
  4. Prefer volume mounts over env vars for highly sensitive data, since env vars can leak through crash dumps, /proc and logging of the environment.
  5. Mark stable config immutable: true to prevent accidental edits and reduce API server watch load.

Real-world example

A UPI payments service might keep PSP_BASE_URL and LOG_LEVEL in a ConfigMap, while the PSP API key and database password come from Vault through External Secrets. Rotating the key in Vault updates the Secret, and a reloader or rollout restart picks it up.

Code examples

ConfigMap and Secret consumed as env vars and files

apiVersion: v1
kind: ConfigMap
metadata:
  name: payments-config
data:
  LOG_LEVEL: info
  PSP_BASE_URL: https://psp.example.in
---
apiVersion: v1
kind: Secret
metadata:
  name: payments-secret
type: Opaque
stringData:                  # plain text here; stored base64-encoded
  DB_PASSWORD: change-me
---
apiVersion: v1
kind: Pod
metadata:
  name: payments
spec:
  containers:
    - name: app
      image: ghcr.io/acme/payments:1.0.0
      envFrom:
        - configMapRef:
            name: payments-config
      volumeMounts:
        - name: creds
          mountPath: /etc/creds  # file /etc/creds/DB_PASSWORD
          readOnly: true
  volumes:
    - name: creds
      secret:
        secretName: payments-secret

Config is injected as env vars, while the password is mounted as a read-only file on tmpfs, which is safer and can update without restarting the container.

Proving base64 is not encryption

kubectl create secret generic demo --from-literal=token=s3cr3t
kubectl get secret demo -o jsonpath='{.data.token}' | base64 -d

Anyone with read access to the Secret can decode it, which is why RBAC and encryption at rest matter.

Key points

  • ConfigMap for non-sensitive config, Secret for credentials; both max 1 MiB.
  • Secret data is base64-encoded, not encrypted, unless encryption at rest is configured.
  • Env vars need a Pod restart to pick up changes; mounted volumes refresh (except subPath).
  • Secret volumes are tmpfs-backed and never written to node disk.
  • Use External Secrets, Sealed Secrets or a CSI driver instead of committing Secrets to Git.

Common follow-up questions

  • How would you rotate a database password without downtime?
  • How do you enable encryption at rest for Secrets?
  • Why might env vars be less safe than mounted files for secrets?
  • How does the External Secrets Operator work?

Interview tips

  • Say 'base64 is encoding, not encryption' early; interviewers often ask this specifically.
  • Show a full security story: encryption at rest, RBAC, external secret store, no secrets in Git.
  • Mention the restart-on-change behaviour for env vars; it is a frequent production surprise.

Concepts covered

ConfigMap, Secret, Base64 Encoding, Encryption at Rest, External Secrets

More Kubernetes & Orchestration questions