Learn Labs
Running Applications

Configuration

ConfigMaps, Secrets, environment variables, and the downward API

The same image runs in every environment; configuration comes from the cluster. The web Deployment gets it three ways:

apiVersion: v1kind: ConfigMapmetadata: { name: web-config }data:VERSION: v1STARTUP_SECONDS: "5"REDIS_HOST: redis---in the Deployment's container:envFrom:- configMapRef: { name: web-config }every key becomes an env varenv:- name: POD_NAMEthe downward API: facts about the pod itselfvalueFrom: { fieldRef: { fieldPath: metadata.name } }- name: NODE_NAMEvalueFrom: { fieldRef: { fieldPath: spec.nodeName } }

That's how every response can say which pod and node served it:

curl localhost:8180/api/whoami
{"pod": "web-5c6cf9586b-l5q7x", "node": "lab-worker2", "version": "v1"}

Changing a ConfigMap changes nothing (yet)

From rollouts.py, with traffic flowing:

1. ConfigMap VERSION=v2, 5 s later, pods still serve: {'v1': 88}

Environment variables are read when the container starts. Updating the ConfigMap doesn't restart anything, so running pods keep the old values until they're replaced, and new pods (from a crash or a scale-up) get the new ones. That's how a cluster ends up running two configurations at once, with nobody having deployed anything.

ConfigMaps mounted as files (volumes: - configMap:) are updated in place after a delay, but the app has to notice and reload; most don't. And subPath mounts never update.

Secrets

Secrets look like ConfigMaps with values base64-encoded:

kubectl -n shop create secret generic api-keys --from-literal=PAYMENT_KEY=sk_live_123
kubectl -n shop get secret api-keys -o jsonpath='{.data.PAYMENT_KEY}' | base64 -d
sk_live_123

Base64 is an encoding, not encryption. What actually protects Secrets:

  • RBAC: who may get or list Secrets in a namespace. list on Secrets is as good as reading all of them.
  • Encryption at rest in etcd (an EncryptionConfiguration on the API server; managed clusters offer KMS integration).
  • Not committing them: manifests in git hold references, and the values come from a secret manager via the External Secrets Operator, Sealed Secrets, or the CSI secrets driver.

Lab

  1. Change VERSION in the ConfigMap and request /api/whoami for a minute. Then delete one pod. What versions do you see now?
  2. Switch web.yaml to a kustomize configMapGenerator and change a value. What does kubectl apply -k do to the Deployment?
  3. Create a Secret, mount it as a file, and kubectl exec to read it.

On this page