Skip to main content
Kubernetes coreLesson 9 of 13

Config and Secrets (ConfigMap · Secret)

Values like the log level or a DB password are kept outside the image. Follow how they get into a Pod, and when a changed value actually takes effect.

ConfigMap valueinfo
Value via env var—
Value via file—
Pod · webUserkubectlConfigMapweb-configLOG_LEVEL=infoSecretdb-credentialpasswordkubeletnode agentEnv varsLOG_LEVEL etc.Containerweb appConfig file/etc/configcreatecreate
RequestResponse / reportWatchAppears in a later step

What you will learn

  • What goes in a ConfigMap vs a Secret
  • Why a Secret is not encrypted
  • Environment variables vs file mounts
  • Why the app keeps the old value after a change

Environment variables vs file mounts​

Environment variablesFile mount
When the app reads itOnce, at program startWhenever it reads the file at the given path
When the ConfigMap/Secret changesTakes effect only after the Pod restartsThe file updates automatically after a delay; the app must re-read it
Good forA few short valuesWhole config files, certificates

Try it yourself​

kubectl get configmap <name> -o yaml # Stored values, all strings
kubectl get secret <name> -o jsonpath='{.data.<key>}' | base64 -d # base64 decodes in one step
kubectl auth can-i get secrets # Can you read Secrets?
kubectl exec <pod> -- env # Env values the running container got
kubectl exec <pod> -- cat /etc/config/LOG_LEVEL # Mounted file value (path from the lesson)

Check yourself

  1. You changed LOG_LEVEL in the ConfigMap from info to debug. The app gets it as an environment variable and still logs at info. Why, and what fixes it?
  2. A teammate says Secret values are encrypted, so committing the Secret YAML to Git is fine. Is that right?
  3. The ConfigMap is mounted as files, and the file under /etc/config now shows the new value, but the app behaves the same. What is the likely cause?

Further reading: ConfigMaps · Secrets