Core Kubernetes · 06
ConfigMaps and Secrets
1.What are they ?
Two core v1 objects that keep settings out of the container image. Same shape, different job.
- ConfigMapPlain config, no secrecy at all.
dataholds UTF-8 text,binaryDataholds bytes (base64 in YAML). - SecretA small confidential value (password, token, key), kept out of images and Pod specs.
- BothNamespaced, max 1 MiB. A Pod only references ones in its own namespace.
- Base64An encoding, not encryption.
Y2hhbmdlLW1lis just change-me, one decode away.
| ConfigMap | Secret | |
|---|---|---|
| Made for | Plain settings | Small confidential values |
| Value fields | data, binaryData | data (base64), stringData (plain text, write-only) |
| type | None | Opaque if not set |
| In etcd | Plaintext | Plaintext too, by default |
| At rest | Encryption is opt-in | Encryption is opt-in |
| On nodes | Nothing special | Only nodes whose Pods need it, in tmpfs, gone with the Pod |
| describe | Prints values | Byte counts only, but a token Secret's token in full |
| Volume field | configMap.name | secret.secretName |
2.How a Pod reads them
Four ways in : env vars, $(VAR) in command or args, files in a read-only volume, or app code calling the Kubernetes API. The kubelet watches each object and hands it to the Pod.
containers: - name: app image: nginx envFrom: # every key, once - configMapRef: name: app-config volumeMounts: - name: creds # files, refreshed mountPath: /etc/creds volumes: - name: creds secret: secretName: db-creds # not .name optional: true # start if missing
3.What sees an edit ?
| Read as | Pod spec field | Sees edits ? | To pick up a change |
|---|---|---|---|
| One key as env var | configMapKeyRef / secretKeyRef | No | Restart the Pods |
| All keys as env vars | envFrom : configMapRef / secretRef | No | Restart the Pods |
| Command / args | $(VAR_NAME) from an env var | No | Restart the Pods |
| Volume, whole mount | volumes[]. / .secret | Yes, within ~90 s | App re-reads the file |
| Volume with subPath | volumeMounts[]. | No | Restart the Pods |
| Kubernetes API | App code, get / watch | Yes, live | App handles the event |
| Immutable object | immutable: true | Locked | New name, then roll the Pods |
Restart = kubectl rollout restart deployment/NAME (see Deployments). A container restart also re-reads env, and subPath once its volume has updated. Volumes follow in 60 to 90 s, Pod by Pod; a Pod annotation refreshes at once. The kubelet skips watching immutable objects; reusing one's name can leave a node on the old copy.
1.Who can read a Secret ?
Default cluster, no hardening : every arrow is a way in. Close them one by one.
- etcdEncryption at rest (this path only).
- RBAC
listorwatchon secrets = read-all. - PodsPod-create = read. Split by namespace.
- NodesNo privileged containers. Mount Secrets only where needed.
- SourceExternal store, see External Secrets.
2.Encryption at rest
Off by default. Pass an EncryptionConfiguration with kube-apiserver --encryption-provider-config; edits need a restart unless its automatic reload flag is on. Use KMS v2 (stable v1.29), or secretbox (not aescbc) for a local key.
kind: EncryptionConfiguration apiVersion: apiserver.config.k8s.io/v1 resources: - resources: [secrets, configmaps] providers: - kms: # 1st encrypts writes apiVersion: v2 name: my-kms endpoint: unix:///run/kms.sock - identity: {} # no crypto, migration only
3.Secret types
All but Opaque and bootstrap start with kubernetes.io/.
tls.crt + tls.keyimagePullSecrets, as is dockercfgAlso built in : basic-auth, ssh-auth, dockercfg (legacy), bootstrap token. For SA tokens use kubectl create token.
4.Gotchas
immutable: true(GA v1.21) cannot be undone.- Encryption applies on write: rewrite old ones (
kubectl replace, StorageVersionMigration). binaryDatakeys never become env vars.- Static Pods can never use Secrets or ConfigMaps (v1.37).
- Many small Secrets eat memory. Cap the count (ResourceQuota).
5.Commands
| Command | What it does |
|---|---|
kubectl create configmap NAME --from-file=FILE | Also --from-literal, --from-env-file. Key = file name. |
kubectl create secret generic NAME --from-literal=k='v' | Opaque Secret. Single-quote special characters. |
kubectl get secret NAME -o jsonpath='{. | Read one value. Base64 hides nothing. |
kubectl create secret ... --dry-run=client -o yaml | Prints plain base64. Keep it out of Git, or encrypt it. |
kubectl auth can-i list secrets --as=USER -n NS | Can USER read every Secret in NS ? |
kubectl get secrets -A -o json | kubectl replace -f - | Rewrite all Secrets with the current first provider. |
list on secrets or Pod-create rights can read a Secret. Turn on encryption at rest (KMS v2), keep RBAC tight, and rewrite old objects after any key change.