0
0
Kubernetesdevops~5 mins

Using ConfigMaps as environment variables in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your applications need settings that can change without rebuilding the app. ConfigMaps let you store these settings separately and use them as environment variables inside your containers.
When you want to change app settings like API URLs without changing the app code.
When multiple pods need the same configuration values and you want to manage them in one place.
When you want to keep sensitive or environment-specific data outside your container images.
When you want to update configuration without restarting or rebuilding your containers manually.
When you want to separate configuration from code for easier maintenance and updates.
Config File - configmap-env.yaml
configmap-env.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-app-config
data:
  APP_MODE: production
  LOG_LEVEL: info
---
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: nginx:1.23.3
        envFrom:
        - configMapRef:
            name: my-app-config

This file has two parts separated by ---:

  • ConfigMap: Stores key-value pairs like APP_MODE and LOG_LEVEL.
  • Deployment: Runs a pod with one container using the nginx image.
  • The envFrom section tells Kubernetes to load all keys from the ConfigMap as environment variables inside the container.
Commands
This command creates the ConfigMap and the Deployment in your Kubernetes cluster. It sets up the environment variables for the app container.
Terminal
kubectl apply -f configmap-env.yaml
Expected OutputExpected
configmap/my-app-config created deployment.apps/my-app created
Check that the pod from the Deployment is running and ready.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE my-app-6d4b7f7d7f-abcde 1/1 Running 0 10s
Inside the running pod, print the value of the APP_MODE environment variable to verify it was set from the ConfigMap.
Terminal
kubectl exec my-app-6d4b7f7d7f-abcde -- printenv APP_MODE
Expected OutputExpected
production
Check the LOG_LEVEL environment variable inside the pod to confirm it matches the ConfigMap value.
Terminal
kubectl exec my-app-6d4b7f7d7f-abcde -- printenv LOG_LEVEL
Expected OutputExpected
info
Key Concept

If you remember nothing else from this pattern, remember: ConfigMaps let you inject configuration data as environment variables into your containers without changing the container image.

Common Mistakes
Not referencing the ConfigMap name correctly in the Deployment's envFrom section.
The container won't receive the environment variables if the ConfigMap name is wrong or missing.
Ensure the configMapRef name exactly matches the ConfigMap metadata name.
Trying to use env instead of envFrom for many variables, writing each key manually.
This is error-prone and harder to maintain when you have many variables.
Use envFrom with configMapRef to load all keys at once.
Changing the ConfigMap after pod creation without restarting the pod.
Pods do not automatically reload environment variables from updated ConfigMaps.
After updating the ConfigMap, restart the pod to apply new environment variables.
Summary
Create a ConfigMap with key-value pairs for your app settings.
Reference the ConfigMap in your Deployment using envFrom to load all keys as environment variables.
Verify the pod is running and check environment variables inside the container with kubectl exec.