Kubernetes ConfigMaps and Secrets: A Developer's Guide
Tags: kubernetes, orchestration, configmap, pods
Published: 2023-11-19 18:39:49

Kubernetes, with its powerful orchestration capabilities, relies on resources like ConfigMaps and Secrets to streamline the management of configuration data within a cluster. Let's delve into these essential components and explore their nuances.

**ConfigMaps: Unlocking Configurations**
ConfigMaps serve as the go-to solution for storing configuration details, be it key-value pairs or entire files. Once deployed in a Kubernetes cluster, ConfigMaps can be effortlessly mounted into any pod, allowing pods to retrieve crucial information stored as environment variables or files. This flexibility empowers developers to access and utilize configuration data as needed, without the hassle of pod restarts.

**Secrets: Safeguarding Sensitive Data**
In the realm of sensitive information, Secrets take the spotlight. The impetus behind Secrets is rooted in the realization that data saved in ConfigMaps is stored in etcd as an object. This raises concerns about security, especially in the face of potential hacker exploits gaining access to etcd.

To fortify against such threats, Kubernetes encrypts the information entered into Secrets at rest. This encryption occurs before the data is transported to etcd, providing an additional layer of security. Kubernetes even allows users to implement custom encryption for heightened protection. Consequently, even if a hacker breaches etcd, accessing Secrets becomes a formidable challenge without the requisite decryption keys.

**Best Practices for Secrets Implementation**
Implementing Secrets necessitates a thoughtful approach to security. Strong Role-Based Access Control (RBAC) should be a cornerstone of your strategy. Not every user should be granted access to Secrets resources, ensuring that sensitive information remains tightly guarded.

**Overcoming ConfigMap Limitations**
One notable limitation of ConfigMaps is the inability to update or change environment variables once they are loaded into a container. To circumvent this constraint, volume mounts come to the rescue. By saving ConfigMap data as files instead of environment variables, developers gain the flexibility to modify configurations dynamically, enhancing the adaptability of Kubernetes deployments.

**Hands-on**
This is a simple ConfigMap file with db-port as the Key and 8000 as the value.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vsqwllb7b2s5vs4spcs.png)
Apply the configMap and check whether it is deployed.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lscpfr35u9kzrfsglb1u.png)


Update the deployment file to add ENV

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t0f64v2ppjg0o253pjcm.png)

Get the pod name and exec into a pod, search for ENV 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7y59adepxlzph3k633wm.png)

Now, change the data value in ConfigMap

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gydgizdapca37vvfjgca.png)

This change will not be reflected inside the pods as env will not be updated. So, we create a volume and mount this volume to the pod

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmq6e1r8avqxgaocp4pl.png)




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5br4604mmogx84fypzma.png)