Configuration, Secrets and storage
Keep settings out of the image with ConfigMaps and Secrets, and keep data with persistent volumes.
A good image does not contain settings. Kubernetes lets you inject them at run time so the same image works in every environment.
ConfigMap
A ConfigMap holds non-sensitive settings as key and value pairs. A pod can read them as environment variables or as files.
kubectl create configmap app-config --from-literal=LOG_LEVEL=infoSecret
A Secret holds sensitive values such as passwords and API keys. It is used in the same way as a ConfigMap.
CarefulSecret values are only Base64-encoded by default, not encrypted. Anyone who can read the Secret can decode it. Limit access with RBAC and enable encryption at rest in the cluster.
Using them in a pod
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-config
key: LOG_LEVEL
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db
key: passwordPersistent storage
The disk of a container disappears with the pod. For data that must survive, a pod asks for a PersistentVolumeClaim (PVC). The cluster provides a PersistentVolume that matches the request. Databases usually run as StatefulSets so each replica keeps its own volume and a stable name.
Test yourself
Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.