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


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

kubectl create configmap <config-name> --from-file=<path-to-file>
kubectl create configmap app-config

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.yamlapiVersion: v1
kind: ConfigMap
name: app-config
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
name: simple-webapp-color
name: simple-webapp-color
- name: simple-webapp-color
image: simple-webapp-color
- containerPort: 8080
- 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





Software Engineer — Openshift CoreOS

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Minesweeper… nah Rock, Paper, Scissors

Using github as content management

Citizen Developers in manufacturing

Command line interface with an access denied banner placed over top

How to Set up Your New Computer for Data Science Projects with Python

Running Apache Flink with RocksDB on Azure Kubernetes Service

Introduction to Python Programming: Mapping — Dictionary

Flutter — Lottie Animation

The DevOps Handbook

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ayesha Kaleem

Ayesha Kaleem

Software Engineer — Openshift CoreOS

More from Medium

Architecting a development lifecycle for a Kubernetes-based deployments

Kubernetes + Adapter Pattern + Minikube

Opentelementry auto-instrumentation Configuration for GRPC based java application- Part1 (APM…

Secure Microservices Configuration Properties via HashiCorp Vault