Skip to content

ConfigMaps & Secrets

Build once, configure per environment. The same demo-app:1.0 image runs in dev, staging and prod. Only config changes.

lab/k8s/10-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata: { name: demo-config, namespace: demo }
data:
APP_MESSAGE: "Hello from Kubernetes!"
APP_COLOR: "#4f8cff"

Use it as env vars (all keys at once, or one at a time):

envFrom:
- configMapRef: { name: demo-config }
env:
- name: COLOR
valueFrom: { configMapKeyRef: { name: demo-config, key: APP_COLOR } }

…or mount it as files (good for whole config files like nginx.conf):

volumes: [{ name: cfg, configMap: { name: demo-config } }]
volumeMounts: [{ name: cfg, mountPath: /etc/demo }]
Terminal window
kubectl create secret generic db -n demo \
--from-literal=DB_PASSWORD='s3cr3t'
envFrom: [{ secretRef: { name: db } }]