How to Create ConfigMap in Kubernetes: Simple Guide
To create a
ConfigMap in Kubernetes, use the kubectl create configmap command followed by the name and data source. You can create it from literal values, files, or directories to store configuration data separately from your containers.Syntax
The basic syntax to create a ConfigMap is:
kubectl create configmap <name> --from-literal=<key>=<value>creates a ConfigMap from literal key-value pairs.kubectl create configmap <name> --from-file=<path>creates a ConfigMap from a file or directory.
The <name> is the ConfigMap's name. The --from-literal option adds direct key-value pairs. The --from-file option loads data from files.
bash
kubectl create configmap my-config --from-literal=key1=value1 kubectl create configmap my-config --from-file=path/to/config-file
Example
This example creates a ConfigMap named app-config with two literal key-value pairs and one file:
bash
kubectl create configmap app-config --from-literal=APP_MODE=production --from-literal=LOG_LEVEL=info --from-file=config.json
Output
configmap/app-config created
Common Pitfalls
Common mistakes when creating ConfigMaps include:
- Using the
latesttag in container images instead of fixed versions (not related to ConfigMap but common in deployments). - Forgetting to specify the correct file path with
--from-file. - Overwriting existing ConfigMaps without realizing it.
- Not applying the ConfigMap to pods or deployments after creation.
Always check if the ConfigMap exists before creating or updating it.
bash
kubectl create configmap app-config --from-file=wrong-path/config.json # Error: file not found kubectl create configmap app-config --from-file=correct-path/config.json # Success: configmap/app-config created
Quick Reference
Summary tips for creating ConfigMaps:
- Use
kubectl create configmap <name>with--from-literalor--from-file. - Check existing ConfigMaps with
kubectl get configmaps. - Update ConfigMaps with
kubectl apply -f configmap.yamlfor complex data. - Mount ConfigMaps as files or environment variables in pods.
Key Takeaways
Use kubectl create configmap with --from-literal or --from-file to create ConfigMaps.
ConfigMaps store configuration separately from container images for flexibility.
Always verify file paths and ConfigMap names to avoid errors.
Apply ConfigMaps to pods to make configuration available inside containers.
Use kubectl get configmaps to list and check existing ConfigMaps.