Kubectl update configMap
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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:
Alternatively, you can create a ConfigMap from a file:
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:
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:
Then, modify the my-config.yaml file as needed and apply the changes:
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:
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:
- Export existing
ConfigMap:
- Update the
db-config.yamlfile to reflect new credentials:
- Apply changes with:
- Restart your pods (if necessary) to ensure they pick up the new configuration:
Key Points Summary
| Method | Use-Case | Advantages |
kubectl edit | Small, quick edits | Fast, immediate feedback |
kubectl apply from a file | Automatic, team environments | Version control, scriptability |
kubectl create --dry-run + apply | Major revisions, initial creation | Verification 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 likeSecretsif 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
- kubectl Use custom-columns output with maps
- kubectl wait --forconditioncomplete --timeout30s
- kubectl YAML config file equivalent of kubectl run ... -i --tty ...
- Kubelet - failed to CreatePodSandbox for coredns; failed to set bridge addr could not add ip addr to cni0 permission denied
- Kubernetes-services load balancing
- Kubernetes - can a Deployment have multiple ReplicaSets?
- kubelet does not have ClusterDNS IP configured in Microk8s
- kubelet saying node master01 not found

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.