Hello, readers! This article talks about the Ways to Generate Kubernetes ConfigMaps with different variants for the same.
So, let us begin!! 🙂
Use of ConfigMaps in Kubernetes
ConfigMaps is a Kubernetes resource that enables us to store the configuration artefacts and information at a single place that can be inculcated easily within a container for the use of the application.
The limit of information for a single ConfigMap cannot exceed 1MiB in size, and it stores the data in the form of a key-value pair. This can be a preferred method of storing the configuration related data instead of having them in the form of environment variables for the application.
This article does not focus on the method to generate ConfigMaps such as the Imperative and Declarative methods. Rather, it focuses on the different variants through which we can generate a ConfigMap from different data resources.
So, let’s have a look at each one of them in the upcoming section.
1. Developing ConfigMaps from directories
We can create the ConfigMaps for any application through the data from directories. For the same, the kubectl tool identifies the basename that is a valid key and packages the information into a brand new ConfigMap.
The below command can be used to generate a ConfigMap from multiple files sitting within the same directory.
Example: Generating config maps from a directory
mkdir -p cm/dir wget https://kubernetes.io/examples/configmap/game.properties -O cm/dir kubectl create configmap demo-config --from-file=cm/dir
In the above example, we follow the steps to generate the ConfigMap-
- At first, we create a local directory/folder.
- We then store any sample file to the above created location.
- Finally, we generate a ConfigMap out of the file stored at the above location.
Thus, within the same directory, we can have multiple files being passed for the generation of the ConfigMap.
2. Develop ConfigMaps from literal values
Apart from using directories and files, we can even create ConfigMaps using the value for a literal that can be generated at a run time.
Have a look at the below example–
kubectl create configmap demo-config --from-literal=will=YES --from-literal=demo.type=NO
Using the above command, we can create a ConfigMap that picks up the data from the literal values specified.
Let us now look at the output configuration of the generated config map-
kubectl get cm demo-config -o yaml
apiVersion: v1 kind: ConfigMap metadata: creationTimestamp: 2021-09-24 name: demo-config namespace: default resourceVersion: "231" uid: dadce046-d673-11e5-8cd0-68234567fd data: will: YES demo.type: NO
3. Develop ConfigMaps from a file
We had a look at the creation of ConfigMaps from directories. Likewise, we can generate a ConfigMap from a single file or even multiple files as shown below-
kubectl create configmap demo-config-2 --from-file=cm/dir/dir.properties
The above command would produce the ConfigMap as shown below-
kubectl describe cm demo-config-2
Name: demo-config-2 Namespace: default Labels: <none> Annotations: <none> Data ==== dir.properties: ---- data=natural secret.code.passphrase=DDDDAAAWERE secret.code.allowed=false secret.code.time=1230
In order to create ConfigMaps from multiple files in a single go, we can make use of the –from-file argument as shown below-
kubectl create configmap demo-config-2 --from-file=cm/dir/dir1.properties --from-file=cm/dir/dir2.properties
This command creates a ConfigMap from two files that sit under the same directory but are different altogether.
By this, we have approached the end of this topic. Feel free to comment below, in case you come across any questions.
For more such posts related to Docker and Kubernetes, Stay tuned with us.
Till then, Happy Learning!! 🙂