peter (admin)   -  



 

Environment Variables vs Mounted Configuration

Here we will learn how to use environment variables and mounted configuration files to pass settings into a container.

Mounted configuration in Kubernetes means we take data from a ConfigMap or Secret and attach it inside the container as a file instead of passing it as an environment variable.

The purpose of this is to provide configuration data in file form, which many applications need to read from a file instead of variables.

In Kubernetes, this is useful when applications expect configuration such as text files, JSON files, or settings files.

For example, we can mount a ConfigMap into a path such as /config. Inside that directory, the application will see files containing the configuration data.

Environment Variables and Mounted Configuration

Both environment variables and mounted configuration are ways to pass data into a container, but they work differently.

Environment variables pass small key-value data directly into the container, while mounted configuration provides full files inside the container.

We use environment variables for simple settings and mounted files for larger or structured data.

Both methods can use the same source, such as a ConfigMap or Secret, but one is read as variables and the other is read as files.

In simple terms, environment variables are used for quick settings, while mounted configuration is used when the application needs actual files to read.

Create the Namespace and ConfigMap

Let's move on and 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, which means our cluster is up and both nodes are properly connected and working.

Now create a namespace named deep:

kubectl create namespace deep

This helps us manage our ConfigMaps, Secrets, and mounted configuration files in one place, making them easier to organize and use inside pods without mixing them with other resources in the cluster.

Next, create a ConfigMap named app-config inside the deep namespace. We define configuration values such as:

APP_COLOR=blue
APP_MODE=production

These values are simple examples showing how configuration is passed into a container.

blue can represent something such as a UI color or display setting, while production shows that the application is running in a real environment instead of a testing environment.

This helps us understand how applications can read and use configuration without changing the code.

Create the Pod Configuration

Let's create a YAML file named env-vs-mount.yaml.

The purpose of this file is to create a pod where we can test both methods of passing configuration:

  • Environment variables

  • Mounted configuration files

This allows us to see how the same ConfigMap data can be used in different ways inside a container.

The YAML configuration is:

apiVersion: v1
kind: Pod
metadata:
  name: env-vs-mount
  namespace: deep
spec:
  containers:
    - name: nginx
      image: nginx
      env:
        - name: APP_COLOR
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: APP_COLOR
      volumeMounts:
        - name: config-volume
          mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: app-config

The apiVersion tells Kubernetes which API we are using, while kind defines that we are creating a pod.

Inside metadata, we give the pod the name env-vs-mount and set the namespace to deep.

This means the pod runs in the same namespace where our ConfigMap exists.

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

Configure the Environment Variable

We define an environment variable named APP_COLOR, which comes from the ConfigMap.

We reference the ConfigMap named app-config and select the APP_COLOR key.

This value is passed into the container as an environment variable.

Mount the Configuration Files

Next, we define the volume mount.

We attach a volume named config-volume to the path:

/etc/config

This means the configuration data will appear as files inside this directory.

In the volumes section, we define that this volume comes from the ConfigMap app-config.

All the keys inside the ConfigMap will therefore be available as files inside /etc/config.

This setup demonstrates both methods together. The same configuration is passed as an environment variable and also mounted as files inside the container.

Save the file and exit.

Apply the Pod Configuration

Apply the file so Kubernetes can create the pod:

kubectl apply -f env-vs-mount.yaml

After successfully creating the pod, verify it:

kubectl get pods -n deep

The env-vs-mount pod should be in the Running state, which means the container is running properly and both the environment variable and mounted configuration have been applied successfully.

Test the Environment Variable

Go inside the pod so we can test both methods of passing configuration:

kubectl exec -it env-vs-mount -n deep -- /bin/bash

Once inside the container, check the environment variable:

env | grep APP_COLOR

You should see:

APP_COLOR=blue

This means the value is coming directly from the ConfigMap as an environment variable.

This demonstrates how simple key-value settings can be passed into an application without changing the code.

Test the Mounted Configuration

Next, check the mounted configuration path:

ls /etc/config

You should see files such as:

APP_COLOR
APP_MODE

This means the same ConfigMap data is also available as files inside the container.

This is different from environment variables because the data is stored as files that the application can read.

To view the application mode, read the file:

cat /etc/config/APP_MODE

The output should be:

production

This confirms that the data has been correctly mounted from the ConfigMap.

Now read the application color:

cat /etc/config/APP_COLOR

The output should be:

blue

This again confirms that the same configuration is available as a file.

Summary

This demonstrates the difference between the two methods.

Environment variables provide quick access to simple values, while mounted configuration provides full files inside the container.

Both methods can use the same ConfigMap, but they are used in different ways depending on how the application needs to access the configuration data.

 


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