Hello, readers! In this article, we will learn the easy ways to configure Kubernetes ConfigMaps in Applications, in detail.
So, let us begin!! 🙂
Kubernetes ConfigMaps – Quick Overview!
As understood in our previous article on ConfigMaps, it is actually a kubernetes resource that can be used to store key-value pairs into it. Though it does not provide security and its limit cannot exceed 1MiB, still it is preferred over the monotonous technique of storing key-value labels for a particular application using the concept of environment variables.
In the context of this topic, we will be covering two different ways of creating a Kubernetes ConfigMap. Also, we will be going through 2 different ways of configuring them with Application other than the volume method.
Creating Kubernetes ConfigMaps
Kubernetes ConfigMaps can be created in the environment using two techniques–
- Imperative Way
- Declarative Way
Let us have a look at them one by one!
1. Imperative way
By Imperative way, we mean to say that we can create ConfigMap from the kubectl command directly without using any configuration YAML file for it.
Have a look at the below two commands–
kubectl create configmap app-config --from-literal=color=blue --from-literal=env=prod
kubectl create configmap app-config --from-file=/usr/src/app/tmp/data.json
- In the first command, we create a configMap that stores key-value pair (color=blue) by using –from-literal in it.
- We can also create a configMap from a file, in the second command, we do the same by specifying the path of the command.
Though the Imperative technique of creation may sound easy, it can be a cumbersome way when it comes to storing huge amount of key-value pairs into it.
To overcome this issue, declarative way comes into picture.
2. Declarative way
In the Declarative technique, we create a configuration YAML for the configMap resource. Within which, under the data configuration, we add the key-value pairs as literal values.
configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config-file
data:
color: blue
env: prod
Further, we use the kubectl apply -f command to apply the above configMap resource for a particular namespace.
kubectl apply -f configMap.yaml -n demo-namespace
3 Ways to Configure Kubernetes ConfigMaps in the Application
Once the configMap is created, we can make use of the below command to confirm the presence of it–
kubectl get cm
Further to get a detailed description of the configmap, we can make use of kubectl describe as shown below–
kubectl describe configmaps
Moving ahead, we can now configure the created configMap into the Application, by specifying the details into the Pod YAML file.
Let us go over the techniques for the same in the upcoming section.
1. ConfigMaps as environment variable in the Pod
We can specify the configMap into the Application by injecting its details into the pod as an environment variable resource.
apiVersion: v1
kind: Pod
metadata:
name: color-app
spec:
containers:
- name: color-app
image: color:1.0
ports:
- containerPort: 8080
envFrom:
- configMapRef:
name: app-config-file
As seen above, we specify the entire configMap as a reference (configMapRef) to the created file and then link it to the Pod YAML configuration.
2. ConfigMap as a single environment portion in the Pod
But what if, from all the key-value pairs we store in the configMap, we only want to associate specific key to its dedicated value from ConfigMap?
For that, we can make use of Single environment variable method to map configMaps to Pod.
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- name: test
image: k8s.gcr.io/hello
env:
# Define the environment variable
- name: color
valueFrom:
configMapKeyRef:
# The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
name: app-config-file
# Specify the key associated with the value
key: color
As seen above, we can mention the key value under the env section, and then map it to the dedicated value from configmap by specifying the key name in the configMapKeyRef section.
3. Using ConfigMaps as a volume in the Pod
We can also mount ConfigMaps as volumes onto the Pod for the application to refer. It has already been covered in the previous article.
Conclusion
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 Kubernetes, Stay tuned with us.
Till then, Happy Learning!! 🙂