Load Config Files into Pods with ConfigMap
ConfigMaps decouple configuration from images so you can change settings without rebuilding.
What
ConfigMaps let you decouple configuration from container images. You can create them from files, directories, or literal key-value pairs, then mount them as volumes inside pods or expose them as environment variables. This means the same container image works across dev, staging, and production with different configs.
Why It Matters
Hardcoding configuration into container images means you need to rebuild and redeploy every time a setting changes. ConfigMaps separate config from code, letting you update database URLs, feature flags, or application settings without touching your image. This is a core principle of twelve-factor app design.
Example
# Create a ConfigMap from a file
kubectl create configmap app-config --from-file=config.yaml
# Create from literal values
kubectl create configmap app-settings \
--from-literal=DB_HOST=postgres.default.svc \
--from-literal=LOG_LEVEL=info
# Mount ConfigMap as a volume in a Pod
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: app
image: myapp:latest
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: app-config
# View ConfigMap contents
kubectl get configmap app-config -o yamlCommon Mistake
Updating a ConfigMap and expecting running pods to pick up the changes automatically. Pods that mount ConfigMaps as volumes will eventually see updates (with a delay), but pods using environment variables from ConfigMaps will NOT update until they are restarted.
Quick Fix
After updating a ConfigMap, restart your deployment with `kubectl rollout restart deployment/<name>`. For automatic reloads, use a tool like Reloader (stakater/Reloader) that watches ConfigMaps and triggers rolling restarts when they change.
Key Takeaways
- 1ConfigMap = external config for your pods
- 2Create from files, directories, or literal values
- 3Mount as volume or expose as env vars
- 4Env var ConfigMaps need pod restart to update
- 5Use Reloader for automatic config refresh
Was this tip helpful?
Help us improve the DevOpsPath daily collection