Intermediate Part 2: ConfigMaps – External Configuration¶
Most real apps need settings that change depending on environment: - Development: database = localhost, log level = debug - Production: database = prod-db.company.com, log level = info - Staging: different API key or feature flags
Hard-coding these inside the container image is bad : you’d have to rebuild/redeploy for every change.
ConfigMaps let you inject configuration outside the image.
Think of it as the pizza chain’s recipe adjustments: - Base pizza is the same (container image) - But some locations add extra cheese or change sauce type (config) without remaking the whole kitchen
Goal for this section¶
Inject two config values into our nginx Pods:
- APP_ENV = development
- LOG_LEVEL = debug
We’ll see them as environment variables inside the running Pods.
Step 1: Create a ConfigMap¶
-
Run this command (literal values – quick way):
-
Check it exists:
You should see your two keys/values.
Step 2: Update your Deployment to use the ConfigMap¶
- Edit your
nginx-deployment.yaml(from beginner track) and add this underspec.template.spec.containers:
(Full container block might now look like the following:)
containers:
- name: nginx
image: nginx:1.25 ports:
- containerPort: 80
env:
- name: APP_ENV
valueFrom:
configMapKeyRef:
name: app-settings
key: APP_ENV
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-settings
key: LOG_LEVEL
- Apply the change: Kubernetes will roll out new Pods with the config injected.
Step 3: Verify the config is inside the Pods¶
-
Find one Pod name:
-
Then run:
-
You should see the following output:
Success! The values came from the ConfigMap, not the image.
Bonus: ConfigMap as Volume Mount – Inject Files into the Container¶
- Environment variables are great for single values, but many apps need configuration files (e.g. nginx.conf, app.yaml, .properties files).
- You can mount a ConfigMap as a volume : Kubernetes creates files inside the container with the exact key/value content from the ConfigMap.
Step A: Create a ConfigMap with file-like content¶
-
We'll make a simple custom nginx.conf snippet. Run the following in your terminal:
-
Check it to confirm:
Step B: Update your Deployment to mount the ConfigMap as a volume¶
-
Edit nginx-deployment.yaml and add these two sections under spec.template.spec:
volumes: - name: config-volume configMap: name: nginx-conf # The ConfigMap we created containers: - name: nginx image: nginx:1.25 ports: - containerPort: 80 volumeMounts: - name: config-volume mountPath: /etc/nginx/conf.d/default.conf # nginx reads config from here subPath: nginx.conf # only mount this one key -
Apply the change:
Kubernetes rolls out new Pods with the custom config mounted.
Step C: Verify the file is mounted¶
-
Get one new Pod name:
-
Then run inside the Pod:
-
You should see your custom config:
Step D: Test it in the browser¶
-
Make sure your Service is still running:
-
Refresh the page → you should now see:
→ Proof: the Pod is using the config file we injected via ConfigMap volume!
Step E: Cleanup (when done)¶
- Run the following Bash command (The Deployment stays : it just loses the custom config on next rollout.)
What we learned¶
- ConfigMaps hold non-sensitive configuration
- Inject as environment variables (env) or as files (volumes : we’ll cover later)
- Change config → restart rollout → new values applied
- Keeps images generic and reusable across environments