Kubernetes ConfigMaps
Create ConfigMaps from literals, files, or YAML and use them as environment variables or mounted configuration files.
What Is a ConfigMap?
A ConfigMap stores non-sensitive configuration data so applications can read settings at runtime instead of hardcoding them into images.
Create a ConfigMap from Literals
kubectl create configmap app-config --from-literal=APP_ENV=dev --from-literal=LOG_LEVEL=info
Create a ConfigMap from a File
kubectl create configmap nginx-config --from-file=nginx.conf
Create a ConfigMap from YAML
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_ENV: dev
LOG_LEVEL: info
Use as Environment Variables with envFrom
apiVersion: v1
kind: Pod
metadata:
name: env-config-demo
spec:
containers:
- name: app
image: busybox:1.36
command: ['sh', '-c', 'env | grep APP_ && sleep 3600']
envFrom:
- configMapRef:
name: app-config
Use a Single Key with valueFrom
apiVersion: v1
kind: Pod
metadata:
name: single-key-demo
spec:
containers:
- name: app
image: busybox:1.36
command: ['sh', '-c', 'echo $LOG_LEVEL && sleep 3600']
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-config
key: LOG_LEVEL
Use as Mounted Files
apiVersion: v1
kind: Pod
metadata:
name: file-config-demo
spec:
volumes:
- name: config-volume
configMap:
name: app-config
containers:
- name: app
image: busybox:1.36
command: ['sh', '-c', 'ls /config && sleep 3600']
volumeMounts:
- name: config-volume
mountPath: /config
Inspect ConfigMaps
kubectl get cm
kubectl describe cm app-config
Why Two Consumption Patterns Exist
Environment variables are simple for app settings. Mounted files are better when the app expects actual config files.
ConfigMap Use
What should ConfigMaps usually store?
File Mount Pattern
Why might you mount a ConfigMap as files instead of using environment variables?