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
FlagEffect
--from-literal=K=VSingle key
--dry-run=client -o yamlGenerate 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
FormKey Name
--from-file=pathFile basename
--from-file=key=pathExplicit key
--from-file=dir/One key per file

3. Creating ConfigMap from Env File

kubectl create cm app-env --from-env-file=.env
FormatDetail
KEY=valueOne 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...
FieldType
dataUTF-8 strings
binaryDataBase64-encoded bytes

5. Listing ConfigMaps

kubectl get cm
kubectl get cm -A -o wide
ColumnMeaning
DATAKey count
AGECreation time

6. Describing ConfigMap Details

kubectl describe cm app-config
kubectl get cm app-config -o yaml
SectionInfo
DataKey/value pairs (text)
BinaryDataBase64 values
EventsRarely 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_
FieldUse
configMapKeyRefSpecific key
envFromAll 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 }
FieldUse
itemsProject specific keys
subPathMount as single file (no auto-update)
defaultModeFile permissions (octal)

9. Using Specific ConfigMap Keys

configMap:
  name: certs
  items:
  - { key: tls.crt, path: server.crt, mode: 0400 }
PropertyDetail
keySource key in CM
pathTarget file name
modePer-file permissions

10. Updating ConfigMaps

MethodEffect
kubectl edit cmInteractive edit
kubectl apply -fDeclarative
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 }
BenefitDetail
PerformanceApiserver skips watches, less etcd load
SafetyPrevent accidental edits
UpdateCreate 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.