Skip to content

Commit b599f4d

Browse files
committed
Blog: k8s configmaps
Signed-off-by: Lee Calcote <lee.calcote@layer5.io>
1 parent 944056f commit b599f4d

2 files changed

Lines changed: 154 additions & 0 deletions

File tree

71.6 KB
Loading
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
---
2+
title: "How can you tell what kind of Kubernetes Configmap you have?"
3+
subtitle: "Understanding when changes are reloaded"
4+
date: 2025-11-03 10:30:05 -0530
5+
author: Lee Calcote
6+
thumbnail: ./image.webp
7+
darkthumbnail: ./image.webp
8+
category: "Kubernetes"
9+
tags:
10+
- devops
11+
- kubernetes
12+
type: Blog
13+
resource: true
14+
published: true
15+
---
16+
17+
import { BlogWrapper } from "../../Blog.style.js";
18+
import { Link } from "gatsby";
19+
import image from "./image.webp";
20+
21+
<BlogWrapper>
22+
23+
A common point of confusion when working with Kubernetes is understanding how `ConfigMap` updates are handled. You’ve pushed a change to your ConfigMap, but your application isn't seeing the new values. What's going on?
24+
25+
The answer depends entirely on **how your application consumes the ConfigMap**. There isn't a "type" of ConfigMap object itself, but rather two distinct *methods of consumption* by a Pod, and each has drastically different behavior regarding updates.
26+
27+
To tell what "kind" you have, you need to look at your Pod or Deployment's YAML definition.
28+
29+
-----
30+
31+
## 🧐 How to Check Your Pod's ConfigMap Consumption
32+
33+
You can find out how a Pod is using a ConfigMap by inspecting its YAML definition. Run this command to get the running YAML for a specific pod:
34+
35+
```bash
36+
kubectl get pod <your-pod-name> -o yaml
37+
```
38+
39+
Now, look for two key sections in the `spec.containers` list:
40+
41+
1. **Environment Variables:** Look for `env` or `envFrom`.
42+
2. **Mounted Volumes:** Look for `volumeMounts` and the corresponding `volumes` section at the Pod spec level.
43+
44+
Let's break down what each one means for reloading.
45+
46+
-----
47+
48+
## 1\. Consumed as Environment Variables
49+
50+
This is when your Pod's YAML injects ConfigMap data directly as environment variables for the container.
51+
52+
### How to Identify It
53+
54+
In your Pod spec, you'll see blocks like this:
55+
56+
```yaml
57+
# ...
58+
spec:
59+
containers:
60+
- name: my-app-container
61+
image: my-app
62+
env: # <-- Look here
63+
- name: MY_CONFIG_KEY
64+
valueFrom: # <-- Or here
65+
configMapKeyRef:
66+
name: my-special-config
67+
key: some.config.key
68+
envFrom: # <-- Or here
69+
- configMapRef:
70+
name: my-special-config
71+
# ...
72+
```
73+
74+
If you see `env` or `envFrom` pointing to a `configMapKeyRef` or `configMapRef`, your application is consuming the ConfigMap as environment variables.
75+
76+
### Reload Behavior: 🛑 No Hot-Reload
77+
78+
This is the most critical difference: **Changes to a ConfigMap are NOT reflected in running Pods that use them as environment variables.**
79+
80+
Environment variables are set by the container runtime *only when the container is created*. They are immutable for the life of that running process.
81+
82+
**How to Apply Changes:** To make the application see the new ConfigMap values, you **must restart the Pod**. The simplest way to do this for a `Deployment` is with a rolling restart:
83+
84+
```bash
85+
kubectl rollout restart deployment <your-deployment-name>
86+
```
87+
88+
When the new Pods are created, they will read the *updated* ConfigMap data and set the new environment variables.
89+
90+
-----
91+
92+
## 2. Consumed as a Mounted Volume
93+
94+
This method mounts your ConfigMap as one or more files inside your Pod's filesystem. Your application is programmed to read its configuration from these files (e.g., `/app/config/settings.properties`).
95+
96+
### How to Identify It
97+
98+
You'll see two corresponding sections in your Pod spec:
99+
100+
1. `spec.containers.volumeMounts`: This tells the container where to mount the volume.
101+
2. `spec.volumes`: This defines the volume itself and links it to the ConfigMap.
102+
103+
104+
```yaml
105+
# ...
106+
spec:
107+
containers:
108+
- name: my-app-container
109+
image: my-app
110+
volumeMounts: # <-- Look here
111+
- name: config-volume
112+
mountPath: /etc/config
113+
volumes: # <-- And here
114+
- name: config-volume
115+
configMap:
116+
name: my-special-config
117+
# ...
118+
```
119+
120+
If you see this `volumes` and `volumeMounts` pairing, your application is consuming the ConfigMap as files.
121+
122+
### Reload Behavior: ✅ Automatic... With a Catch
123+
124+
This method **does support hot-reloading**, but with two important caveats:
125+
126+
1. **There is a delay.** When you update the ConfigMap object, the `kubelet` on the node is responsible for updating the mounted files. This is not instantaneous. It relies on a periodic sync cycle, and the total delay can be **60 to 90 seconds (or even longer)** before the files at `mountPath` are actually updated.
127+
128+
2. **Your application must support it.** Kubernetes *only* updates the files on disk. It does **not** send a signal (like `SIGHUP`) to the process or restart the container. Your application must be built to:
129+
130+
* Watch the configuration files for changes (using a library like `fsnotify`).
131+
* Periodically re-read the configuration files on its own timer.
132+
133+
If your application only reads its config files on startup, it will behave just like the environment variable method: **it will not see the changes until it is restarted.**
134+
135+
-----
136+
137+
## Summary: ConfigMap Reload Behavior
138+
139+
Here’s a simple table to remember the differences:
140+
141+
| Consumption Method | How to Identify in Pod YAML | Are Changes Updated in Running Pod? | How Are Changes Seen? |
142+
| :--- | :--- | :--- | :--- |
143+
| **Environment Variables** | `spec.containers.env` <br> `spec.containers.envFrom` | **No**| Pod must be **restarted**. |
144+
| **Mounted Volume** | `spec.containers.volumeMounts` <br> `spec.volumes` | **Yes** ✅ (with delay) | Kubelet updates files. **Application must be coded** to reload the updated file. |
145+
146+
### What If I Need Automatic Restarts?
147+
148+
If you are using the volume mount method but your application doesn't support live reloading, you can use a "reloader" tool. A popular open-source controller like [**Stakater's Reloader**](https://github.com/stakater/Reloader) can watch for ConfigMap changes and automatically trigger a rolling restart of any Deployment that uses it. This gives you the best of both worlds: configuration in files and automatic updates for apps that can't reload on their own.
149+
150+
-----
151+
152+
This [Kubernetes ConfigMap Tutorial video](https://www.youtube.com/watch?v=P7mHN_PUz_w) provides a great visual walkthrough of how to create and use ConfigMaps, including injecting them as both volumes and environment variables.
153+
154+
</BlogWrapper>

0 commit comments

Comments
 (0)