This lesson explores configuration changes in Kubernetes and how to troubleshoot issues with environment variable updates in running pods.
In this lesson, we explore a common scenario encountered in Kubernetes where configuration changes (such as updates to ConfigMaps or Secrets) are not immediately reflected in running pods. This guide walks you through inspecting deployments, testing environment configurations, and troubleshooting issues related to environment variable updates.
Focus on the environment configuration where the web application receives its environment variables from a ConfigMap named web-message. To inspect this ConfigMap, retrieve its details by running:
Next, let’s investigate the issue with the two-tier application, which is not becoming ready due to a failing readiness probe. The probe uses the following command to verify connectivity to the MySQL server:
To diagnose the problem, execute the following command to inspect the environment variables within the two-tier app pod:
Copy
Ask AI
k exec -n production -it two-tier-app-7b7798b66b-wzb4n -- /bin/sh# printenv | grep MYSQLMYSQL_PORT_3306_TCP_ADDR=10.105.100.67MYSQL_PASSWORD=adminMYSQL_PORT_3306_TCP_PORT=3306MYSQL_HOST=mysqlMYSQL_SERVICE_HOST=10.105.100.67MYSQL_PORT_3306_TCP_PROTO=tcpMYSQL_USER=rootMYSQL_PORT_3306_TCP=10.105.100.67:3306MYSQL_SERVICE_PORT=3306MYSQL_DB=mydb
Here, notice that the MYSQL_HOST variable is set to “mysql”. This value is provided by a secret named app-secrets. To inspect the secret, run:
Copy
Ask AI
k get secret -n production app-secrets -o yaml
Within the secret, the problematic field is encoded in base64. Verify the correct hostname by running:
Copy
Ask AI
echo "bXlzcHcw" | base64 -d
This command outputs:
Copy
Ask AI
mysql
Even after correcting the secret, the pod might still use the old value because it was not restarted. To refresh the environment variables, restart the two-tier application deployment:
Copy
Ask AI
k rollout restart deployment -n production two-tier-app
Once restarted, monitor the pod’s status and events to ensure it becomes ready and successfully connects to the MySQL server.
These examples illustrate that updating a ConfigMap or Secret does not automatically refresh the environment variables in running pods. Kubernetes requires a pod or deployment restart for changes to take effect. For large-scale environments with many pods, manually tracking which resources need a restart can be challenging.
Whenever you modify an external ConfigMap or Secret that is mounted onto pods, ensure that you restart the affected pods or deployments. This step is essential for the changes to be applied to the running configuration.
In an upcoming lesson, we will discuss strategies to manage these challenges more efficiently in your Kubernetes clusters.For more detailed Kubernetes concepts and best practices, check these resources: