How to Create ConfigMap from Literal in Kubernetes
Use the
kubectl create configmap command with the --from-literal flag to create a ConfigMap from literal key-value pairs. For example, kubectl create configmap my-config --from-literal=key1=value1 --from-literal=key2=value2 creates a ConfigMap named my-config with two entries.Syntax
The basic syntax to create a ConfigMap from literal values is:
kubectl create configmap <configmap-name> --from-literal=<key1>=<value1> --from-literal=<key2>=<value2>- <configmap-name>: The name you want to give your ConfigMap.
- --from-literal: Flag to specify a key-value pair directly.
- You can add multiple
--from-literalflags to include multiple entries.
bash
kubectl create configmap <configmap-name> --from-literal=<key1>=<value1> --from-literal=<key2>=<value2>
Example
This example creates a ConfigMap named app-config with two literal key-value pairs: environment=production and log_level=info.
bash
kubectl create configmap app-config --from-literal=environment=production --from-literal=log_level=info
Output
configmap/app-config created
Common Pitfalls
Common mistakes when creating ConfigMaps from literals include:
- Forgetting to use the
--from-literalflag before each key-value pair. - Using spaces around the equals sign
=, which is not allowed. - Not quoting values that contain spaces or special characters, which can cause errors.
- Trying to create a ConfigMap with a name that already exists without using
kubectl applyor deleting the old one first.
bash
kubectl create configmap my-config --from-literal=key1= value1 # Wrong: space after '=' kubectl create configmap my-config --from-literal=key1=value1 --from-literal key2=value2 # Wrong: missing '=' after --from-literal kubectl create configmap my-config --from-literal="key with space"="value with space" # Correct: quotes for spaces
Quick Reference
| Command | Description |
|---|---|
| kubectl create configmap | Create ConfigMap with one literal key-value pair |
| kubectl create configmap | Create ConfigMap with multiple literals |
| kubectl get configmap | View ConfigMap details in YAML format |
| kubectl delete configmap | Delete a ConfigMap |
Key Takeaways
Use the --from-literal flag with kubectl create configmap to add key-value pairs directly.
Each literal key-value pair requires its own --from-literal flag without spaces around '='.
Quote values with spaces or special characters to avoid errors.
Check existing ConfigMap names to avoid conflicts before creating new ones.
Use kubectl get configmap to verify your ConfigMap after creation.