ConfigMaps in Kubernetes

ConfigMaps plays an important role in managing the environment variables between pods in Kubernetes. When we have many pod definition files so it is very difficult to manage the environment variables and its values. So, we create a config map definition file to define the variables in the form of key-value pair and inject those environment variables into the pod definition files and manage it centrally using Configuration Maps. We have two kinds of approaches to create config maps i.e. Imperative and declarative approach.
kubectl create configmap #Imperativekubectl create -f #Declarative
Just like any other Kubernetes objects, there are two ways of creating a ConfigMap. The imperative way — without using a ConfigMap definition file and the Declarative way by using a ConfigMap Definition file. If you do not wish to create a config map definition, you could simply use the kubectl create configmap command and specify the required arguments. Let’s take a look at that first. With this method, you can directly specify the key-value pairs in the command line. To create a configMap of the given values, run the kubectl create configmap command.
Create Configmaps
ConfigMap
APP_COLOR: blue APP_MODE: prod
Imperative Approach
kubectl create configmap <config-name> --from-literal=<key>=<value>kubectl create configmap app-config --from-literal=APP_COLOR=blue
--from-literal=APP_MOD=prod
kubectl create configmap <config-name> --from-file=<path-to-file>kubectl create configmap app-config --from-file=app_config.properties
The command is followed by the config name and the option –from-literal. The from-literal option is used to specify the key-value pairs in the command itself. In this example, we are creating a configmap by the name app-config, with a key-value pair APP_COLOR=blue. If you wish to add additional key-value pairs, simply specify the from literal options multiple times. However, this will get complicated when you have too many configuration items. Another way to input the configuration data is through a file. Use the –from-file option to specify a path to the file that contains the required data. The data from this file is read and stored under the name of the file
Declarative Approach: kubectl create –f
config-map.yaml
#config-map.yamlapiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_COLOR: blue
APP_MODE: prod
kubectl create –f config-map.yaml
Let us now look at the declarative approach. For this, we create a definition file, just like how we did for the pod. The file has apiVersion, kind, metadata and instead of spec, here we have “data”. The apiVersion is v1, kind is ConfigMap. Under metadata specify the name of the configmap. We will call it app-config. Under data add the configuration data in a key-value format. Run the kubectl create command and specify the configuration file name.
So that creates the app-config config map with the values we specified. You can create as many configmaps as you need in the same way for various different purposes. Here I have one for my application, other for mysql and another one for redis. So it is important to name the config maps appropriately as you will be using these names later while associating it with PODs.

View ConfigMaps
kubectl get configmapskubectl describe configmaps
ConfigMap in Pods
Now that we have the configmap created to let us proceed with Step 2 — Configuring it with a POD. Here I have a simple pod definition file that runs my application simple web application.
To inject an environment variable, add a new property to the container called envFrom. The envFrom property is a list, so we can pass as many environment variables as required. Each item in the list corresponds to a configMap item. Specify the name of the configmap we created earlier. This is how we inject a specific configmap from the ones we created before. Creating the pod definition file now creates a web application with a blue background.
# pod-definition.yamlapiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
labels:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
ports:
- containerPort: 8080
envFrom:
- configMapRef:
name: app-config # name taken from the congig-map.yaml
Run the following command to create the pod:
kubectl create –f pod-definition.yaml
What we just saw was using configMaps to inject environment variables. There are other ways to inject configuration data into PODs. You can inject a single environment variable or inject the whole configuration data as files in a volume

CheatSheet of ConfigMaps:
$ kubectl get cm
$ kubectl get cm --all -namespaces
$ kubectl get cm --all -namespaces -o yaml
References:
https://kubernetes.io/docs/tasks/configure-pod-container/configure-podconfigmap/