How to use ConfigMaps in Kubernetes?
· Category: Kubernetes
Short answer
ConfigMaps store non-sensitive configuration data as key-value pairs. They can be mounted as files inside a container or injected as environment variables, keeping configuration separate from application code.
Steps
- Create a ConfigMap from literals, files, or directories.
- Reference it in a Pod spec as environment variables or volumes.
- Update the ConfigMap and restart Pods to apply changes.
Example
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
database.properties: |
host=db
port=5432
Mount as a volume:
volumes:
- name: config
configMap:
name: app-config
Tips
- Use ConfigMaps for feature flags, URLs, and non-secret settings.
- Mounted ConfigMaps are read-only by default.
- Changes to a mounted ConfigMap propagate without restarting the Pod, but applications may need to reload.
Common issues
- ConfigMaps have a 1 MiB size limit.
- Binary data should use Secrets or binary ConfigMaps with encoding.
- Updating a ConfigMap does not automatically trigger Pod restarts.