How to Update an Immutable ConfigMap in Kubernetes
The DevOps team is running a production web application in the default namespace. The web-app Deployment uses a ConfigMap named app-config to provide environment variables to its Pods.
The development team needs to change the application's LOG_LEVEL from info to debug. However, when you try to update the existing configuration, you discover the ConfigMap cannot be modified because it is immutable.
Deployment manifest:
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
namespace: default
immutable: true
data:
APP_MODE: "production"
LOG_LEVEL: "info"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- name: web-app
image: nginx:latest
envFrom:
- configMapRef:
name: app-config
ports:
- containerPort: 80Your task is to troubleshoot the configuration and apply the required change without causing unnecessary application downtime.
The final state must have:
- The
web-appDeployment running 2 replicas. APP_MODEset toproduction.LOG_LEVELset todebug.- The Deployment using the updated configuration.
- The configuration available as environment variables in newly created Pods.
- The Kubernetes Deployment rollout completed successfully.
- The application remains available during the configuration update.
Verify the updated configuration from the newly created Pods and confirm that they contain LOG_LEVEL=debug
ConfigMaprefer to the official Kubernetes documentation.Create the ConfigMap and Deployment
Apply the initial manifest using the following command. The expected output confirms that the ConfigMap and Deployment were created successfully.
kubectl apply -f web-app.yamlExpected output:
configmap/app-config created
deployment.apps/web-app createdVerify the Deployment
Check the Deployment with the following command
$ kubectl get deployment web-app
NAME READY UP-TO-DATE AVAILABLE AGE
web-app 2/2 2 2 16sCheck the Deployment to confirm that both replicas are ready. You should see READY 2/2, with 2 replicas up-to-date and available, and two running Pods created by the Deployment. You should see two running Pods.
Inspect the ConfigMap
List the ConfigMaps with the following command
$ kubectl get configmaps
NAME DATA AGE
app-config 2 3m11s
kube-root-ca.crt 1 3d22hInspect the app-config ConfigMap to verify that it is immutable and contains the expected configuration values.
You should see immutable: true, with APP_MODE set to production and LOG_LEVEL set to info.
Identify the Problem
The key configuration is immutable: true, which means the ConfigMap cannot be modified after creation. Therefore, changing LOG_LEVEL from info to debug on the existing app-config ConfigMap will fail.
Create the New ConfigMap
Create a new file named app-config-v2.yaml. This file will be used to define the updated ConfigMap with the required configuration changes.
Apply the new ConfigMap manifest to create app-config-v2. The expected output confirms that the new ConfigMap was created successfully.
$ kubectl apply -f app-config-v2.yaml
configmap/app-config-v2 createdUpdate the Deployment
Edit the web-app Deployment and update the ConfigMap reference from app-config to app-config-v2.
kubectl edit deployment web-appFind:
envFrom:
- configMapRef:
name: app-configChange it to:
envFrom:
- configMapRef:
name: app-config-v2Save and exit the editor to apply the change.
Alternative: Update the Deployment with kubectl set env
You can also update the ConfigMap reference without manually editing the entire Deployment:
kubectl set env deployment/web-app --from=configmap/app-config-v2
This is a useful CKA shortcut when the ConfigMap values need to be exposed as environment variables.
However, for this exercise, understanding and modifying the existing envFrom.configMapRef is important because the task specifically tests ConfigMap references.
Check the Rollout
Changing the Pod template triggers a new Deployment rollout.
Check the rollout status to confirm that the update completed successfully,
$ kubectl rollout status deployment/web-app
deployment "web-app" successfully rolled outThe rollout should complete successfully, with the Deployment showing READY 2/2, UP-TO-DATE 2, and AVAILABLE 2.
$ kubectl get deployment web-app
NAME READY UP-TO-DATE AVAILABLE AGE
web-app 2/2 2 2 32mVerify LOG_LEVEL
Select one of the newly created Pods and inspect its environment variables. Verify that LOG_LEVEL=debug and APP_MODE=production are present, confirming that the Pod is using the updated ConfigMap.
Select one of the new Pods:
kubectl get podsThen check its environment variables:
$ kubectl exec web-app-76dbf6bb64-bzk4v -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
.
.
APP_MODE=production
LOG_LEVEL=debug
.
.Configuration Flow
The Deployment does not copy the ConfigMap into the Pod filesystem. Instead, envFrom references the ConfigMap and exposes its keys as environment variables inside the container.
For example, LOG_LEVEL=debug from app-config-v2 becomes the LOG_LEVEL environment variable in the newly created Pods.
Why Does the Deployment Roll Out?
The Deployment initially references app-config through configMapRef. When it is changed to app-config-v2, the Deployment's Pod template is modified.
Since the Pod template has changed, the Deployment creates a new ReplicaSet and gradually replaces the old Pods with new ones using the updated configuration.
Conceptually, the Deployment starts with an old ReplicaSet whose Pods reference app-config.
When the Deployment is updated to use app-config-v2, it creates a new ReplicaSet with Pods that reference the updated ConfigMap.
The Deployment then gradually replaces the old Pods with the new Pods during the rolling update.
Why Is This Minimal Downtime?
Deployments use the RollingUpdate strategy by default. Instead of deleting all existing Pods at once, Kubernetes gradually replaces the old Pods with new ones, allowing the application to remain available during the update.
You can verify the strategy with kubectl get deployment web-app -o jsonpath='{.spec.strategy.type}', which should return RollingUpdate.
To monitor the rollout, use kubectl rollout status deployment/web-app. This allows the configuration change to be introduced without taking all replicas down simultaneously.
Important ConfigMap Concept
There are two important scenarios to remember.
Mutable ConfigMap
If the ConfigMap is not immutable, such as when immutable: false is specified or immutable is not specified, its data can be updated.
However, when the ConfigMap is consumed as environment variables, updating it does not automatically update the environment variables of already-running Pods. The Pods must be recreated or rolled out to receive the updated values.
Immutable ConfigMap
When immutable: true is specified, you cannot change the ConfigMap data after creation.
The solution is to create a new ConfigMap, such asapp-config-v2, and update the workload to reference the new ConfigMap.
CKA Exam Tips
These tips will help you quickly identify the important Kubernetes concepts and avoid common mistakes during the exam.
Tip 1: Check immutable
When a ConfigMap update fails, inspect the ConfigMap using:
kubectl get configmap <name> -o yaml
Look for immutable: true. If the ConfigMap is immutable, its data cannot be modified.
Tip 2: Create a New ConfigMap
For an immutable ConfigMap, the existing configuration cannot be modified. Instead, create a new ConfigMap, such as app-config-v2, and update the workload to use the new ConfigMap.