In this blog post, we will explore what Kubernetes ConfigMaps are and when they should be used in Kubernetes deployments.
ConfigMaps are Kubernetes resources that can be used to store configuration information, such as passwords and API keys. They are stored in Kubernetes objects called ConfigMap objects.
ConfigMaps are a Kubernetes feature that allows you to decouple configuration from code. Configuration files can be managed as ConfigMaps and injected into pods at runtime. This is useful for managing configurations in a distributed system, where multiple instances of the same application may run simultaneously on different nodes.
Create a ConfigMap
To create a ConfigMap, you first need to create a Kubernetes manifest file. The following is an example ConfigMap manifest.
To deploy it I use Kubectl with the following command.
kubectl apply -f map.yaml
apiVersion: v1 kind: ConfigMap metadata: name: my-configmap data: myvar: myvalue
The manifest file defines a ConfigMap named my-configmap, with the key-value pair data containing. In this example, the myvar variable is set to myvalue.
Create Pod and Access ConfigMap
Once you have created a ConfigMap, you can inject it into your Kubernetes pods using the configMapRef attribute of the container runtime. The following is an example pod manifest:
kind: Pod apiVersion: v1 metadata: name: mypod spec: containers: - name: mypod image: nginx:latest envFrom: - configMapRef: name: my-configmap
If I log in to the container the myvar variable is available inside the container for apps to access it.
root@mypod:/# echo $myvar myvalue root@mypod:/#