Creating ConfigMaps Imperatively with kubectl
What You'll Learn
You'll learn how to create ConfigMaps on-the-fly using kubectl commands, without writing YAML manifests.
What Does "Imperatively" Mean?
When you create resources imperatively, you're telling Kubernetes exactly what to do right now through direct commands. Think of it like ordering food verbally at a restaurant versus handing the waiter a written order form (declarative). Both get you food, but one is immediate and command-based.
Three Ways to Create ConfigMaps with kubectl
From Literal Values
Use --from-literal to create key-value pairs directly in the command:
kubectl create configmap app-config \
--from-literal=DATABASE_URL=postgres://db:5432 \
--from-literal=LOG_LEVEL=info
This creates a ConfigMap named app-config with two configuration entries.
From Files
Use --from-file to load configuration from a file. The filename becomes the key, and the file contents become the value:
kubectl create configmap nginx-config \
--from-file=nginx.conf
You can also specify a custom key:
kubectl create configmap app-properties \
--from-file=config=application.properties
From Directories
Load all files in a directory at once:
kubectl create configmap multi-config \
--from-file=configs/
Each file in the configs/ directory becomes a separate key-value pair in the ConfigMap.
When to Use Imperative Creation
Imperative ConfigMap creation is perfect for testing, quick demos, or generating initial configurations. For production environments, you'll typically want declarative YAML manifests tracked in version control.
Key Takeaway: Use kubectl create configmap with --from-literal, --from-file, or directories to quickly generate ConfigMaps without writing YAML, ideal for development and testing scenarios.