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.
3. Interactive: Config Injection Visualizer
See how data flows from ConfigMap to Pod.
ConfigMap
DB_HOST=db.prod
DEBUG=false
→
Pod (Env Vars)
DB_HOST=???
DEBUG=???
4. Code Example: Using ConfigMaps
ConfigMap
Pod (Env)
Pod (Volume)
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