CodeBlanks Logo Icon CodeBlanks

Challenge - ConfigMap

Write a ConfigMap and mount it as env in a Pod.

Challenge: externalize the config

Instead of baking an nginx site's config file into the image, you'll move it into a ConfigMap. The file will be mounted as a volume; when the config changes, the file will update without restarting the pods, and nginx will be told to reload.

kubectl create configmap nginx-ayar --from-file=default.conf
# ... volumes: configMap: nginx-ayar  ->  /etc/nginx/conf.d
kubectl create configmap nginx-ayar --from-file=default.conf --dry-run=client -o yaml | kubectl apply -f -
kubectl exec deploy/web -- nginx -s reload

Practice A deployment template with a volume

Write a Deployment template's spec section (starting with spec:): an ayar volume from the nginx-ayar ConfigMap under volumes; container named nginx, image nginx:1.27; mount ayar at /etc/nginx/conf.d with readOnly: true.

Starter code

terminal
# buraya yaz

Sample solution

terminal
spec:
  volumes:
    - name: ayar
      configMap:
        name: nginx-ayar
  containers:
    - name: nginx
      image: nginx:1.27
      volumeMounts:
        - name: ayar
          mountPath: /etc/nginx/conf.d
          readOnly: true

Expected output

deployment.apps/web configured

Practice Update the config

Write three commands: generate the nginx-ayar ConfigMap from the default.conf file as YAML without creating it and pipe it into kubectl apply -f -; check the file by running cat /etc/nginx/conf.d/default.conf in deploy/web; run nginx -s reload in deploy/web.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl create configmap nginx-ayar --from-file=default.conf --dry-run=client -o yaml | kubectl apply -f -
kubectl exec deploy/web -- cat /etc/nginx/conf.d/default.conf
kubectl exec deploy/web -- nginx -s reload

Expected output

configmap/nginx-ayar configured
server {
    listen 80;
    location / { root /usr/share/nginx/html; }
}
2026/11/03 13:05:21 [notice] 42#42: signal process started

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 In this setup, what would happen if the config file were mounted with subPath?

  • There'd be no difference
  • The ConfigMap update wouldn't reach the file Correct answer
  • The file would never appear
  • nginx would crash

Explanation: subPath mounts aren't refreshed automatically.

2 Make nginx reload its config.

Question
kubectl exec deploy/web -- nginx -s ____
Answer
kubectl exec deploy/web -- nginx -s reload

Explanation: nginx -s reload rereads the config without stopping the process.

3 Match.

Term Meaning
--from-file=default.conf ConfigMap from a file
configMap volume Mount as a file
| kubectl apply -f - Update
nginx -s reload Reload

All lessons in Intermediate