Kubernetes ConfigMaps explained: Create, mount and use them

Learn how to create Kubernetes ConfigMaps and use them in three ways: as environment variables, with envFrom, and as volume mounts. Includes working YAML examples.

4 lessons · 10 min · Advanced

2 minutes reading time

Written by

Civo Team
Civo Team

Marketing Team at Civo

ConfigMaps let you separate configuration data from your container images. Instead of baking environment-specific values into your image, you store them in a ConfigMap and inject them at runtime. This means the same image can run in development, staging, and production with different configuration, without rebuilding.

ConfigMaps are for non-sensitive data. For passwords, tokens, and certificates, use Secrets instead.

Create a ConfigMap

From a literal value:

kubectl create configmap demo --from-literal=name=saiyam

From a file:

kubectl create configmap test --from-file=file.prop

From an environment file (multiple key-value pairs as separate data entries):

kubectl create configmap test2 --from-env-file=env.prop

View all ConfigMaps:

kubectl get configmap

Inspect a ConfigMap:

kubectl describe configmap demo

Expected output:

Name: demo
Namespace: default
Data
====
name:
----
saiyam

Three ways to consume a ConfigMap

PatternUse when

env with configMapKeyRef

You need one or two specific keys from a ConfigMap

envFrom with configMapRef

You want all ConfigMap keys loaded as environment variables at once

Volume mount

Your application reads configuration from files on disk

configmap-consumption-patterns

Pattern 1: reference a specific key as an environment variable

apiVersion: v1
kind: Pod
metadata:
name: demo
spec:
containers:
- name: busybox
image: busybox
command: ["env"]
env:
- name: HELLO
valueFrom:
configMapKeyRef:
name: demo
key: name
restartPolicy: Never
kubectl create -f demo.yaml
kubectl logs demo

Expected output:

HELLO=saiyam

Pattern 2: load all keys as environment variables with envFrom

Use envFrom when you want every key in the ConfigMap available as an environment variable without listing them individually:

apiVersion: v1
kind: Pod
metadata:
name: demo-envfrom
spec:
containers:
- name: busybox
image: busybox
command: ["env"]
envFrom:
- configMapRef:
name: demo
restartPolicy: Never
kubectl create -f demo-envfrom.yaml
kubectl logs demo-envfrom

All keys from the demo ConfigMap will appear as environment variables in the container output.

Pattern 3: mount a ConfigMap as a volume

When your application reads configuration from files on disk, mount the ConfigMap as a volume. Each key in the ConfigMap becomes a file at the mount path, with the value as the file contents.

First, create a ConfigMap with multiple keys:

apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
file1: |
setting1=value1
setting2=value2
file2: |
debug=true
log_level=info
kubectl apply -f cm-for-volume.yaml

Then mount it into a pod:

apiVersion: v1
kind: Pod
metadata:
name: cm-volume
spec:
containers:
- name: busybox
image: busybox
command: ["ls", "/etc/config"]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: app-config
restartPolicy: Never
kubectl create -f demo2.yaml
kubectl logs cm-volume

Expected output:

file1
file2

Each key from the ConfigMap appears as a file at /etc/config. The value of each key is the file contents.

ConfigMaps as a volume type

ConfigMaps and Secrets are treated as ephemeral volume types in Kubernetes. They appear in the pod's volumes spec just like any other volume type, alongside emptyDir, hostPath, and PersistentVolumeClaims. The key difference is that their contents come from cluster objects rather than from the node filesystem or a storage backend.

This means you can mix ConfigMap volumes with other volume types in the same pod spec. A pod might mount a ConfigMap for configuration files, a Secret for certificates, and a PersistentVolumeClaim for application data, all in the same volumes section.

One important behaviour: when a ConfigMap is mounted as a volume, Kubernetes updates the files automatically when the ConfigMap changes, typically within 60 seconds. Environment variables set from ConfigMaps at pod startup do not update without a pod restart.

Civo Team
Civo Team

Marketing Team at Civo

Civo is the Sovereign Cloud and AI platform designed to help developers and enterprises build without limits. We bridge the gap between the openness of the public cloud and the rigorous security of private environments, delivering full cloud parity across every deployment. As a team, we are dedicated to providing scalable compute, lightning-fast Kubernetes, and managed services that are ready in minutes. Through CivoStack Enterprise and our FlexCore appliance, we empower organizations to maintain total data sovereignty on their own hardware.

Our mission is to make the cloud faster, simpler, and fairer. By providing enterprise-grade NVIDIA GPUs and streamlined model management, we ensure that high-performance AI and machine learning are accessible to everyone. Built for transparency and performance, the Civo Team is here to give you total control over your infrastructure, your data, and your spend.

View author profile