0
0
Kubernetesdevops~5 mins

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

Choose your learning style9 modes available
Introduction
Sometimes you need to store small pieces of configuration data directly in Kubernetes without creating files. Creating ConfigMaps from literals lets you do this by typing the data right in the command line. This helps keep your app settings organized and easy to change.
When you want to quickly add a few configuration values to your app without making a file.
When you need to test different settings by changing values on the fly.
When you want to share simple environment variables with your pods.
When you want to keep configuration separate from your app code but don't want extra files.
When you want to update small config values without redeploying your app.
Commands
This command creates a ConfigMap named 'example-config' with two key-value pairs: APP_MODE set to 'production' and LOG_LEVEL set to 'info'. We use --from-literal to add each pair directly from the command line.
Terminal
kubectl create configmap example-config --from-literal=APP_MODE=production --from-literal=LOG_LEVEL=info
Expected OutputExpected
configmap/example-config created
--from-literal - Adds a key-value pair directly from the command line to the ConfigMap
This command shows the full details of the ConfigMap 'example-config' in YAML format so you can verify the keys and values were set correctly.
Terminal
kubectl get configmap example-config -o yaml
Expected OutputExpected
apiVersion: v1 kind: ConfigMap metadata: name: example-config data: APP_MODE: production LOG_LEVEL: info
-o yaml - Outputs the resource details in YAML format
This command gives a human-readable summary of the ConfigMap, showing the keys and their values clearly.
Terminal
kubectl describe configmap example-config
Expected OutputExpected
Name: example-config Namespace: default Labels: <none> Annotations: <none> Data ==== APP_MODE: production LOG_LEVEL: info Events: <none>
Key Concept

If you remember nothing else from this pattern, remember: --from-literal lets you create ConfigMaps quickly by typing key-value pairs directly in the command line.

Common Mistakes
Using spaces around the equal sign in --from-literal like --from-literal=APP_MODE = production
The command treats spaces as separate arguments, causing errors or wrong key-value parsing.
Write the key and value without spaces: --from-literal=APP_MODE=production
Trying to create a ConfigMap with duplicate keys using multiple --from-literal flags
Kubernetes will only keep one value per key, so duplicates cause confusion or overwrite.
Use unique keys for each --from-literal flag.
Not verifying the ConfigMap after creation
You might think the ConfigMap was created correctly but it could have missing or wrong data.
Always run 'kubectl get configmap NAME -o yaml' or 'kubectl describe configmap NAME' to check.
Summary
Use 'kubectl create configmap NAME --from-literal=key=value' to create ConfigMaps from command line values.
Verify the ConfigMap content with 'kubectl get configmap NAME -o yaml' or 'kubectl describe configmap NAME'.
Avoid spaces around equal signs and duplicate keys when using --from-literal.