ConfigMaps and Secrets
Draft: not yet run against a live cluster. Treat output as representative and check it on yours.
Two questions come up constantly: “I changed the ConfigMap, why didn’t the app see it?” and “Is our Secret actually secret?” Both have precise answers, and the second one matters a lot in a regulated environment.
Under the hood
Section titled “Under the hood”-
The objects live in etcd. Secrets are stored unencrypted unless the API server is configured with an
EncryptionConfiguration(ideally backed by a KMS provider). Managed platforms vary in what they enable by default. -
The kubelet fetches them for pods on its node. When a pod starts, the kubelet reads the referenced objects. If one is missing, or a referenced key is missing, the container can’t start:
CreateContainerConfigError. Mark a referenceoptional: trueif it’s genuinely optional. -
Environment variables are fixed at container start. The process gets a copy. Changing the ConfigMap later changes nothing for running containers.
-
Volume files are refreshed. The kubelet updates projected files atomically through a symlink swap. The app still has to notice: re-read the file, or watch it.
-
Secret volumes are held in memory (tmpfs) on the node, not written to the node’s disk.
Minimal example
Section titled “Minimal example”kubectl create configmap app-config --from-literal=GREETING=hellokubectl create secret generic app-secret --from-literal=API_KEY=s3cr3tkubectl get secret app-secret -o jsonpath='{.data.API_KEY}' | base64 -d; echoThe last line prints s3cr3t. Anyone allowed to get that Secret can read it, which is the point of the RBAC advice below.
apiVersion: v1kind: Podmetadata: name: consumerspec: containers: - name: app image: busybox:1.36 command: ['sh', '-c', 'while true; do echo "env=$GREETING file=$(cat /config/GREETING)"; sleep 10; done'] env: - name: GREETING valueFrom: { configMapKeyRef: { name: app-config, key: GREETING } } volumeMounts: - { name: config, mountPath: /config } volumes: - name: config configMap: { name: app-config }Apply it, change the ConfigMap with kubectl create configmap app-config --from-literal=GREETING=bye --dry-run=client -o yaml | kubectl apply -f -, and watch kubectl logs -f consumer. After a short delay, file= changes to bye. env= never does.
How it breaks
Section titled “How it breaks”Self-check
Section titled “Self-check”You changed a ConfigMap consumed through envFrom. When will the running container see the new value?
Never. Environment variables are set at container start. You have to restart the pods, for example with a rollout.
Is a Kubernetes Secret encrypted?
Not by default. Values are base64-encoded. Encryption at rest in etcd requires API server configuration, ideally with a KMS provider.
Can a pod in namespace a mount a Secret from namespace b?
No. ConfigMaps and Secrets can only be referenced from the same namespace.
Maps to: CKAD: Application Environment, Configuration and Security; CKA: Workloads and Scheduling. Check the current exam curriculum, since domain names change between revisions.