ConfigMap injection methods¶
ConfigMap injection methods are the techniques used to pass configuration data stored in Kubernetes ConfigMaps into running containers.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md] These methods allow applications to consume configuration without requiring changes to the application code, thereby facilitating different deployment environments such as Development or Production.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]
There are primarily two ways to inject ConfigMap data: as Environment Variables or as Mounted Files (Volumes).^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]
Environment Variable Injection¶
This method injects specific key-value pairs from a ConfigMap directly into the container's environment variables.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]
To use this method, the Pod definition must use the valueFrom and configMapKeyRef fields within the env section.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md] This explicitly maps an environment variable name to a specific key within the ConfigMap.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]
env:
- name: POSTGRES_USER
valueFrom:
configMapKeyRef:
name: initdb-kv-yaml
key: PG_USER
This approach is suitable for simple configuration data like database usernames or ports.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]
Volume Injection¶
This method mounts the entire content of a ConfigMap (or specific files) into the container's filesystem as files.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]
In the Pod definition, a volumes section is defined using the configMap type, specifying the name of the ConfigMap to use.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md] Correspondingly, the volumeMounts section in the container specification defines the mountPath where these files will appear.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]
volumes:
- name: initdb
[ConfigMap](<./configmap.md>):
name: initdb
volumeMounts:
- mountPath: /docker-entrypoint-initdb.d
name: initdb
This is particularly useful for injecting larger configuration files, such as initialization scripts (e.g., initdb.sql).^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]
Sources¶
^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]
Related¶
- ConfigMap
- [[Kubernetes Volumes]]