ConfigMap and env
Hub › Kubernetes › 02
Goal
Externalise configuration from the container image into a Kubernetes ConfigMap and inject it as an environment variable into the web Deployment.
Why
Hard-coding config in an image means a rebuild every time a value changes. A ConfigMap lets you update values without touching the image — you just re-apply the manifest.
Step 1 — Create the ConfigMap
Create configmap.yaml:
apiVersion: v1
kind: ConfigMap
metadata:
name: web-config
data:
GREETING: "hello from configmap"Apply it:
kubectl apply -f configmap.yamlVerify it exists:
kubectl get configmap web-configExpected output:
NAME DATA AGE
web-config 1 5sStep 2 — Reference the ConfigMap from the Deployment
Open deployment.yaml and add an env block to the container spec so it reads GREETING from web-config:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80
env:
- name: GREETING
valueFrom:
configMapKeyRef:
name: web-config
key: GREETINGApply the updated Deployment:
kubectl apply -f deployment.yamlStep 3 — Verify the env var is injected
Once the pod restarts, exec into it and print the variable:
kubectl exec deploy/web -- printenv GREETINGExpected output:
hello from configmapCheckpoint
You now have a running web Deployment with GREETING sourced from a ConfigMap. No image rebuild was required — changing the value in configmap.yaml and re-applying is all that's needed.
Next: 03 Probes