Mastering ConfigMaps: The Key to Dynamic Kubernetes Configurations
ConfigMaps exist to solve a fundamental problem in application deployment: the need to separate environment-specific configurations from container images. This decoupling makes your applications more portable and easier to manage across different environments. By using key-value pairs, ConfigMaps store non-confidential data that can be consumed in various ways, such as environment variables, command-line arguments, or configuration files in a volume.
When a Pod is launched, the kubelet utilizes the data from the ConfigMap. If the ConfigMap is updated, the projected keys in mounted volumes are eventually refreshed, as the kubelet checks for freshness during periodic syncs. You can also create immutable ConfigMaps starting from v1.19, which prevents changes after creation, adding an extra layer of stability for your configurations.
In production, remember that ConfigMaps do not provide secrecy or encryption; use Secrets for confidential data instead. They also have a size limitation of 1 MiB, so avoid storing large data sets. If you're mounting a ConfigMap as a subPath volume, be aware that updates won't propagate to the container. These nuances are critical for ensuring that your applications run smoothly and securely in a Kubernetes environment.
Key takeaways
- →Understand that ConfigMaps store non-confidential data in key-value pairs.
- →Utilize ConfigMaps to decouple environment-specific configurations from container images.
- →Remember that updates to mounted ConfigMaps are eventually reflected, but subPath mounts won't receive updates.
- →Use immutable ConfigMaps for configurations that should not change after creation.
- →Avoid using ConfigMaps for sensitive data; opt for Secrets instead.
Why it matters
In production, effectively managing configurations with ConfigMaps can significantly enhance your application's portability and reduce deployment errors. This leads to smoother operations and quicker iterations in your development cycle.
Code examples
apiVersion:v1kind:ConfigMapmetadata:name:game-demodata:# property-like keys; each key maps to a simple valueplayer_initial_lives:"3"ui_properties_file_name:"user-interface.properties"# file-like keysgame.properties:|enemy.types=aliens,monstersplayer.maximum-lives=5user-interface.properties:|color.good=purplecolor.bad=yellowallow.textmode=trueapiVersion:v1kind:Podmetadata:name:configmap-demo-podspec:containers:-name:demoimage:alpinecommand:["sleep","3600"]env:# Define the environment variable-name:PLAYER_INITIAL_LIVES# Notice that the case is different here# from the key name in the ConfigMap.valueFrom:configMapKeyRef:name:game-demo# The ConfigMap this value comes from.key:player_initial_lives# The key to fetch.-name:UI_PROPERTIES_FILE_NAMEvalueFrom:configMapKeyRef:name:game-demokey:ui_properties_file_namevolumeMounts:-name:configmountPath:"/config"readOnly:truevolumes:# You set volumes at the Pod level, then mount them into containers inside that Pod-name:configconfigMap:# Provide the name of the ConfigMap you want to mount.name:game-demo# An array of keys from the ConfigMap to create as filesitems:-key:"game.properties"path:"game.properties"-key:"user-interface.properties"path:"user-interface.properties"apiVersion:v1kind:Podmetadata:name:env-configmapspec:containers:-name:appcommand:["/bin/sh","-c","printenv"]image:busybox:latestenvFrom:-configMapRef:name:myconfigmapWhen NOT to use this
The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.
Want the complete reference?
Read official docsUnified observability — logs, uptime monitoring, and on-call in one place. Used by 50,000+ engineering teams to ship faster and sleep better.
Try Better Stack free →Unifying AI Workloads: KubeCon, OpenInfra, and PyTorch Conference in China
Discover how the convergence of KubeCon, OpenInfra Summit, and PyTorch Conference in China is set to revolutionize AI workloads. By integrating Kubernetes orchestration with OpenInfra's infrastructure and PyTorch's AI frameworks, organizations can achieve scalable and reliable AI solutions.
Flipkart's Chaos Engineering Triumph: Scaling Kubernetes with Confidence
Chaos engineering is essential for building resilient systems, and Flipkart's recent success showcases its power. By executing 90% of chaos experiments in staging, they ensure stability during high-traffic events. Discover how they customized LitmusChaos for their unique needs.
Extend Your CKA Certification: The Power of CKS
Want to keep your Kubernetes Administrator certification current? Passing the Certified Kubernetes Security Specialist (CKS) exam now extends your CKA certification. This new feature simplifies credential maintenance for cloud-native professionals.
Get the daily digest
One email. 5 articles. Every morning.
No spam. Unsubscribe anytime.