Skip to content

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.

  1. 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.

  2. 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 reference optional: true if it’s genuinely optional.

  3. Environment variables are fixed at container start. The process gets a copy. Changing the ConfigMap later changes nothing for running containers.

  4. 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.

  5. Secret volumes are held in memory (tmpfs) on the node, not written to the node’s disk.

Terminal window
kubectl create configmap app-config --from-literal=GREETING=hello
kubectl create secret generic app-secret --from-literal=API_KEY=s3cr3t
kubectl get secret app-secret -o jsonpath='{.data.API_KEY}' | base64 -d; echo

The last line prints s3cr3t. Anyone allowed to get that Secret can read it, which is the point of the RBAC advice below.

consumer.yaml
apiVersion: v1
kind: Pod
metadata:
name: consumer
spec:
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.

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.