d
WE ARE EXPERTS IN TECHNOLOGY

Let’s Work Together

n

StatusNeo

Securing Your Kubernetes Cluster: The Role of ConfigMaps and Secrets

In Kubernetes, managing configuration data and sensitive information securely is critical for application management. Two key resources in Kubernetes that help us manage such data are ConfigMaps and Secrets. These resources allow you to separate your configuration from your application code, making your deployment more flexible and secure.

Why Do We Need ConfigMaps and Secrets?

When deploying applications in Kubernetes, sometimes you need to update configuration details or manage sensitive information such as API keys, passwords, and certificates. Traditionally, if we wanted to update a configuration, we would have to manually open the terminal of the POD, make changes to the configuration files, and restart the pods. This approach is not ideal, as it’s manual, error-prone, and inefficient.

Instead of directly modifying the configuration inside the pod, Kubernetes offers a better way using ConfigMaps and Secrets. These resources allow you to store your configurations and secrets separately from your code, making it easier to update and manage them.

ConfigMaps

A ConfigMap is a Kubernetes resource used to store configuration data in the form of key-value pairs. You can use ConfigMaps to inject as environment variables into your pods or mount configuration files directly into the pod’s filesystem. This way, your application can consume configuration settings without needing to rebuild the Docker image or modify the pod directly.

For example, you might have a configuration file or key-value pairs like:

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
DATABASE_URL: "mysql://localhost:3306"
API_KEY: "12345abcde"
This ConfigMap holds some configuration data like the database URL and an API key.

Secrets

Secrets are similar to ConfigMaps but are intended for storing sensitive information, such as passwords, tokens, or private keys. While ConfigMaps store data in plaintext, Secrets are base64-encoded by default and can be optionally encrypted when stored in the Kubernetes backend (etcd). Kubernetes also allows you to provide your own encryption algorithm for Secrets if needed for additional security.

For example, a secret could look like this:

secret.yaml

apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
data:
password: cGFzc3dvcmQ= # base64 encoded value for 'password'

In the above example, the password is encoded using base64. When this secret is accessed by the pod, Kubernetes will decode it into its original value.

ConfigMaps vs Secrets

  • ConfigMaps are used to store general configuration data that is not sensitive. These are stored as plaintext and can be injected as environment variables or mounted as files inside a pod
  • Secrets, on the other hand, are used to store sensitive data such as passwords, API tokens, and certificates. They are encoded (usually base64) and can be encrypted for extra security.

Important Points to Remember:

  • Both ConfigMaps and Secrets are stored in the etcd database within Kubernetes, but Secrets are encrypted by default.
  • Secrets are designed for sensitive information, whereas ConfigMaps are designed for configuration data.

Updating ConfigMaps and Secrets Dynamically

One common question is: “When the data in a ConfigMap or Secret is updated, does the change reflect in the pod immediately?”The answer is: No.

Kubernetes does not automatically update the environment variables or mount points in a running pod when the data in a ConfigMap or Secret changes. To make use of the updated values, you would need to restart the pod.

However, there is a better solution to this:   Volume Mounts.

How to Use Volume Mounts to Dynamically Update ConfigMaps

To solve the issue of needing to manually restart pods after updating a ConfigMap or Secret, Kubernetes provides the option to use volume mounts. With this approach, the ConfigMap or Secret is mounted as a volume inside the pod, and Kubernetes will automatically update the content of the volume when the data in the ConfigMap or Secret is changed.

Example of Using Volume Mounts with ConfigMap:

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
 replicas: 1
selector:
   matchLabels:
     app: my-app
template:
  metadata:
    labels:
  app: my-app
spec:
containers:
 – name: my-app-container
     image: my-app-image
volumeMounts:
 – mountPath: /etc/config  # Mounts the ConfigMap content inside the pod at /etc/config
      name: config-volume # The volume reference
volumes:
    – name: config-volume
configMap:
      name: my-config # The ConfigMap to mount

The ConfigMap my-config is mounted as a volume into the pod at /etc/config. When the ConfigMap or Secret is updated, Kubernetes will automatically update the mounted volume.

Conclusion

Kubernetes ConfigMaps and Secrets are powerful tools for managing configuration data and sensitive information securely and flexibly. By separating configuration from your application code, you can easily update configurations without needing to rebuild Docker images or restart pods manually.