0
0
KubernetesHow-ToBeginner · 4 min read

How to Use ConfigMap as Environment Variable in Kubernetes

In Kubernetes, you can use a ConfigMap as environment variables by referencing it in the pod's envFrom or env section. This injects the key-value pairs from the ConfigMap directly into your container's environment variables.
📐

Syntax

To use a ConfigMap as environment variables in a pod, you add the envFrom field under the container spec. This field points to the ConfigMap by name and imports all its keys as environment variables.

Alternatively, you can use env with valueFrom.configMapKeyRef to import specific keys.

  • envFrom: Imports all keys from the ConfigMap as environment variables.
  • env: Imports specific keys as environment variables.
yaml
containers:
  - name: my-container
    image: my-image
    envFrom:
      - configMapRef:
          name: my-configmap

# OR for specific keys
containers:
  - name: my-container
    image: my-image
    env:
      - name: MY_VAR
        valueFrom:
          configMapKeyRef:
            name: my-configmap
            key: my-key
💻

Example

This example shows a ConfigMap with two keys and a pod that uses envFrom to load them as environment variables.

yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-config
data:
  APP_MODE: production
  LOG_LEVEL: info
---
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
    - name: example-container
      image: busybox
      command: ["sh", "-c", "echo APP_MODE=$APP_MODE; echo LOG_LEVEL=$LOG_LEVEL; sleep 3600"]
      envFrom:
        - configMapRef:
            name: example-config
Output
APP_MODE=production LOG_LEVEL=info
⚠️

Common Pitfalls

  • Not creating the ConfigMap before the pod tries to use it causes pod startup failure.
  • Using envFrom imports all keys, which may unintentionally expose sensitive data.
  • Referencing a non-existent key with configMapKeyRef causes the pod to fail.
  • Changes to ConfigMap do not update environment variables in running pods automatically; pods must be restarted.
yaml
containers:
  - name: bad-container
    image: busybox
    env:
      - name: MISSING_VAR
        valueFrom:
          configMapKeyRef:
            name: example-config
            key: missing-key  # This key does not exist

# Correct way:
containers:
  - name: good-container
    image: busybox
    env:
      - name: APP_MODE
        valueFrom:
          configMapKeyRef:
            name: example-config
            key: APP_MODE
📊

Quick Reference

FieldDescriptionUsage
envFromImports all keys from a ConfigMap as environment variablesenvFrom: - configMapRef: name: my-configmap
env with configMapKeyRefImports a specific key from a ConfigMap as an environment variableenv: - name: VAR_NAME valueFrom: configMapKeyRef: name: my-configmap key: key-name
ConfigMapStores key-value pairs for configurationapiVersion: v1 kind: ConfigMap metadata: name: my-configmap data: key1: value1 key2: value2
Pod restartRequired to update env vars after ConfigMap changeskubectl delete pod

Key Takeaways

Use envFrom to load all ConfigMap keys as environment variables in a container.
Use env with configMapKeyRef to load specific ConfigMap keys as environment variables.
Ensure the ConfigMap exists before creating pods that reference it to avoid errors.
Pods must be restarted to pick up ConfigMap changes in environment variables.
Avoid exposing sensitive data by carefully selecting which ConfigMap keys to import.