0
0
Kubernetesdevops~5 mins

Creating ConfigMaps from files in Kubernetes - Step-by-Step CLI Walkthrough

Choose your learning style9 modes available
Introduction
Sometimes your application needs configuration data like settings or keys. Kubernetes ConfigMaps let you store this data separately from your app code. Creating ConfigMaps from files helps you keep configuration organized and easy to update.
When you want to provide environment-specific settings to your app without changing the app itself
When you have multiple configuration files that your app needs to read
When you want to update configuration without rebuilding your container image
When you want to share configuration data across multiple pods
When you want to keep sensitive data separate from your app code (non-secret data)
Commands
This command creates a ConfigMap named 'my-config' using the contents of the file 'app.properties'. Kubernetes stores the file content as key-value pairs inside the ConfigMap.
Terminal
kubectl create configmap my-config --from-file=app.properties
Expected OutputExpected
configmap/my-config created
--from-file - Specifies the file to use as the source for the ConfigMap data
This command shows the details of the ConfigMap 'my-config' in YAML format so you can verify the data was stored correctly.
Terminal
kubectl get configmaps my-config -o yaml
Expected OutputExpected
apiVersion: v1 kind: ConfigMap metadata: name: my-config data: app.properties: | # content of app.properties file here key1=value1 key2=value2
-o yaml - Outputs the ConfigMap details in YAML format
This command creates a ConfigMap named 'multi-config' from two files, 'config1.txt' and 'config2.txt'. Each file's content is stored under its filename as a key.
Terminal
kubectl create configmap multi-config --from-file=config1.txt --from-file=config2.txt
Expected OutputExpected
configmap/multi-config created
--from-file - Can be used multiple times to add multiple files to the ConfigMap
This command shows a summary of the 'multi-config' ConfigMap, including the keys (filenames) it contains and their sizes.
Terminal
kubectl describe configmap multi-config
Expected OutputExpected
Name: multi-config Namespace: default Labels: <none> Annotations: <none> Data ==== config1.txt: 25 bytes config2.txt: 30 bytes Events: <none>
Key Concept

If you remember nothing else from this pattern, remember: ConfigMaps let you store configuration data from files so your app can use it without changing the app image.

Common Mistakes
Using a directory path instead of a file path with --from-file
The command expects a file, not a directory, so it will fail or create an empty ConfigMap
Specify the exact file path for each --from-file flag
Not verifying the ConfigMap content after creation
You might think the ConfigMap has the right data but it could be empty or incorrect
Always run 'kubectl get configmaps NAME -o yaml' or 'kubectl describe configmap NAME' to check
Using the same filename multiple times with --from-file
Keys in ConfigMaps must be unique; duplicate filenames will overwrite previous data silently
Ensure each file has a unique name or use --from-file=key=filepath to set custom keys
Summary
Use 'kubectl create configmap NAME --from-file=filename' to create a ConfigMap from a file.
Verify the ConfigMap content with 'kubectl get configmaps NAME -o yaml' or 'kubectl describe configmap NAME'.
You can add multiple files by repeating the --from-file flag for each file.