Creating ConfigMaps
ConfigMaps store non-sensitive configuration data as key-value pairs. They decouple configuration from container images, letting you change settings without rebuilding or redeploying.
From Literal Values
Create a ConfigMap directly from command-line key-value pairs:
kubectl create configmap app-config \
--from-literal=DATABASE_HOST=postgres \
--from-literal=DATABASE_PORT=5432 \
--from-literal=LOG_LEVEL=info
From a File
Store an entire file as a ConfigMap entry. The filename becomes the key:
# Single file
kubectl create configmap nginx-config --from-file=nginx.conf
# Custom key name
kubectl create configmap nginx-config --from-file=default.conf=nginx.conf
# Entire directory
kubectl create configmap config-dir --from-file=./config/
From an Env File
Load key-value pairs from a .env-style file:
# app.env contains: KEY=value (one per line)
kubectl create configmap app-config --from-env-file=app.env
Unlike --from-file, this creates individual keys for each line rather than storing the file as a single entry.
YAML Definition
Define a ConfigMap declaratively for version control:
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
namespace: default
data:
DATABASE_HOST: postgres
DATABASE_PORT: "5432"
LOG_LEVEL: info
app.properties: |
server.port=8080
server.context-path=/api
feature.cache.enabled=true
Apply it with:
kubectl apply -f configmap.yaml
Verifying
kubectl get configmap app-config
kubectl describe configmap app-config
kubectl get configmap app-config -o yaml
All values in a ConfigMap are stored as strings. Numeric values like port numbers must be quoted in YAML definitions.