Hello, readers! In this article, we will be focusing on Kubernetes ConfigMaps in detail.
So let us begin!! 🙂
Table of Contents
What is a ConfigMap object?
Kubernetes provides us with various resources to have our data on the platform and be orchestrated by the kubernetes configuration. One such API resource is ConfigMaps.
Kubernetes ConfigMaps is a as a key-value storage object and it enable us to store data in the form of key-value pairs.
With ConfigMaps, we can store the non-confidential data as a key value pair. Along with this, we can even decouple configuration specific to the environment through the container images with ConfigMaps. This makes our application portable.
Note: If you plan to have confidential data as key value pairs, it is always recommended to choose Secrets over ConfigMaps. Because, ConfigMaps do not provide any kind of encryption or protection of the data.
The data in the Kubernetes ConfigMaps cannot exceed 1MiB. It does not support heavy data storage.
Let us now create a ConfigMap in the upcoming section.
Creation of a Kubernetes ConfigMap object
Have a look at the below YAML schema of a Kubernetes ConfigMap:
apiVersion: v1 kind: ConfigMap metadata: name: demo data: name: "Joe" property: "Rented" code: "00124698701"
In the above YAML, we define the kind property as a ConfigMap. Further, we provide a name to the ConfigMap alongside the key .metdata.name as demo.
Unlike other resources, in ConfigMaps, we make use of data to store all the key-value pairs under it.
Referring ConfigMaps within Kubernetes Pods
Having defined a ConfigMap, now is the task to understand, how do we refer the ConfigMaps into the other Kubernetes resources to be utilized at the container level?
This section provides you with an answer.
We can specify the specification of the ConfigMap into the Pod. By this, it would then configure all the containers in that Pod based on the key-value paired data in the ConfigMap.
There are different ways to configure a container inside a Pod using ConfigMaps-
- Frame the code within k8 Pod that would use k8 API to read and implement a ConfigMap.
- Set environment variables for a container
- Add a read-only volume within a file that can be made accessible to an application
Let us now have a look at the below YAML wherein we have referred a ConfigMap within a Pod–
apiVersion: v1 kind: Pod metadata: name: demopod spec: containers: - name: demo-init image: hello-world volumeMounts: - name: demo-vol mountPath: "/etc/vol" readOnly: true volumes: - name: dem-vol configMap: name: demo
In the above configuration, we have mounted ConfigMap as a data volume. Multiple pods can refer the same ConfigMap.
For the same, we have followed the below steps:
- Create a ConfigMap.
- Add a volume spec under the Pod template and set a reference to the ConfigMap object by specifying it’s name.
- Add a volume mounts spec and name an unused directory within Pod where you want your ConfigMap to appear and reside.
In our case, we have made the ConfigMap reside and appear at /etc/vol directory within the Pod.
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.
For more such posts related to Docker and Kubernetes, Stay tuned with us.
Till then, Happy Learning!! 🙂