How To Manage Kubernetes ConfigMaps?
Last Updated :
22 Jan, 2024
ConfigMaps in Kubernetes are used to store non-confidential data in key-value pairs. They allow you to decouple configuration from image content, making your applications more portable. Managing ConfigMaps involves creating, updating, and using them within your Kubernetes environment. Pods can consume ConfigMaps as environment variables, command-line arguments, or configuration files in a volume.
ConfigMap simplifies application configuration and management. Instead of hardcoding configuration data within your application or Docker container, you can externalize it using a ConfigMap.
Creating and Implementing ConfigMaps
A. Steps to create a Configmap using kubectl
ConfigMaps in Kubernetes serves as a method to inject configuration data into containers. To create a ConfigMap using kubectl, follow these steps:
Step 1: Define your Kubernetes
Start by defining the ConfigMap in a YAML file. This involves specifying the apiVersion, kind, and metadata. Within the data section, add your configuration as key-value pairs. Each key under data represents a different configuration item.

Step 2: Create ConfigMap in cluster
Use the kubectl create command to add your ConfigMap to the Kubernetes cluster.
For example:
kubectl create -f filename.yaml
This command reads your YAML file and creates a ConfigMap in your specified environment.
Step 3: Verify creation
Ensure your ConfigMap is correctly created or not by giving the following command. which lists all ConfigMaps in your current kubernetes namespace.
kubectl get configmaps
B. Using ConfigMaps in Pods
Step 1: Reference ConfigMap in Pod Specification
In your pod's YAML file, under spec, use env to define environment variables. Reference the ConfigMap with 'valueFrom' and 'configMapKeyRef' to assign values from your ConfigMap to the container's environment variables.
Step 2: Mount ConfigMap as Volume
For more complex configurations, you can mount the entire ConfigMap as a volume. Under volumes, reference your ConfigMap, and use 'volumeMounts' in the container spec to specify the 'mountPath'.

C. Example code snippets
For complex applications, consider using multiple ConfigMaps. This approach allows you to organize configuration data logically and maintain cleaner code.

This example demonstrates referencing multiple ConfigMaps (database-config and App-config) in a single file, showcasing how to manage different aspects of configuration separately.
- Managing changes: When updating ConfigMaps, remember that changes don’t automatically propagate to pods. You may need to restart the affected services for the updated configurations to take effect.
- You can update ConfigMap by below command
kubectl create configmap my-configmap --from-file=my-config.properties --dry-run=client -o yaml | kubectl apply -f -
- Restart the pods to pickup the new configuration
kubectl rollout restart deployment my-deployment
Managing ConfigMaps
A. Storage and Lifecycle management
In a Kubernetes cluster, ConfigMaps are fundamental for storing non-sensitive data in key-value pairs. Stored within the Kubernetes API, these ConfigMaps can be referenced by pods and provide a mechanism to inject configuration data into containers. Utilizing kubectl, you can create, update, and manage these configmaps, ensuring that the right configuration is applied to the right service at the right time. This approach allows for dynamic updating of environment variables and configuration data, minimizing the need for container rebuilds.
B. Handling changes and updates in configmaps
When changes occur in the ConfigMaps, it's crucial to propagate these changes effectively across the cluster. Update your ConfigMaps through the kubectl command, and Kubernetes ensures that the updated values are mounted into the containers via volumes or environment variables. To apply the changes, you might need to restart the associated pods. This process highlights the importance of understanding the connection between ConfigMaps, pods, and how Kubernetes handles updates to maintain service stability and functionality.
ConfigMaps, thus, serve as a versatile tool, managing and applying changes across multiple environments in your Kubernetes cluster, enhancing the efficiency of deploying and managing applications.
Best Practices
- Use ConfigMaps for configuration data that changes less frequently than the application code.
- Add comments or descriptions within ConfigMap YAMLs for better understanding.
- Version ConfigMaps by including a version number in their names.
Overall, ConfigMaps are a flexible way to inject configuration data into pods separately from container images.
Conclusion
In conclusion, ConfigMaps in Kubernetes are a powerful tool for managing environment-specific configuration data in a Kubernetes cluster. By decoupling configuration data from container images, you can easily deploy and manage applications in different environments using the same container image.
Similar Reads
Kubernetes - ConfigMaps Kubernetes allows you to run and manage applications in containers. However, when you need to update configurations like usernames, passwords, or URLs without modifying the application code, ConfigMaps provide an efficient solution. ConfigMaps separate application configuration from the application
10 min read
How to Manage Kubernetes Secrets ? Most applications deployed through Kubernetes require access to databases, services, and other resources located externally. The easiest way to manage the login information necessary to access those resources is by using Kubernetes secrets. Secrets help organize and distribute sensitive information
12 min read
How to Deploy Kubernetes on GCP? Kubernetes, the open-supply field orchestration platform, has emerged as the standard for coping with containerized applications. Google Cloud Platform (GCP) provides a robust and scalable surrounding for deploying Kubernetes clusters. In this comprehensive manual, we are able to walk you through th
5 min read
How to Change Namespace in Kubernetes ? Kubernetes namespaces offer an indispensable instrument for resource organization and isolation in the ever-changing realm of container orchestration. Like distinct districts in a busy metropolis, each namespace in your Kubernetes cluster serves as a dedicated place where you may organize related st
4 min read
Kubernetes Pods: How to Create and Manage Them Kubernetes is an open-source container orchestration system mainly used for automated software deployment, management, and scaling. Kubernetes is also known as K8s. Kubernetes was originally developed by Google, but it is now being maintained by the Cloud Native Computing Foundation. It was original
13 min read