DEV Community

Cover image for Kubernetes ConfigMaps and Secrets: A Developer's Guide
bharatrajtj
bharatrajtj

Posted on

Kubernetes ConfigMaps and Secrets: A Developer's Guide

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
Apply the configMap and check whether it is deployed.

Image description

Update the deployment file to add ENV

Image description

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

Image description

Now, change the data value in ConfigMap

Image description

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

Image description

Top comments (0)