ConfigMaps are not designed for sensitive information. Use Secrets for credentials and tokens.
Why Use ConfigMaps?
Hard-coding environment variables in Pod specs is tedious when you maintain multiple workloads. Consider this example:ConfigMap Lifecycle
- Create a ConfigMap
- Inject it into your Pod
1. Creating a ConfigMap
Kubernetes supports both imperative and declarative creation methods.Imperative (kubectl) Approach
From literal key–value pairs:
Declarative (YAML) Approach
Defineconfig-map.yaml:
Use descriptive names and labels for each ConfigMap (e.g.,
app-config, mysql-config, redis-config) so you can manage them easily across environments.Inspecting ConfigMaps
List all ConfigMaps:2. Injecting ConfigMaps into Pods
You can consume ConfigMaps in two main ways:- As environment variables
- As files via a mounted volume
A. Environment Variables
Bulk Injection with envFrom
Inject all keys as environment variables:
Single Key Injection
Inject a specific key:B. File-based Injection
Mount ConfigMap entries as files inside the container:app-config appears as a separate file in /etc/config.