Configure ConfigMaps

Configure ConfigMaps

In this tutorial, we will discuss about how to configure ConfigMaps in Kubernetes.

Configuring ConfigMaps in Kubernetes

In the previous tutorial, we discussed how to define environment variables in a POD definition file.

When you have a lot of POD definition files, it will become difficult to manage the environment data stored within the query files.

We can take this information out of the POD definition file and manage it centrally using configuration maps.

ConfigMaps are used to pass configuration data in the form of key value pairs in Kubernetes.

When a POD is created, inject the ConfigMap into the POD. So the key value pairs that are available as environment variables for the application hosted inside the container in the POD.

There are 2 phases involved in configuring ConfigMaps. First create the ConfigMaps and second inject them into the POD.

Just like any other Kubernetes object, there are 2 ways of creating a ConfigMap.

The imperative way – without using ConfigMap definition file and the declarative way – by using a ConfigMap definition way.

Imperative Approach

If you do not wish to create a ConfigMap definition, you could simply use following command

$ kubectl create configmap <config-name> --from-literal=<key>=<value>

E.g

$ kubectl create configmap app-config --from-literal=APP_COLOR=pink

If you wish to add additional key value pairs then simply specify the –from-leteral options multiple times

$ kubectl create configmap app-config --from-literal=APP_COLOR=pink --from-literal=APP_MODE=production

However this will get complicated when you have too many configuration items. Another way to input configuration data is through a file.

$ kubectl create configmap <config-name> --from-file=<path-to-file>
$ kubectl create configmap app-config --from-file=app-config.properties
Declarative Approach

In this approach 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”.

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  APP_COLOR: pink
  APP_MODE: production

Once you are ready then you need to run the following command.

$ kubectl create -f config-map.yaml

Above command will creates the app-config ConfigMap with the values we specified. You can create as many ConfigMaps as you need.

To view ConfigMaps, you need to run the following command.

$ kubectl get configmaps
NAME            DATA           AGE
app-config      2              3s

To describe ConfigMaps, you need to run the following command.

$ kubectl describe configmaps

Above command will list the configuration data as well under the data section.

Now that we have the ConfigMap created let us proceed with another step configuring it with a POD.

apiVersion: v1
kind: Pod
metadata:
  name: my-web-app
spec:
  containers:
  - name: my-web-app-container
    image: my-web-app
    ports:
      - containerPort: 8080
    envFrom:
      - configMapRef:
          name: app-config

Configure ConfigMaps


Scroll to top