peter (admin)   -  



 

ConfigMaps vs Secrets

Let's start a new section where we will learn how to manage application settings, protect important data, and control access inside Kubernetes.

We will also learn how applications use configuration and secrets, how different users and pods get limited or full access, and how everything is controlled step by step.

We will test what works and what fails so you can clearly see how permissions behave in real scenarios.

By the end of this section, you will understand how to run applications safely, protect data, and control access using real commands that you can easily apply in your own environment.

The first lecture is ConfigMaps versus Secrets, where we will learn how to manage application settings and protect sensitive data in Kubernetes.

What Is a ConfigMap?

A ConfigMap in Kubernetes is used to store simple configuration data that your application needs to run, such as environment variables, settings, or file content.

This data is not sensitive, which means it should not include passwords or private information.

Environment variables are simple key-value settings that are passed to a container so the application can use them while running. They help control application behavior without changing the code.

For example, we can define:

APP_NAME=myapp

Inside the container, the application can read this value and use it while running.

The main purpose of a ConfigMap is to separate configuration from the application code, so you do not need to change your application every time a setting changes.

In Kubernetes, the role of a ConfigMap is to provide configuration data to pods in an easy and flexible way, either as environment variables or as files inside the container.

For example, if your application needs a setting such as application mode, either production or debug, you can store it in a ConfigMap and use it inside the container without changing the code.

What Is a Secret?

A Secret in Kubernetes is used to store sensitive information such as passwords, API keys, tokens, or private data that must be protected.

The purpose of Secrets is to keep this important data separate from application configuration files and code.

In Kubernetes, Secrets can be used to provide sensitive data to pods when needed. They can also be used as environment variables or mounted as files inside the container.

For example, if your application needs a database password, you can store it in a Secret and use it inside the pod without exposing it directly in the application code.

Create the Namespace

Let's understand this concept practically.

For this, we are already logged into both the master node and the worker node.

To verify the cluster, run:

kubectl get nodes

Both the master and worker should be in the Ready state. This means the cluster is up and both nodes are properly connected and working.

Now create a namespace named deep:

kubectl create namespace deep

This allows us to manage ConfigMaps and Secrets within a specific namespace without mixing them with other resources in the cluster.

Create the ConfigMap

Next, create a ConfigMap named app-config where we store environment variables for our application.

We define:

APP_NAME=myapp
APP_ENV=production

APP_NAME is an environment variable with the value myapp, so the application can read and use this name while running.

APP_ENV is another environment variable with the value production, so the application knows that it is running in a production environment.

These environment variables help control application behavior without changing the code. Both values are stored inside the ConfigMap so they can be used easily inside a pod.

Create the Secret

After successfully creating the ConfigMap, create a Secret named app-secret where we store sensitive environment variables for the application.

For example:

DB_USER=admin
DB_PASS=password123

DB_USER stores the database username, while DB_PASS stores the database password.

These values are sensitive, so they are stored in a Secret instead of a ConfigMap.

Verify the ConfigMap and Secret

First, verify the ConfigMap:

kubectl get configmap

The app-config ConfigMap should be created successfully and contain two data entries. This confirms that our configuration values are stored and ready to be used by a pod.

Next, verify the Secret:

kubectl get secret

The app-secret Secret should be created successfully. Its type is Opaque, which is a generic Secret type used to store key-value data.

The data count should be 2, confirming that it contains the two sensitive values we added earlier.

Create a Pod Using the ConfigMap and Secret

Now create a YAML file named config-secret-pod.yaml.

We want to create a pod that can use both the ConfigMap and the Secret so we can pass configuration data and sensitive data into the container.

The pod configuration can be defined as follows:

apiVersion: v1
kind: Pod
metadata:
  name: config-secret-demo
spec:
  containers:
    - name: nginx
      image: nginx
      env:
        - name: APP_NAME
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: APP_NAME
        - name: DB_USER
          valueFrom:
            secretKeyRef:
              name: app-secret
              key: DB_USER

The apiVersion tells Kubernetes which API version is being used, while kind defines that we are creating a pod.

Inside metadata, we give the pod the name config-secret-demo so Kubernetes can identify it.

In the spec section, we define the container. We give it a name and use the NGINX image so a web server can run.

The env section defines environment variables that will be passed into the container.

First, we define APP_NAME, which comes from the ConfigMap. We reference the ConfigMap named app-config and select the APP_NAME key.

This value is taken from the ConfigMap and used inside the container.

Next, we define DB_USER, which comes from the Secret. We reference the Secret named app-secret and select the DB_USER key.

This value is taken from the Secret and used inside the container.

The pod is therefore using both the ConfigMap and Secret together. Normal configuration comes from the ConfigMap, while sensitive data comes from the Secret. Both are passed as environment variables inside the container.

Save the file and exit.

Apply the Pod Configuration

Apply the YAML file so Kubernetes can create the pod and pass both the ConfigMap and Secret values into the container as environment variables:

kubectl apply -f config-secret-pod.yaml

Verify the pod:

kubectl get pods

The config-secret-demo pod should be in the Running state. This means the container is running successfully and both the ConfigMap and Secret have been applied correctly.

Verify the Environment Variables

Go inside the pod so we can check whether the environment variables are working correctly:

kubectl exec -it config-secret-demo -- /bin/bash

Once inside the container, use the environment listing utility to check the application name:

env | grep APP_NAME

The output should show:

APP_NAME=myapp

This confirms that the value is coming from the ConfigMap.

Now check the database variable:

env | grep DB_USER

The output should show:

DB_USER=admin

This confirms that the value is coming from the Secret.

Both the ConfigMap and Secret are therefore properly connected to the pod, and their values are available inside the container as environment variables.

The application can now use these values while running without hard-coding them.

This makes the setup clean and secure because normal configuration is taken from the ConfigMap and sensitive data is taken from the Secret.

Exit from the container:

exit

View Secret Data

If you want to see the Secret in detail, you can retrieve it in YAML format:

kubectl get secret app-secret -o yaml

The data values are not shown in plain text. Instead, they are shown in an encoded form.

This means Kubernetes stores the Secret values in an encoded format rather than displaying them directly.

However, Base64 encoding is not encryption. Anyone with sufficient access to the Secret can decode the values and read the original data.

Decode a Secret Value

If you want to decode an encoded value, you can use a decoding utility.

For example:

echo 'cGFzc3dvcmQxMjM=' | base64 --decode

The decoded value is:

password123

This demonstrates that Secrets are not encrypted by default simply because they are Base64 encoded. They are encoded, and anyone with access to the encoded value can decode it.

Summary

This lecture demonstrated how to manage application settings and protect sensitive data in Kubernetes.

ConfigMaps are used for normal application configuration, such as environment settings, while Secrets are used for sensitive information such as database credentials.

We created a ConfigMap and a Secret, used both in a pod, and verified that their values were available inside the container as environment variables.

We also examined how Secret values are represented in YAML and demonstrated that Base64 encoding does not provide encryption by itself.

 


Comments (0)

There are no comments yet. Be the first to comment.

Log in or register to leave a comment. all comments are subject to admin approval