# Update ConfigMap without restarting Pods in Kubernetes

*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 : 
<img src="https://media1.giphy.com/media/kaq6GnxDlJaBq/giphy.gif?cid=790b76111bacced6c43e9192131284b8a76e057fcb996616&rid=giphy.gif&ct=g">

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 : 
1. Setting up env vars
2. Mounting the config values as a file (aka data volume).

### Problem Statement

**You updated a ConfigMap, Now you want to propagate this updated change to all existing running Pods.**  
 
### Options
#### Option #1
**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.** 

#### Option #2
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](https://kubernetes.io/docs/concepts/storage/volumes/#using-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](https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#mounted-configmaps-are-updated-automatically)


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](https://kubernetes.io/docs/concepts/configuration/configmap/#mounted-configmaps-are-updated-automatically).

---

I hope this quick & short blog made sense :) 

<img src="https://media1.giphy.com/media/t2eBr71ACeDC0/giphy.gif?cid=790b7611673694a4fbbad457fa36c78a6018d027890e7a5b&rid=giphy.gif&ct=g">

---

### Terminology Used

**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.

---

### References 
1. https://en.wikipedia.org/wiki/Kubernetes 
2. https://www.docker.com/resources/what-container
3. https://kubernetes.io/docs/concepts/configuration/configmap/
4. https://kind.sigs.k8s.io/docs/user/quick-start/
5. https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#mounted-configmaps-are-updated-automatically
6. https://kubernetes.io/docs/concepts/configuration/configmap/#mounted-configmaps-are-updated-automatically
 
