ConfigMaps are Kubernetes objects that store configuration data in key-value pairs. They provide a way to decouple configuration from container images, making applications more portable and easier to manage across different environments.
What are ConfigMaps?
ConfigMaps allow you to store non-confidential configuration data separately from your application code. This enables you to:
- Change configuration without rebuilding container images
- Use the same image across different environments (dev, staging, prod)
- Share configuration between multiple pods
- Update configuration without restarting containers (in some cases)
Creating ConfigMaps
Method 1: From Literal Values
kubectl create configmap app-config \
--from-literal=database_host=db.example.com \
--from-literal=database_port=5432 \
--from-literal=log_level=info
Method 2: From Files
kubectl create configmap app-config --from-file=config.properties
Method 3: From YAML Manifest
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
namespace: default
data:
database_host: "db.example.com"
database_port: "5432"
log_level: "info"
Using ConfigMaps in Pods
As Environment Variables
apiVersion: v1
kind: Pod
metadata:
name: app-pod
spec:
containers:
- name: app
image: myapp:latest
envFrom:
- configMapRef:
name: app-config
As Volume Mounts
apiVersion: v1
kind: Pod
metadata:
name: app-pod
spec:
containers:
- name: app
image: myapp:latest
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: app-config
Best Practices
- Separate ConfigMaps by Concern: Create separate ConfigMaps for different configuration types
- Use Immutable ConfigMaps: For configuration that never changes (Kubernetes 1.19+)
- Don't Store Sensitive Data: Use Secrets for sensitive information
- Use SubPath for Single Files: When mounting a single file, use subPath
- Validate Configuration: Add validation in your application
Troubleshooting
# Verify ConfigMap exists
kubectl get configmap app-config
# Check pod environment
kubectl exec <pod-name> -- env | grep <variable-name>
# Check volume mounts
kubectl describe pod <pod-name> | grep -A 10 "Mounts:"
Related Resources
Conclusion
ConfigMaps are essential for managing application configuration in Kubernetes. They enable you to separate configuration from code, making your applications more flexible and easier to manage.