Kubernetes ConfigMaps and Secrets Explained
In lesson 3 you built an image. Now your app needs a log level, which differs between your laptop and production, and a database password, which must not sit in an image anyone with pull access can open. This lesson explains Kubernetes ConfigMaps and Secrets: how they keep settings outside the image, how a pod reads them, and how to keep passwords safe.
Why settings live outside the image
Configuration is whatever changes between the places your app runs while the code stays the same: a log level, a database address, a password. An image is fixed once built, as in lesson 4. Bake settings in and you need one build for development, one for staging (the last test before real users) and one for production, so the image you tested is not the one you ship.
Keep settings outside and you build once and ship the exact image you tested. Lesson 6 did this with Docker, passing REDIS_HOST as an environment variable, a named setting handed to a process when it starts. ConfigMaps and Secrets are how Kubernetes stores such settings for pods.
ConfigMaps: settings as keys or whole files
A ConfigMap is a Kubernetes object that stores non-sensitive settings as key-value pairs. A value can be a short string or a whole configuration file. It holds at most 1 MiB and serves only pods in its own namespace, the named group of objects from lesson 8.
You can write one in YAML. This file, web-config.yaml, holds two short settings and one file:
apiVersion: v1
kind: ConfigMap
metadata:
name: web-config
data:
GREETING: namaste
LOG_LEVEL: info
app.properties: |
color=purple
max_items=20
Everything sits under data, and every value is a string. The | starts a YAML block: the indented lines below become one value.
You create a cluster in lesson 17, so read along for now. Without a YAML file, kubectl create configmap builds the same object: each --from-literal adds a key, and --from-file adds one named after the file, holding its contents:
kubectl create configmap web-config \
--from-literal=GREETING=namaste \
--from-literal=LOG_LEVEL=info \
--from-file=app.properties
You should see one line confirming it:
configmap/web-config created
If you already applied the YAML, you get an already exists error instead, because a name is used once per kind in a namespace. A wrong file name gives an error ending in no such file or directory, and nothing is created.
Secrets: base64 is not encryption
A Secret is an object much like a ConfigMap, for sensitive values such as passwords, API tokens and keys. It also holds key-value pairs up to 1 MiB, and pods use it the same ways, shown next. The difference is handling: access to Secrets is granted separately, and the kubelet keeps mounted Secret files in memory, not on the node's disk.
These commands create a Secret named db-pass, then print it as YAML:
kubectl create secret generic db-pass --from-literal=password=pa55
kubectl get secret db-pass -o yaml
generic gives the default type, Opaque, meaning data of your choice; the TLS Secret in lesson 12 was another type. You should see a created line, then YAML, trimmed here:
secret/db-pass created
apiVersion: v1
data:
password: cGE1NQ==
kind: Secret
type: Opaque
The password is not hidden. Base64 is an encoding, a fixed way to write any bytes with letters, digits and a few symbols so they fit safely in YAML. Encoding is not encryption: there is no key, so anyone who can read the Secret can reverse it:
kubectl get secret db-pass -o jsonpath='{.data.password}' | base64 --decode
This prints pa55. By default, the API server also stores Secrets unencrypted in etcd, the cluster's database from lesson 8. A Secret marks a value as sensitive; protection comes from the steps below.
Into the pod: variables or files
A pod uses a ConfigMap or Secret in two ways, chosen in the Deployment's pod template. As environment variables, values are copied into the container when it starts. As a mounted volume, each key becomes a file in a directory inside the container. In the diagram, notice that one ConfigMap feeds both routes at once.
Here is the pod template part of web.yaml from lesson 10 with both routes added. configMapKeyRef and secretKeyRef each pick one key; the volumes entry turns the whole ConfigMap into files, and volumeMounts places them at /etc/web:
spec:
containers:
- name: web
image: nginx:1.27-alpine
env:
- name: GREETING
valueFrom:
configMapKeyRef:
name: web-config
key: GREETING
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-pass
key: password
volumeMounts:
- name: config
mountPath: /etc/web
volumes:
- name: config
configMap:
name: web-config
nginx ignores these values; you only watch them arrive. To load every key of a ConfigMap as variables at once, use envFrom with a configMapRef. The diagram shows the steps from here.
Apply both files, wait for the rollout, then ask a container for its values. kubectl exec deploy/web runs a command in one pod of the Deployment:
kubectl apply -f web-config.yaml -f web.yaml
kubectl rollout status deployment/web
kubectl exec deploy/web -- printenv GREETING DB_PASSWORD
Trimmed to the last line of each step, you should see:
configmap/web-config created
deployment.apps/web configured
deployment "web" successfully rolled out
namaste
pa55
If web-config existed, the first line says configured or unchanged. A misspelt name shows in kubectl get pods: CreateContainerConfigError for a missing env source or key, or a pod stuck in ContainerCreating for a missing volume ConfigMap. Lesson 20 debugs both.
When a value changes
Now change LOG_LEVEL to debug and apply web-config.yaml again. Mounted files update by themselves, but not at once: the kubelet refreshes them on its periodic sync, one minute by default, plus a cache delay. Your app must also reread the file. A file mounted with subPath, which places one key at a chosen path, never updates.
Environment variables never change inside a running container: change GREETING and printenv still shows namaste. Editing a ConfigMap starts no rollout either, because the pod template did not change. To replace the pods with fresh ones that read the new values:
kubectl rollout restart deployment/web
It prints deployment.apps/web restarted. Forgetting this step is a classic mistake.
Keeping Secrets safe
Because base64 hides nothing, a Secret is only as safe as the cluster around it. The flowchart sorts settings, and the Kubernetes documentation lists four steps.
Restrict access with RBAC. Role-based access control, the permission system in lesson 16, decides who may get, list or watch Secrets. Grant these sparingly, since list returns the contents too. Anyone allowed to create pods in a namespace can also read its Secrets, by starting a pod that uses them.
Turn on encryption at rest. The API server then encrypts Secrets before writing them to etcd, so a copied etcd disk or backup is useless without the key. On your own cluster, you pass it an EncryptionConfiguration file with --encryption-provider-config; managed services offer it as a setting.
Never commit Secrets to Git. Committing a Secret's YAML hands the password to everyone who can read the repository, and its history keeps it after you delete the file. If one slips in, change the password.
Consider an external secret store. A store such as HashiCorp Vault or a cloud secrets manager keeps the real value outside the cluster, under its own access rules. Tools such as the Secrets Store CSI Driver, a storage plugin, mount values from it into pods as files.
Check yourself
Your app reads LOG_LEVEL from an environment variable. An hour after you change the ConfigMap, the pods still use the old level. Why, and what fixes it?
Variables are copied in when a container starts, and editing a ConfigMap does not change the pod template, so nothing restarted. Run kubectl rollout restart deployment/web, or mount the setting as a file the app rereads.
A teammate says a Secret's YAML is safe to commit because the values are base64-encoded. What is wrong?
Base64 is reversible by anyone, with no key. Everyone who can read the repository, including its history, now has the password: remove the file and change the password.
RBAC stops a developer from reading Secrets in a namespace, but they may create pods there. Can they still get the database password?
Yes: they can start a pod that loads the Secret and print it. Permission to create pods there amounts to reading its Secrets, so grant it as carefully.