Introduction

Managing application configuration efficiently is essential in Kubernetes environments. Hardcoding configuration values such as database URLs, API endpoints, or application settings inside container images makes applications difficult to maintain and update.

Kubernetes provides ConfigMaps, a native resource that allows you to separate configuration data from application code. This enables administrators and developers to update application settings without rebuilding container images, making deployments more flexible and manageable.

This article explains what ConfigMaps are, why they are useful, how to create and use them, and the best practices for managing configuration in Kubernetes.

What is a ConfigMap?

A ConfigMap is a Kubernetes API object used to store non-confidential configuration data in key-value pairs.

Applications running inside Pods can consume ConfigMaps in multiple ways, including:

  • Environment variables
  • Command-line arguments
  • Configuration files mounted as volumes

ConfigMaps are intended for configuration data only. Sensitive information such as passwords, API keys, and certificates should be stored in Kubernetes Secrets instead.

Why Use ConfigMaps?

ConfigMaps offer several advantages for Kubernetes applications.

Separation of Configuration and Code

Application configuration is stored independently from container images, making deployments more portable.

Easy Configuration Updates

Configuration values can be modified without rebuilding Docker images.

Improved Reusability

The same application image can be deployed across development, testing, and production environments using different ConfigMaps.

Simplified Management

Centralized configuration makes maintaining multiple applications easier.

Creating ConfigMaps

There are multiple ways to create a ConfigMap.

Method 1: Using kubectl

Run the following command to create a ConfigMap.

kubectl create configmap app-config –from-literal=APP_NAME=MyApplication –from-literal=ENVIRONMENT=Production

Verify the ConfigMap.

kubectl get configmaps

View the ConfigMap details.

kubectl describe configmap app-config

Method 2: Using a YAML File

Create a file named configmap.yaml and add the following content.

apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_NAME: MyApplication
ENVIRONMENT: Production
LOG_LEVEL: INFO

Apply the ConfigMap.

kubectl apply -f configmap.yaml

Verify the ConfigMap.

kubectl get configmap app-config

Using ConfigMaps as Environment Variables

Create a Pod using the following YAML.

apiVersion: v1
kind: Pod
metadata:
name: volume-demo
spec:
containers:
– name: application-container
image: your-application-image:latest
volumeMounts:
– name: config-volume
mountPath: /etc/config
volumes:
– name: config-volume
configMap:
name: app-config

Create the Pod.

kubectl apply -f pod.yaml

Verify the Pod.

kubectl get pods

Mounting ConfigMaps as Volumes

Create the following Pod definition.

apiVersion: v1
kind: Pod
metadata:
name: volume-demo
spec:
containers:
– name: application-container
image: your-application-image:latest
volumeMounts:
– name: config-volume
mountPath: /etc/config
volumes:
– name: config-volume
configMap:
name: app-config

Deploy the Pod.

kubectl apply -f volume-demo.yaml

Verify the Pod.

kubectl get pods

View the mounted files.

kubectl exec -it volume-demo — ls /etc/config

Updating ConfigMaps

Edit the ConfigMap.

kubectl edit configmap app-config

Or update it using the YAML file.

kubectl apply -f configmap.yaml

Verify the updated ConfigMap.

kubectl describe configmap app-config

Restart the Deployment to apply the updated environment variables.

kubectl rollout restart deployment <deployment name >

Note: Applications using ConfigMaps as environment variables generally require a Pod restart to pick up updated values. Applications reading ConfigMaps from mounted volumes can detect changes depending on how the application is designed.

Conclusion

ConfigMaps are a fundamental Kubernetes resource that simplifies application configuration management. By separating configuration from application code, they improve flexibility, portability, and maintainability across different environments.

Whether used as environment variables or mounted as configuration files, ConfigMaps enable teams to update application settings without rebuilding container images, making Kubernetes deployments more efficient and easier to manage.

Leave a Reply