Over the years, Kubernetes has become the go-to platform for managing containerized applications.

However, one big challenge in the container ecosystem is efficiently managing configuration data for applications running on Kubernetes clusters.

This is where Kubernetes Config Maps play a key role.

Kubernetes Config Maps provides a mechanism for storing and managing configuration data separate from your application code. In other words, you are able to decouple the configuration of your application with the code you commit to a source repository.

In this post, we will look at how to create a Kubernetes Config Map and use it within a Pod.

1 – What is Kubernetes Config Map?

Kubernetes Config Map is basically a Kubernetes resource or object.

Its job is to store configuration data in a key-value pair format. In the context of a Kubernetes Cluster, the Config Map acts as a centralized repository for all configuration settings.

With config maps, you don’t need to hard-code configuration values into your application containers or code. Also, modifying the configurations and redeploying the application becomes much easier.

Here are a couple of important use-cases for config maps in Kubernetes:

  • Storing configuration data that can change independently of the application code. Think of environment variables, database connection strings and other settings.
  • Sharing configuration data across multiple pods or applications.

When you think about the advantages of using Kubernetes Config Maps, they promote a cleaner separation of code and configuration. This is a key best practice for building containerized applications and allows DevOps engineers on your team to manage configurations independently of developers.

Config Maps also make configuration updates easier.

You can just apply the change to the Config Map without redeploying the application or rebuilding the container image.

2 – How to create a Kubernetes Config Map

There are multiple ways to create a Kubernetes Config Map.

Let’s look at each approach with examples:

Using Config Map YAML file

Consider the scenario where you want to store a simple key-value pair such as a database URL in a Config Map.

The below YAML file defines such as Config Map.

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config-map
data:
  mongodb-url: mongodb://mongo-host:27017/mydb

As you can see, the name of the Config Map is “my-config-map”.

It has a single key-value pair. The key is mongodb-url and the value is mongodb://mongo-host:27017/mydb.

You can now apply this configuration file by using the below command.

$ kubectl apply -f config-map.yaml

Using a Property File

You can also create a Kubernetes Config Map directly with the kubectl command by providing a property file containing key-value pairs.

You can create a configuration file using JSON as follows:

{
    "db_url": "mongodb://mongo-host:27017/mydb"
}

Next, you can simply use the below command to create the config map using the file.

$ kubectl create configmap db-url --from-file=config.json
configmap/db-url created

Multi-Part Configuration File

Config Maps in Kubernetes are highly versatile.

For example, you can even have a multi-part configuration file as below:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-multi-config
data:
  database-config.yml: |
    database:
      host: localhost
      port: 5432
      username: myuser
      password: mypassword
  application-config.yml: |
    application:
      log-level: DEBUG
      max-connections: 100

This can be applied using kubectl as follows:

$ kubectl apply -f multi-part-config.yaml
configmap/my-multi-config created

3 – Using Config Maps In Pods

After creating the config maps, you can use them within Kubernetes Pods.

Here are a couple of ways to do so:

Mounting Config Maps as Volumes

One of the most powerful features of Kubernetes Config Maps is their ability to be mounted as volumes within the pods. If interested, you can read more about Kubernetes Volumes.

This way you can inject configuration data directly into the file system of your containers, making it easy for the application code to access the configuration.

See the below example of a Pod YAML file where you define a volume and mount the Config Map into the pod’s container.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: my-app-image:latest
      volumeMounts:
        - name: config-volume
          mountPath: /config
  volumes:
    - name: config-volume
      configMap:
        name: my-config-map

In this example, you create a volume named config-volume and mount the Config Map named my-config-map into the /config directory of the container.

Setting Environment Variables from Config Maps

Kubernetes also lets you set environment variables in your pods based on Config Map data.

See the below example:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: my-app-image:latest
      env:
        - name: DATABASE_URL
          valueFrom:
            configMapKeyRef:
              name: my-config-map
              key: mongodb-url

Basically, here we create an environment variable DATABASE_URL in the pod’s container. The value is sourced from the mongodb-url key in the my-config-map Config Map.

4 – Best Practices for Kubernetes Config Maps

While using Config Maps, it’s important to adhere to a few best practices:

Naming Conventions

This is a fundamental best practice when working with Config Maps in Kubernetes.

Meaningful and structured names can simplify management and reduce the likelihood of naming conflicts.

See the below example:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-app-config

Organize Config Maps for Scalability

As your Kubernetes deployment grows, it’s important to maintain Config Maps in an organized manner.

To do so, group related Config Maps by application, environment or purpose to streamline management.

See the below example:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-a-config
  labels:
    app: app-a
data:
  ...

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-b-config
  labels:
    app: app-b
data:
  ...

In this example, Config Maps for different applications are labeled accordingly in order to make it easy to manage the configurations.

Conclusion

That’s all for this post.

To close things off, Kubernetes Config Maps are an extremely important tool for DevOps professionals as well as application developers.

Config Maps streamline the management of configuration data. They help decouple the application from the configuration code, allowing easy maintenance and changes whenever needed.

To learn another way of dealing with configuration aspects in Kubernetes, you can check out my post on creating and using Kubernetes Secrets.

If you have any comments or queries about this post, please mention them in the comments section below.

Categories: BlogKubernetes

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *