Sub topic 9
ConfigMaps and Secrets
This chapter assumes you can read a Pod spec and a Deployment, and that you know how labels connect resources. ConfigMaps and Secrets keep configuration out of container images, so one image can run in several environments.
ConfigMaps
A ConfigMap is a namespaced object that stores non-confidential configuration as key-value pairs. It has a data field instead of spec. A value can be a short setting or the full contents of a file.
apiVersion: v1
kind: ConfigMap
metadata:
name: web-config
namespace: demo
data:
LOG_LEVEL: info
greeting.txt: |
Hello from a ConfigMapThe | is a YAML block scalar. It keeps line breaks, so greeting.txt holds a multi-line file.
Secrets
A Secret has the same shape, but it is meant for passwords, tokens, and keys. Kubernetes treats Secrets more carefully than ConfigMaps. You can restrict access with RBAC, and Secrets are not shown in full by default in some tools.
apiVersion: v1
kind: Secret
metadata:
name: web-secret
namespace: demo
type: Opaque
stringData:
API_TOKEN: replace-meUse stringData to write plain text. The API server encodes it into data as base64 when it stores the object. If you write data yourself, you must supply base64 values.
Consuming them in a Pod
A container can read this data in two ways: as environment variables, or as files in a mounted volume. The Pod below uses both.
apiVersion: v1
kind: Pod
metadata:
name: web-demo
namespace: demo
spec:
containers:
- name: web
image: nginx:1.27
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: web-config
key: LOG_LEVEL
- name: API_TOKEN
valueFrom:
secretKeyRef:
name: web-secret
key: API_TOKEN
volumeMounts:
- name: config
mountPath: /etc/web
readOnly: true
volumes:
- name: config
configMap:
name: web-configEach key in the ConfigMap becomes a file under /etc/web, so you get /etc/web/greeting.txt and /etc/web/LOG_LEVEL. To load every key as an environment variable, use envFrom with a configMapRef or secretRef.
Common errors
- Missing reference. If the Pod names a ConfigMap or Secret that does not exist, it stays in
CreateContainerConfigError. Apply the ConfigMap or Secret first, or put them in the same file ahead of the Pod. - Wrong namespace. A Pod can only read ConfigMaps and Secrets in its own namespace.
- Stale values. Environment variables are read when the container starts. Editing a ConfigMap does not change them. Restart the Pods, for example with
kubectl rollout restart deployment web -n demo. - Mounted files and
subPath. Files from a mounted volume are updated eventually after an edit. A mount that usessubPathis never updated. - Size limit. A single ConfigMap or Secret can hold at most 1 MiB.
Exercise
You edit the value of LOG_LEVEL in web-config and run kubectl apply. The running Pods still log at the old level. Why, and what is the fix?
Show answer
The Pods read LOG_LEVEL through an environment variable, and environment variables are fixed when the container starts. Restart the Pods (for example with kubectl rollout restart deployment web -n demo), or mount the ConfigMap as a volume and have the app re-read the file.