Creating ConfigMaps from literals in Kubernetes - Performance & Efficiency
When creating ConfigMaps from literals in Kubernetes, it is useful to understand how the time to create them grows as you add more literals.
We want to see how the number of literals affects the work Kubernetes does to create the ConfigMap.
Analyze the time complexity of the following command.
kubectl create configmap example-config \
--from-literal=key1=value1 \
--from-literal=key2=value2 \
--from-literal=key3=value3
This command creates a ConfigMap named example-config with three key-value pairs provided directly as literals.
- Primary operation: Processing each literal key-value pair to add it to the ConfigMap.
- How many times: Once for each literal provided in the command.
As you add more literals, Kubernetes processes each one individually to build the ConfigMap.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Processes 10 literals |
| 100 | Processes 100 literals |
| 1000 | Processes 1000 literals |
Pattern observation: The work grows directly with the number of literals; doubling literals doubles the work.
Time Complexity: O(n)
This means the time to create the ConfigMap grows linearly with the number of literals you provide.
[X] Wrong: "Adding more literals won't affect the creation time much because it's just one command."
[OK] Correct: Each literal is processed separately, so more literals mean more work and longer creation time.
Understanding how input size affects command execution helps you explain efficiency and resource use clearly, a useful skill in real-world DevOps tasks.
What if we changed from literals to loading data from a file? How would the time complexity change?
Practice
kubectl create configmap command with the --from-literal option do?Solution
Step 1: Understand the command purpose
Thekubectl create configmapcommand is used to create a new ConfigMap resource in Kubernetes.Step 2: Role of
The--from-literal--from-literaloption allows adding key-value pairs directly from the command line as literals, without needing a file.Final Answer:
Creates a ConfigMap with key-value pairs directly from command line literals -> Option DQuick Check:
Creating ConfigMap from literals = Creates a ConfigMap with key-value pairs directly from command line literals [OK]
- Confusing create with delete commands
- Thinking --from-literal reads from files
- Assuming it lists ConfigMaps
app-config with two literals: mode=production and debug=false?Solution
Step 1: Check correct option format for literals
The correct syntax requires each literal to be passed with--from-literal=key=valueformat.Step 2: Validate kubectl create configmap app-config --from-literal=mode=production --from-literal=debug=false
kubectl create configmap app-config --from-literal=mode=production --from-literal=debug=false uses--from-literal=mode=productionand--from-literal=debug=false, which is the correct syntax.Final Answer:
kubectl create configmap app-config --from-literal=mode=production --from-literal=debug=false -> Option AQuick Check:
Correct literal syntax uses '=' after --from-literal [OK]
- Omitting '=' after --from-literal
- Passing multiple literals without repeating --from-literal
- Using spaces instead of '=' between key and value
kubectl create configmap test-config --from-literal=env=dev --from-literal=version=1.2
Then run:
kubectl get configmap test-config -o yaml
Solution
Step 1: Understand multiple literals usage
The command uses two--from-literaloptions, which is valid to add multiple key-value pairs.Step 2: Output of
The output YAML will show a ConfigMap named 'test-config' with a data section containing keys 'env' and 'version' with their respective values.kubectl get configmap -o yamlFinal Answer:
A ConfigMap named 'test-config' with data keys 'env' and 'version' set to 'dev' and '1.2' -> Option AQuick Check:
Multiple literals create multiple keys in ConfigMap data [OK]
- Thinking multiple literals cause errors
- Expecting keys to include '=' sign
- Assuming ConfigMap data is empty
kubectl create configmap my-config --from-literal=key1=value1 key2=value2
But it fails with an error. What is the likely cause?
Solution
Step 1: Analyze command syntax
The command uses--from-literal=key1=value1correctly but then addskey2=value2without--from-literal=.Step 2: Understand correct usage
Each literal must be preceded by its own--from-literal=option. Omitting it causes a syntax error.Final Answer:
Missing '=' after --from-literal for the second key-value pair -> Option BQuick Check:
Each literal needs its own --from-literal= [OK]
- Passing multiple literals without repeating --from-literal
- Assuming one --from-literal can take multiple pairs
- Confusing --from-literal with --from-file
app-settings with these key-value pairs:env=production, debug=false, and timeout=0. You run:kubectl create configmap app-settings --from-literal=env=production --from-literal=debug=false --from-literal=timeout=0
Later, you mount this ConfigMap as environment variables in a Pod. What should you be careful about regarding the
timeout value?Solution
Step 1: Understand ConfigMap data types
ConfigMaps store all values as strings, so '0' is stored as the string "0".Step 2: Effect when used as environment variable
When mounted as environment variables, the value will be "0" (string), not a numeric zero or boolean false.Final Answer:
The value '0' is a string and will be passed as "0", not as a number zero -> Option CQuick Check:
ConfigMap values are strings, even '0' [OK]
- Assuming '0' is treated as false or ignored
- Expecting automatic type conversion
- Thinking ConfigMap creation fails with '0' value
Ready to prove it?
Pass the test and this pattern moves unknown
