0
0
KubernetesHow-ToBeginner · 3 min read

How to Use ConfigMap in Pod in Kubernetes

Use a ConfigMap in a Pod by referencing it in the Pod's YAML under envFrom for environment variables or volumes and volumeMounts for files. This lets your Pod access configuration data without rebuilding the container image.
📐

Syntax

A ConfigMap is referenced in a Pod spec in two main ways:

  • Environment variables: Use envFrom to load all keys as environment variables.
  • Files: Use volumes to mount the ConfigMap as files inside the container.

Each method requires specifying the configMap name and where to mount or inject the data.

yaml
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: example-container
    image: nginx
    envFrom:
    - configMapRef:
        name: example-configmap
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config
  volumes:
  - name: config-volume
    configMap:
      name: example-configmap
💻

Example

This example shows a ConfigMap with two keys and a Pod that uses it both as environment variables and as files.

yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-configmap
data:
  APP_COLOR: "blue"
  APP_MODE: "production"
---
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: example-container
    image: busybox
    command: ["sh", "-c", "env; cat /etc/config/APP_COLOR"]
    envFrom:
    - configMapRef:
        name: example-configmap
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config
  volumes:
  - name: config-volume
    configMap:
      name: example-configmap
Output
APP_COLOR=blue APP_MODE=production ...other env vars... blue
⚠️

Common Pitfalls

  • Not creating the ConfigMap before the Pod causes Pod creation to fail.
  • Misspelling the ConfigMap name in the Pod spec leads to errors.
  • For file mounts, forgetting to specify volumeMounts causes files not to appear.
  • Using env instead of envFrom requires specifying each key manually.
yaml
apiVersion: v1
kind: Pod
metadata:
  name: wrong-pod
spec:
  containers:
  - name: container
    image: busybox
    envFrom:
    - configMapRef:
        name: wrong-name  # Wrong ConfigMap name
---
# Correct usage:
apiVersion: v1
kind: Pod
metadata:
  name: correct-pod
spec:
  containers:
  - name: container
    image: busybox
    envFrom:
    - configMapRef:
        name: example-configmap
📊

Quick Reference

Use these tips when working with ConfigMaps in Pods:

  • Use envFrom to load all ConfigMap keys as environment variables.
  • Use volumes and volumeMounts to mount ConfigMap data as files.
  • Always create the ConfigMap before the Pod.
  • Check names carefully to avoid typos.

Key Takeaways

Reference ConfigMaps in Pods using envFrom for environment variables or volumes for files.
Create the ConfigMap before deploying the Pod to avoid errors.
Double-check ConfigMap names in Pod specs to prevent failures.
Use volumeMounts to mount ConfigMap data as files inside containers.
envFrom loads all ConfigMap keys as environment variables automatically.