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.
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.
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-credentialsVerify 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 yamlMounting 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
Secretobjects in-cluster. - Secrets Store CSI Driver: mounts secrets directly from the external vault into Pod tmpfs without creating a Kubernetes
Secretinetcdat all.
Gotchas
- Base64 is not encryption:
echo "c2VjcmV0" | base64 -dreveals the value immediately. - etcd encryption at rest is off by default in vanilla Kubernetes; enable
EncryptionConfigurationwithkms(v2) onkube-apiserver. - RBAC over-permissioning: granting
get/listonsecretscluster-wide lets any compromised Pod or CI job read every namespace's credentials. - Immutable ConfigMaps/Secrets: setting
immutable: trueprevents accidental edits and reduceskube-apiserverwatch load on large clusters.
Production checklist
- Enable
EncryptionConfiguration(KMS v2) onkube-apiserverforsecretsinetcd - Restrict
secretsRBAC to least-privilege namespaces and specific resource names - Keep raw
Secretmanifests out of Git (use ESO, CSI driver, or SealedSecrets/SOPS) - Prefer
volumeMounts(withoutsubPath) or trigger rollouts on config hash changes - Avoid printing
envor 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
Related Notes
Requests, Limits, QoS & OOMKilled
How Kubernetes CPU/memory requests and limits shape QoS classes, CPU throttling vs OOMKilled (exit 137), plus a fictional training postmortem example.
Helm Basics: Charts, Values, Releases, and Kustomize
Package parameterized Kubernetes manifests with Helm charts, manage releases and rollbacks, and know when Kustomize is simpler.