Managing ConfigMaps
1. Creating ConfigMap from Literal
kubectl create cm app-config \
--from-literal=LOG_LEVEL=info \
--from-literal=API_URL=https://api.example.com
| Flag | Effect |
|---|---|
--from-literal=K=V | Single key |
--dry-run=client -o yaml | Generate manifest only |
2. Creating ConfigMap from File
kubectl create cm nginx-conf --from-file=nginx.conf
kubectl create cm app-config --from-file=key=path/to/file
kubectl create cm certs --from-file=./certs/ # all files in dir
| Form | Key Name |
|---|---|
--from-file=path | File basename |
--from-file=key=path | Explicit key |
--from-file=dir/ | One key per file |
3. Creating ConfigMap from Env File
kubectl create cm app-env --from-env-file=.env
| Format | Detail |
|---|---|
| KEY=value | One per line |
| Comments | # at line start |
4. Creating ConfigMap from YAML
apiVersion: v1
kind: ConfigMap
metadata: { name: app-config }
data:
LOG_LEVEL: info
app.properties: |
server.port=8080
spring.profiles.active=prod
binaryData:
truststore.jks: BASE64...
| Field | Type |
|---|---|
| data | UTF-8 strings |
| binaryData | Base64-encoded bytes |
5. Listing ConfigMaps
kubectl get cm
kubectl get cm -A -o wide
| Column | Meaning |
|---|---|
| DATA | Key count |
| AGE | Creation time |
6. Describing ConfigMap Details
kubectl describe cm app-config
kubectl get cm app-config -o yaml
| Section | Info |
|---|---|
| Data | Key/value pairs (text) |
| BinaryData | Base64 values |
| Events | Rarely populated |
7. Using ConfigMap as Environment Variables
env:
- name: LOG_LEVEL
valueFrom: { configMapKeyRef: { name: app-config, key: LOG_LEVEL } }
envFrom:
- configMapRef: { name: app-config }
- configMapRef: { name: feature-flags, optional: true }
prefix: FF_
| Field | Use |
|---|---|
| configMapKeyRef | Specific key |
| envFrom | All keys; prefix optional |
8. Mounting ConfigMap as Volume
volumes:
- name: cfg
configMap:
name: nginx-conf
defaultMode: 0644
items:
- { key: nginx.conf, path: nginx.conf }
containers:
- volumeMounts:
- { name: cfg, mountPath: /etc/nginx/nginx.conf, subPath: nginx.conf }
| Field | Use |
|---|---|
| items | Project specific keys |
| subPath | Mount as single file (no auto-update) |
| defaultMode | File permissions (octal) |
9. Using Specific ConfigMap Keys
configMap:
name: certs
items:
- { key: tls.crt, path: server.crt, mode: 0400 }
| Property | Detail |
|---|---|
| key | Source key in CM |
| path | Target file name |
| mode | Per-file permissions |
10. Updating ConfigMaps
| Method | Effect |
|---|---|
| kubectl edit cm | Interactive edit |
| kubectl apply -f | Declarative |
| kubectl create cm ... --dry-run=client -o yaml | apply -f - | Regenerate |
Note: Mounted file CMs update within ~minute via kubelet sync. env-injected values are NOT updated — pod restart required. Use checksum annotation to roll Deployments on change.
11. Using Immutable ConfigMaps
apiVersion: v1
kind: ConfigMap
metadata: { name: app-config }
immutable: true
data: { LOG_LEVEL: info }
| Benefit | Detail |
|---|---|
| Performance | Apiserver skips watches, less etcd load |
| Safety | Prevent accidental edits |
| Update | Create new CM & switch pod ref |
12. Deleting ConfigMaps
kubectl delete cm app-config
kubectl delete cm -l app=web
Warning: Pods referencing missing CMs fail to start unless
optional: true is set.