📖 Scenario: You are managing a Kubernetes cluster for a small web application. You want to store configuration data in ConfigMaps by loading it from files on your local machine. This helps keep your app settings organized and separate from your container images.
🎯 Goal: Learn how to create a Kubernetes ConfigMap from files using the kubectl create configmap command with the --from-file option. You will create a ConfigMap named app-config from two configuration files.
📋 What You'll Learn
Create two configuration files named app.properties and db.properties with specific content.
Use kubectl create configmap with --from-file to create a ConfigMap named app-config from these files.
Verify the ConfigMap was created and contains the correct data.
💡 Why This Matters
🌍 Real World
Storing configuration data separately from application code is a common practice in Kubernetes. ConfigMaps allow you to manage settings easily and update them without rebuilding container images.
💼 Career
Knowing how to create and manage ConfigMaps from files is essential for Kubernetes administrators and DevOps engineers to configure applications dynamically and maintain clean deployment workflows.
Progress0 / 4 steps
1
Create configuration files
Create two files named app.properties and db.properties with the exact content below:
Use the echo command with redirection > to create files with content.
2
Create ConfigMap from files
Use the command kubectl create configmap app-config --from-file=app.properties --from-file=db.properties to create a ConfigMap named app-config from the two files.
Kubernetes
Hint
Use kubectl create configmap with --from-file option for each file.
3
Verify the ConfigMap contents
Use the command kubectl get configmap app-config -o yaml to display the ConfigMap details and verify it contains keys app.properties and db.properties.
Kubernetes
Hint
Use kubectl get configmap with -o yaml to see full details.
4
Display ConfigMap keys
Use the command kubectl get configmap app-config -o jsonpath='{.data}' to print only the data keys and their values stored in the ConfigMap.
Kubernetes
Hint
Use -o jsonpath='{.data}' to extract only the data field from the ConfigMap.
Practice
(1/5)
1. What is the main purpose of creating a ConfigMap from a file in Kubernetes?
easy
A. To deploy a new pod automatically
B. To create a new container image
C. To monitor resource usage of pods
D. To store configuration data separately from application code
Solution
Step 1: Understand ConfigMap role
ConfigMaps hold configuration data separately from app code to keep apps flexible.
Step 2: Recognize file usage
Creating ConfigMaps from files allows easy management of config data outside the container image.
Final Answer:
To store configuration data separately from application code -> Option D
Quick Check:
ConfigMap purpose = separate config storage [OK]
Hint: ConfigMaps separate config from code for flexibility [OK]
Common Mistakes:
Confusing ConfigMaps with container images
Thinking ConfigMaps deploy pods
Assuming ConfigMaps monitor resources
2. Which of the following is the correct command to create a ConfigMap named app-config from a file called settings.conf?
easy
A. kubectl create configmap app-config --from-file=settings.conf
B. kubectl create configmap --name=app-config settings.conf
C. kubectl configmap create app-config settings.conf
D. kubectl create configmap app-config -f settings.conf
Solution
Step 1: Recall correct syntax for creating ConfigMap from file
The correct syntax is kubectl create configmap [name] --from-file=[filename].
kubectl create configmap app-config --from-file=settings.conf -> Option A
Quick Check:
Correct syntax uses --from-file=filename [OK]
Hint: Use --from-file=filename after configmap name [OK]
Common Mistakes:
Using -f instead of --from-file
Placing filename without --from-file flag
Wrong command order or flags
3. Given the command kubectl create configmap my-config --from-file=app.properties, what will be the key name inside the ConfigMap if the file is named app.properties?
medium
A. app
B. my-config
C. app.properties
D. configmap
Solution
Step 1: Understand key naming in ConfigMaps from files
When creating a ConfigMap from a file without specifying a key, the filename is used as the key.
Step 2: Apply to given filename
Since the file is named app.properties, the key inside the ConfigMap will be app.properties.
Final Answer:
app.properties -> Option C
Quick Check:
Filename = key name in ConfigMap [OK]
Hint: Filename becomes key if no key specified [OK]
Common Mistakes:
Assuming ConfigMap name is the key
Using partial filename as key
Confusing key with ConfigMap name
4. You run kubectl create configmap test-config --from-file=config.txt but get an error saying the file does not exist. What is the most likely cause?
medium
A. The ConfigMap name is invalid
B. The file config.txt is not in the current directory
C. You need to use --from-env-file instead
D. kubectl command does not support --from-file
Solution
Step 1: Check file existence requirement
The --from-file flag requires the file to exist in the current directory or given path.
Step 2: Identify cause of error
If the file is missing or path is wrong, kubectl will error that the file does not exist.
Final Answer:
The file config.txt is not in the current directory -> Option B
Quick Check:
File must exist where command runs [OK]
Hint: Check file path and existence before creating ConfigMap [OK]
Common Mistakes:
Assuming ConfigMap name causes file error
Using wrong flag instead of --from-file
Ignoring file location or path
5. You have two config files: db.conf and app.conf. You want to create a single ConfigMap named multi-config containing both files as keys. Which command should you use?
hard
A. kubectl create configmap multi-config --from-file=db.conf --from-file=app.conf
B. kubectl create configmap multi-config --from-file=db.conf,app.conf
C. kubectl create configmap multi-config --from-file=./db.conf ./app.conf
D. kubectl create configmap multi-config -f db.conf -f app.conf
Solution
Step 1: Understand multiple file syntax
To add multiple files, use multiple --from-file flags, one per file.