Today, in the world of application development, it’s recommended to separate the configuration from the code. Kubernetes’ ConfigMapshelps us do this.
Create a ConfigMap
ConfigMap allows you to define key and values or a properties file that can be used either at startup of the pod, or as a mount on the pod.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We can use the ConfigMap for configuration inside a pod in the following ways:
Using commands and arguments inside a container.
Using environment variables for a container
Adding the ConfigMaps as a file in read-only volume, from where the application can read.
Using the Kubernetes API in the code running inside the container that can read the configmap.
Example
Here, we load the ConfigMaps values during pod initialization as environment variables.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Following is an example of how to use values from a ConfigMap at the time of container initialization:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Volumes are set at the pod level, then mounted into containers inside that pod. The mounted configuration maps are updated automatically when the value in the `ConfigMap` changes.
Conclusion
ConfigMaps are a great mechanism to set up configurations separate from code. The configurations are updated automatically and redeployment is not required.
Note: The above does not hold true if the ConfigMap is marked as immutable.
One thought on “What is a ConfigMap in Kubernetes?”
Comments are closed.