ConfigMaps
Welcome to the module on ConfigMaps. Here you will learn key concepts and best practices to master this topic.
1. The Problem
Hardcoding configuration (DB URLs, API Keys) in container images is bad practice.
- Rebuilds: You have to rebuild the image to change config.
- Security: Secrets might leak.
- Environment: Dev/Staging/Prod need different configs.
2. The Solution: ConfigMap
A key-value store for non-sensitive data. You can inject data into Pods as:
- Environment Variables: Good for simple flags.
- Files (Volume Mount): Good for
nginx.conforapplication.properties.
The Analogy: The Worker’s Instruction Manual
Think of a Kubernetes Pod as a newly hired worker reporting for their first day on a construction site. The container image is the worker’s innate skills. However, the worker doesn’t know which house to build or where the materials are located. A ConfigMap is like an instruction manual handed to the worker the exact moment they step onto the site. This manual allows the exact same worker to perform their job differently depending on the site they are assigned to.
War Story: The Frozen Config
In a large-scale deployment at a major e-commerce company, engineers updated a ConfigMap containing a feature toggle to disable a failing external payment gateway. However, because the configuration was injected as environment variables upon startup, the running pods did not detect the change. The application continued to route traffic to the failing gateway, resulting in hours of downtime. The team had to manually run a rolling restart (
kubectl rollout restart deployment) to force the pods to recreate and pull the updated environment variables. This emphasizes a crucial hardware reality: Environment variables injected from ConfigMaps are static. Theexecvecall has already occurred.
3. Interactive: Config Injection Visualizer
See how data flows from ConfigMap to Pod.
ConfigMap
Pod (Env Vars)
4. Code Example: Using ConfigMaps
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
# Key-Value pairs
DB_HOST: "postgres-svc"
LOG_LEVEL: "info"
# File content
nginx.conf: |
server {
listen 80;
}
apiVersion: v1
kind: Pod
metadata:
name: env-demo
spec:
containers:
- name: app
image: my-app
envFrom:
- configMapRef:
name: app-config
apiVersion: v1
kind: Pod
metadata:
name: volume-demo
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: config-volume
mountPath: /etc/nginx/conf.d
volumes:
- name: config-volume
configMap:
name: app-config