📖 Scenario: You are managing a Kubernetes application that needs configuration files to be available inside its containers. Instead of baking these configs into the container image, you want to use Kubernetes ConfigMaps to provide these files dynamically.This approach helps you update configuration without rebuilding images, similar to how you might keep your app settings in a separate folder on your computer that your app reads from.
🎯 Goal: Learn how to create a ConfigMap with configuration data, mount it as a volume inside a Pod, and verify the configuration files are accessible inside the container.
📋 What You'll Learn
Create a ConfigMap named app-config with two configuration files: app.properties and db.properties with exact content.
Create a Pod named configmap-pod that mounts the app-config ConfigMap as a volume at /etc/config.
Verify the mounted files inside the Pod by printing their contents.
💡 Why This Matters
🌍 Real World
Using ConfigMaps as mounted volumes is a common way to provide configuration files to applications running in Kubernetes without rebuilding container images.
💼 Career
Understanding ConfigMaps and volume mounts is essential for Kubernetes administrators and DevOps engineers to manage application configurations efficiently.
Progress0 / 4 steps
1
Create a ConfigMap with configuration files
Create a ConfigMap named app-config with two files: app.properties containing app.name=MyApp and app.version=1.0, and db.properties containing db.host=localhost and db.port=5432. Use the kubectl create configmap app-config --from-literal=app.properties='app.name=MyApp\napp.version=1.0' --from-literal=db.properties='db.host=localhost\ndb.port=5432' command.
Kubernetes
Hint
Use kubectl create configmap with --from-literal twice to add two files.
2
Create a Pod that mounts the ConfigMap as a volume
Create a Pod manifest named configmap-pod.yaml with a Pod named configmap-pod. The Pod should run the busybox image and mount the app-config ConfigMap as a volume named config-volume at the path /etc/config. Use the command: ['sleep', '3600'] to keep the Pod running.
Kubernetes
Hint
Define a Pod with a container that mounts the ConfigMap as a volume at /etc/config.
3
Apply the Pod manifest and verify the Pod is running
Apply the Pod manifest file configmap-pod.yaml using kubectl apply -f configmap-pod.yaml. Then check the Pod status with kubectl get pod configmap-pod to confirm it is running.
Kubernetes
Hint
Use kubectl apply -f to create the Pod and kubectl get pod to check its status.
4
Verify the ConfigMap files inside the Pod
Use kubectl exec configmap-pod -- cat /etc/config/app.properties and kubectl exec configmap-pod -- cat /etc/config/db.properties to print the contents of the mounted ConfigMap files inside the Pod.
Kubernetes
Hint
Use kubectl exec with cat to read the files inside the Pod.
Practice
(1/5)
1. What is the main purpose of using a ConfigMap as a mounted volume in Kubernetes?
easy
A. To provide configuration data as files inside a container without rebuilding the image
B. To store container logs persistently
C. To increase container CPU limits dynamically
D. To manage container networking settings
Solution
Step 1: Understand ConfigMap role
ConfigMaps hold configuration data separately from container images.
Step 2: Understand mounting as volume
Mounting a ConfigMap as a volume makes its data available as files inside the container.
Final Answer:
To provide configuration data as files inside a container without rebuilding the image -> Option A
Hint: ConfigMaps as volumes = config files inside container [OK]
Common Mistakes:
Confusing ConfigMaps with persistent storage for logs
Thinking ConfigMaps control CPU or networking
Assuming ConfigMaps require container rebuild
2. Which of the following is the correct YAML snippet to mount a ConfigMap named app-config as a volume in a pod?
easy
A. volumes:\n - name: config-volume\n emptyDir: {}
B. volumes:\n - name: config-volume\n secret:\n name: app-config
C. volumes:\n - name: config-volume\n persistentVolumeClaim:\n claimName: app-config
D. volumes:\n - name: config-volume\n configMap:\n name: app-config
Solution
Step 1: Identify volume type for ConfigMap
ConfigMaps are mounted using the configMap field inside volumes.
Step 2: Match correct YAML structure
volumes:\n - name: config-volume\n configMap:\n name: app-config correctly uses configMap: with the ConfigMap name.
Final Answer:
volumes:\n - name: config-volume\n configMap:\n name: app-config -> Option D
Quick Check:
ConfigMap volume uses configMap: name [OK]
Hint: ConfigMap volume uses 'configMap:' with name [OK]
Common Mistakes:
Using secret instead of configMap for ConfigMap volumes
Confusing persistentVolumeClaim with ConfigMap
Using emptyDir which is an empty temp volume
3. Given this pod volume mount snippet, what file content will appear inside the container at /etc/config/app.properties if the ConfigMap app-config has a key app.properties with value mode=production?
A. A directory named app.properties with no content
B. No file created because mountPath is a directory
C. A file named app.properties with content 'mode=production'
D. A file named app.properties with content 'mode=development'
Solution
Step 1: Understand ConfigMap key to file mapping
Each key in ConfigMap becomes a file with that name and its value as content.
Step 2: Check mountPath and file presence
Mounting at /etc/config creates files inside that directory, including app.properties with content mode=production.
Final Answer:
A file named app.properties with content 'mode=production' -> Option C
Quick Check:
ConfigMap key = filename, value = file content [OK]
Hint: ConfigMap keys become files with their values inside mountPath [OK]
Common Mistakes:
Thinking mountPath must be a file, not directory
Assuming file content changes without ConfigMap update
Confusing file name with directory
4. You mounted a ConfigMap as a volume, but inside the container the files are missing. Which is the most likely cause?
medium
A. The ConfigMap name in the volume spec does not match any existing ConfigMap
B. The container image does not support volumes
C. The pod has no CPU resources assigned
D. The ConfigMap keys are empty strings
Solution
Step 1: Check ConfigMap reference correctness
If the ConfigMap name in the volume spec is wrong, Kubernetes cannot mount it, so files won't appear.
Step 2: Eliminate unrelated causes
Container images support volumes by default; CPU resources don't affect file mounting; keys cannot be empty strings in ConfigMaps.
Final Answer:
The ConfigMap name in the volume spec does not match any existing ConfigMap -> Option A
Quick Check:
Wrong ConfigMap name = no files mounted [OK]
Hint: Check ConfigMap name spelling in volume spec [OK]
Common Mistakes:
Blaming container image for missing files
Ignoring ConfigMap existence or name mismatch
Assuming resource limits affect volume mounts
5. You want to mount only specific keys from a ConfigMap named app-config as files inside a container at /etc/config. Which volume configuration snippet achieves this?