Update ConfigMap without restarting Pods in Kubernetes
Check how you can avoid restarting or recreating Pods to propagate new changes in ConfigMaps
3 min read
This blog is basically a tip around Kubernetes Administration.
I am assuming you know about and have some experience in Kubernetes & ConfigMaps.
Anyways I will try to explain concepts as easy as possible 😀
This is how I used to feel when I started :
So Don't worry, Let's start improving ourselves, no matter how much but consistently 👍
When We use ConfigMaps?
When you want to supply environment-specific config values like env vars.
Using ConfigMaps, You can also mount your config as files in a particular directory which can be later read by our application.
So, In Short, using ConfigMaps, you can supply configs by :
- Setting up env vars
- Mounting the config values as a file (aka data volume).
You updated a ConfigMap, Now you want to propagate this updated change to all existing running Pods.
You can delete existing pods or restart the deployment rollout.
But You know what this is not feasible in some cases where you don't want any downtime or you simply just don't want to delete any running Pods.
Note - if you are using ConfigMaps to set env vars then this is the only option you have.
This option is only applicable if you have mounted your ConfigMap as a file in your Pod. Also if you are using ConfigMap as a subPath volume, this option won't be applicable.
So Let's discuss this option in detail.
The Option #2
This is not something which I have invented, rather I read this on the Official K8s docs page here
Actually, there are two ways you can achieve this,
1. Use Annotation
According to the docs I mentioned above, You can update the pod's annotations which will trigger a refresh and your ConfigMap data volume will be populated with the latest data. Simple! 👀
2. Wait for Kubelet to refresh ConfigMap data
If configured, Kubelet also tries to refresh the ConfigMaps for pods periodically to keep things in sync. This interval is called ConfigMap Cache TTL
Extra - There is one property called ConfigMapAndSecretChangeDetectionStrategy which you can configure when you start Kubelet. Possible values we can pass to this are : Get, Cache & Watch (Default) This property defines how we want Kubelet to detect and propagate ConfigMap and Secret changes.
More info : ConfigMaps | Kubernetes.
I hope this quick & short blog made sense :)
Kubernetes - Kubernetes is an open-source container orchestration system for automating software deployment, scaling, and management.
Containers - A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.
Pods - Pods are the smallest, most basic deployable objects in Kubernetes. Pods contain one or more containers.
ConfigMaps - A ConfigMap allows you to decouple environment-specific configuration (mostly environment variables) from your container images so that your applications are easily portable.
Did you find this article valuable?
Support Kratik Jain by becoming a sponsor. Any amount is appreciated!