Sub topic 12
Passing Configuration into a Pod
This chapter assumes you can read a Pod manifest, know what a volume and a volume mount are, and have kubectl access to a cluster. You will learn three ways to get settings into a container without rebuilding its image: literal environment variables, ConfigMaps, and Secrets.
Why separate configuration from the image
An image should run unchanged in development, staging, and production. Settings such as a log level or a database host belong outside the image. Kubernetes gives you two objects for this. A ConfigMap holds non-sensitive key-value data. A Secret holds sensitive data such as passwords and tokens. A Pod consumes either one as environment variables or as files in a mounted volume.
Literal environment variables
The simplest option is the env field on a container, as in the first manifest of this tutorial:
env:
- name: GREETING
value: helloThis works for fixed values, but the value is stored in the Pod spec itself. Changing it means editing the manifest.
Using a ConfigMap
Create a ConfigMap from literal values:
kubectl create configmap app-config --from-literal=LOG_LEVEL=debug --from-literal=GREETING=helloThen reference it from a Pod. You can pull in one key with valueFrom, or all keys with envFrom.
apiVersion: v1
kind: Pod
metadata:
name: config-demo
spec:
containers:
- name: app
image: busybox:1.36
command:
- sh
- -c
- echo "$LOG_LEVEL $GREETING_MSG"; sleep 3600
env:
- name: GREETING_MSG
valueFrom:
configMapKeyRef:
name: app-config
key: GREETING
envFrom:
- configMapRef:
name: app-configHere GREETING_MSG receives one key under a new name. envFrom creates a variable for every key, so LOG_LEVEL and GREETING also appear.
Using a Secret
A Secret is used the same way, with secretKeyRef or secretRef:
kubectl create secret generic db-creds --from-literal=password=s3cretenv:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-creds
key: passwordMounting as files
Mounting is the second delivery method. Each key becomes a file, and the file content is the value.
spec:
containers:
- name: app
image: busybox:1.36
command: ["sh", "-c", "cat /etc/config/LOG_LEVEL; sleep 3600"]
volumeMounts:
- name: config
mountPath: /etc/config
readOnly: true
volumes:
- name: config
configMap:
name: app-configReplace configMap with secret and name with secretName to mount a Secret.
Environment variables versus files
| Behavior | Environment variable | Mounted volume |
|---|---|---|
| Updated when the ConfigMap or Secret changes | No, fixed at container start | Yes, after a short delay |
| Suited to | Simple settings | Config files, certificates |
Mounts using subPath do not receive updates either.
Common errors
CreateContainerConfigErrormeans the Pod references a ConfigMap, Secret, or key that does not exist. Runkubectl describe podand read the events.- The ConfigMap or Secret must be in the same namespace as the Pod.
- To make a Pod ignore a missing object, set
optional: trueon the reference. - Environment variables never refresh. Restart the Pod to pick up new values.
Exercise
You update a ConfigMap that a running Pod uses through both envFrom and a mounted volume. Which one sees the new value without restarting the Pod, and why?
Show answer
The mounted volume. Kubernetes refreshes the files after a short delay. Environment variables are set once when the container starts, so the Pod must be restarted to see the change.
Exercise
A new Pod is stuck in CreateContainerConfigError. Which command shows the cause, and what is the most likely reason?
Show answer
kubectl describe pod <name> shows the events. The most likely reason is that a referenced ConfigMap, Secret, or key is missing or sits in a different namespace.