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. data holds UTF-8 text, binaryData holds 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. Y2hhbmdlLW1l is just change-me, one decode away.
ConfigMapSecret
Made forPlain settingsSmall confidential values
Value fieldsdata, binaryDatadata (base64), stringData (plain text, write-only)
typeNoneOpaque if not set
In etcdPlaintextPlaintext too, by default
At restEncryption is opt-inEncryption is opt-in
On nodesNothing specialOnly nodes whose Pods need it, in tmpfs, gone with the Pod
describePrints valuesByte counts only, but a token Secret's token in full
Volume fieldconfigMap.namesecret.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.

ConfigMap / Secret stored API server + etcd watch kubelet cache get / watchif RBAC allows envread once volumeup to ~90 s subPathread once APIlive watch restart Pod app re-reads restart Pod app reacts
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 asPod spec fieldSees edits ?To pick up a change
One key as env varconfigMapKeyRef / secretKeyRefNoRestart the Pods
All keys as env varsenvFrom : configMapRef / secretRefNoRestart the Pods
Command / args$(VAR_NAME) from an env varNoRestart the Pods
Volume, whole mountvolumes[].configMap / .secretYes, within ~90 sApp re-reads the file
Volume with subPathvolumeMounts[].subPathNoRestart the Pods
Kubernetes APIApp code, get / watchYes, liveApp handles the event
Immutable objectimmutable: trueLockedNew 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.

In short : ConfigMaps hold plain settings, Secrets hold small confidential values. Env vars are copied once, whole-volume files follow edits eventually, and base64 protects nothing.

1.Who can read a Secret ?

etcd or a backupplain unless encrypted get secretsreads that one list / watch secretsreads every value in ns Secretstored in etcd sent tothe node create Podsin that namespace Pod mounts itany Secret in its ns node tmpfs copydeleted with the Pod privileged containeron the same node

Default cluster, no hardening : every arrow is a way in. Close them one by one.

  • etcdEncryption at rest (this path only).
  • RBAClist or watch on 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
Secret write any client kube-apiservernew DEK per write, AES-GCM KMS v2 plugingRPC, UNIX socket wrap Remote KMSholds the KEK etcdk8s:enc:kms:v2:... ciphertext +wrapped DEK stolen etcd copy: not enough

3.Secret types

All but Opaque and bootstrap start with kubernetes.io/.

OpaqueAny keys, the default
tlstls.crt + tls.key
dockerconfigjsonfor imagePullSecrets, as is dockercfg
service-account-tokenlegacy, not auto-made since v1.24

Also 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).
  • binaryData keys 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

CommandWhat it does
kubectl create configmap NAME --from-file=FILEAlso --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='{.data.KEY}' | base64 --decodeRead one value. Base64 hides nothing.
kubectl create secret ... --dry-run=client -o yamlPrints plain base64. Keep it out of Git, or encrypt it.
kubectl auth can-i list secrets --as=USER -n NSCan USER read every Secret in NS ?
kubectl get secrets -A -o json | kubectl replace -f -Rewrite all Secrets with the current first provider.
In short : Anyone with etcd access, 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.