How to Configure ConfigMaps in Applications in Kubernetes?

How to Configure ConfigMaps in Applications in Kubernetes?

Introduction

Many applications require configuration data such as server URLs, API keys, and other settings. In Kubernetes, ConfigMaps make it easy to manage this type of configuration data and inject it into your applications as environment variables or configuration files.

In this blog, we will learn how to create and configure ConfigMaps and how to use them in applications running in Kubernetes.

What is a ConfigMap?

ConfigMaps are Kubernetes objects that contain key-value pairs of configuration data. These key-value pairs can be used to store environment variables, command-line arguments, configuration files, and other kinds of data.

ConfigMaps are decoupled from the applications that use them, which means that you can modify them without changing your application code. This makes ConfigMaps a great way to manage configuration data for your applications.

Creating a ConfigMap

We can create a ConfigMap from a YAML file, or from the command line. Let's create a simple ConfigMap that contains a few key-value pairs:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
  namespace: default
data:
  server.url: http://example.com
  api.key: abcdefg

In this YAML file, we're creating a ConfigMap called "my-config" in the "default" namespace. The data field contains two key-value pairs, "server.url" and "api.key".

We can create this ConfigMap by running the following command:

$ kubectl apply -f configmap.yaml

This will create the ConfigMap in Kubernetes, and we can verify that it exists by running:

$ kubectl get configmap my-config

Using ConfigMaps in Applications

Now that we've created a ConfigMap, let's see how we can use it in an application. There are two main ways to use ConfigMaps in applications: as environment variables, or as configuration files.

Using ConfigMaps as Environment Variables

To use a ConfigMap as an environment variable in an application, we need to define the ConfigMap as a Kubernetes environment variable. We can do this in our application's deployment YAML file.

Let's say that we have a deployment YAML file for our application that contains a container definition:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app-image:latest
        ports:
        - containerPort: 8080

To use the ConfigMap we created earlier as environment variables, we can modify the pod definition like this:

spec:
  containers:
  - name: my-app
    image: my-app-image:latest
    ports:
    - containerPort: 8080
    env:
    - name: SERVER_URL
      valueFrom:
        configMapKeyRef:
          name: my-config
          key: server.url
    - name: API_KEY
      valueFrom:
        configMapKeyRef:
          name: my-config
          key: api.key

In this example, we're defining two environment variables, "SERVER_URL" and "API_KEY", which will be populated with the values from the "server.url" and "api.key" keys in our ConfigMap.

Using ConfigMaps as Configuration Files

We can also use ConfigMaps as configuration files in our applications. To do this, we need to define the ConfigMap as a Kubernetes volume and mount it into the pod running our application.

Let's say that our application reads its configuration from a file called "config.properties". We can create a ConfigMap that contains this file, and then mount it as a volume in our application's pod:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
  namespace: default
data:
  config.properties: |
    SERVER_URL=http://example.com
    API_KEY=abcdefg

In this example, we're creating a ConfigMap called "my-config" that contains a file called "config.properties". The contents of this file are the same as the environment variables we defined earlier.

To mount this ConfigMap as a volume in our application's pod, we can modify our deployment YAML file like this:

spec:
  containers:
  - name: my-app
    image: my-app-image:latest
    ports:
    - containerPort: 8080
    volumeMounts:
    - name: config-volume
      mountPath: /app/config
  volumes:
  - name: config-volume
    configMap:
      name: my-config

In this example, we're defining a volume called "config-volume" that is backed by the "my-config" ConfigMap. We're then mounting this volume at the path "/app/config" in our application's container.

Conclusion

ConfigMaps are an essential tool for managing configuration data in Kubernetes. In this tutorial, we've learned how to create ConfigMaps, and how to use them as environment variables and configuration files in our applications. With this knowledge, you can manage your application configurations more easily and keep your code decoupled from configuration data.

source:- https://kubernetes.io/docs/concepts/configuration/configmap/