Kubernetes
kubectl
configMap
configuration management
DevOps

Kubectl update configMap

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

In the Kubernetes ecosystem, ConfigMap is a crucial API resource used for managing configuration data. This data can be consumed by application containers running in Pods, allowing for dynamic configuration management without the need to modify container images. Often, it is necessary to update a ConfigMap when application configurations change, and kubectl provides several ways to facilitate this. Understanding how to efficiently update a ConfigMap is essential for maintaining the smooth operation of applications.

Creating a ConfigMap

Before discussing update strategies, let's first understand how a ConfigMap is created. Here's an example of creating a ConfigMap from a key-value pair:

bash
kubectl create configmap my-config --from-literal=key1=value1

Alternatively, you can create a ConfigMap from a file:

bash
kubectl create configmap my-config --from-file=config-file.properties

Updating a ConfigMap

There are several methods to update a ConfigMap, each suited for different scenarios.

1. Using kubectl edit

One straightforward approach is using the kubectl edit command. This method opens the current ConfigMap in a text editor (typically vi or the one set in the $EDITOR environment variable), allowing you to make interactive updates:

bash
kubectl edit configmap my-config

This method is suitable for quick edits and allows for directly viewing the impact of changes.

2. Using kubectl apply

When changes are more extensive or need to be applied programmatically, consider using kubectl apply with a YAML configuration file. First, export the existing ConfigMap:

bash
kubectl get configmap my-config -o yaml > my-config.yaml

Then, modify the my-config.yaml file as needed and apply the changes:

bash
kubectl apply -f my-config.yaml

This method supports version control systems and integration with CI/CD pipelines, making it ideal for team environments and automated deployments.

3. Using kubectl create configmap with --dry-run and -o yaml

This technique involves creating a new manifest from scratch when you cannot directly update the current YAML file:

bash
kubectl create configmap my-config --from-literal=key1=value2 --dry-run=client -o yaml > my-updated-config.yaml
kubectl apply -f my-updated-config.yaml

This is useful in situations where major revisions are being made, and it's necessary to verify changes prior to application.

Example Scenario

Consider a scenario where your application needs updated database credentials due to a routine password change. Below is how you'd typically manage this:

  1. Export existing ConfigMap:
bash
   kubectl get configmap db-config -o yaml > db-config.yaml
  1. Update the db-config.yaml file to reflect new credentials:
yaml
1   apiVersion: v1
2   kind: ConfigMap
3   metadata:
4     name: db-config
5   data:
6     database: mydatabase
7     username: myuser
8     password: newpassword
  1. Apply changes with:
bash
   kubectl apply -f db-config.yaml
  1. Restart your pods (if necessary) to ensure they pick up the new configuration:
bash
   kubectl rollout restart deployment/my-deployment

Key Points Summary

MethodUse-CaseAdvantages
kubectl editSmall, quick editsFast, immediate feedback
kubectl apply from a fileAutomatic, team environmentsVersion control, scriptability
kubectl create --dry-run + applyMajor revisions, initial creationVerification before application

Considerations

  • Restarting Pods: After updating a ConfigMap, pods using it might need a restart to read the new configurations.
  • Imperative vs Declarative: Choose between imperative (edit) and declarative (apply) approaches based on operational needs.
  • Security: Be cautious with sensitive data in ConfigMap, such as passwords; consider alternatives like Secrets if encryption is required.

By leveraging these techniques, managing configurations in Kubernetes becomes a more streamlined and integrated process. The ability to update ConfigMaps effectively is fundamental to modern, dynamic application management in Kubernetes environments.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.