Difficulty: Intermediate
What is the difference between a ConfigMap and a Secret in Kubernetes, how do Pods consume them, and are Secrets actually secure?
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.
env.valueFrom.configMapKeyRef / secretKeyRef for single keys, or envFrom to import all keys./etc/config/app.yaml. Secret volumes are backed by tmpfs, so they never touch the node's disk.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.
| Aspect | ConfigMap | Secret |
|---|---|---|
| Intended data | Non-sensitive config | Credentials, tokens, certificates |
| Storage format | Plain text | Base64-encoded in the API |
| Size limit | 1 MiB | 1 MiB |
| Volume backing | Regular volume | tmpfs (in memory) |
| Special types | None | kubernetes.io/tls, dockerconfigjson, basic-auth and more |
| Encryption at rest | Possible but uncommon | Should be enabled via EncryptionConfiguration or KMS |
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:
list or watch on Secrets, and avoid * verbs./proc and logging of the environment.immutable: true to prevent accidental edits and reduce API server watch load.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.
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.
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.
ConfigMap, Secret, Base64 Encoding, Encryption at Rest, External Secrets