Skip to content

Use ConfigMaps

Problem

Applications often require configuration such as:

  • API endpoints
  • feature flags
  • application settings

Hardcoding these values inside container images makes them difficult to change.

Kubernetes ConfigMaps allow configuration to be injected into pods.


Step 1 — Create a ConfigMap

Create configmap.yaml.

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  APP_ENV: production
  API_URL: https://api.example.com

Apply it.

kubectl apply -f configmap.yaml

Verify.

kubectl get configmap

Expected output:

NAME         DATA
app-config   2


Step 2 — Use ConfigMap in Deployment

Update the deployment.

deployment.yaml

envFrom:
  - configMapRef:
      name: app-config

Full container example:

containers:
  - name: app
    image: nginx
    envFrom:
      - configMapRef:
          name: app-config

Apply.

kubectl apply -f deployment.yaml


Step 3 — Verify Environment Variables

Enter the pod.

kubectl exec -it <pod-name> -- /bin/sh

Check variables.

env

Expected:

APP_ENV=production
API_URL=https://api.example.com


Troubleshooting

ConfigMap Not Found

Check:

kubectl get configmap


Pod Not Restarting

ConfigMap updates do not restart pods automatically.

Restart deployment.

kubectl rollout restart deployment <deployment-name>