This article covers Kubernetes ConfigMaps and Secrets in detail. Hard-coding configuration values and passwords into container images is one of the most common mistakes in containerized applications. Every environment change forces a rebuild, and credentials end up burned into image layers forever. Kubernetes solves this with two dedicated objects: a Kubernetes ConfigMap and Secret pair of mechanisms that inject configuration and sensitive data into pods at runtime. In this guide you will learn how a Kubernetes ConfigMap and Secret differ, how to create both, and the three ways to consume them — as environment variables, as single values, and as mounted files.
What Is a Kubernetes ConfigMap and Secret? – Kubernetes ConfigMaps and Secrets
A ConfigMap stores non-sensitive configuration as key-value pairs: feature flags, endpoints, log levels, whole configuration files. A Secret exists for sensitive data: passwords, API tokens, TLS keys. The two objects look almost identical in YAML; the practical differences are intent and handling:
- Secrets are base64-encoded in the manifest (encoding, not encryption).
- Kubernetes can restrict who may read Secrets through RBAC, and etcd encryption at rest can protect them on disk.
- Secrets can be mounted as in-memory tmpfs files so values never touch the container’s filesystem layer.
Because base64 is trivially reversible, treat a Kubernetes ConfigMap and Secret with the same care: never commit real Secret manifests to a public Git repository.
Create a Kubernetes ConfigMap and Secret – Kubernetes ConfigMaps and Secrets
ConfigMap from literal values
kubectl create configmap app-config \
--from-literal=APP_ENV=production \
--from-literal=LOG_LEVEL=info \
--from-literal=FEATURE_X=true
ConfigMap from a file
kubectl create configmap nginx-config --from-file=default.conf
The file’s name becomes the key, and its content becomes the value — perfect for shipping a full nginx.conf or application.properties.
Secret from literal values
kubectl create secret generic db-credentials \
--from-literal=DB_USER=appuser \
--from-literal=DB_PASSWORD='S3cureP@ss'
Creating from the command line keeps plaintext passwords out of YAML files on your disk (though they may appear in your shell history — use --from-file or lead the command with a space where supported).
Use a Kubernetes ConfigMap and Secret in a Pod
Option 1: All keys as environment variables
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: web
image: myapp:1.0
envFrom:
- configMapRef:
name: app-config
- secretRef:
name: db-credentials
Option 2: Individual values
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-config
key: LOG_LEVEL
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-credentials
key: DB_PASSWORD
Option 3: Mounted as files
volumeMounts:
- name: config-volume
mountPath: /etc/app
volumes:
- name: config-volume
configMap:
name: nginx-config
File mounts have a big advantage: when the ConfigMap changes, the mounted files update automatically (within about a minute), so applications that can reload their configuration pick up changes without a restart. Environment variables, by contrast, are fixed when the container starts — changing a Kubernetes ConfigMap and Secret consumed as env vars requires a pod restart to take effect.
Best Practices for Every Kubernetes ConfigMap and Secret
- One object per application and environment: name them clearly, for example
billing-config-prod. - Enable etcd encryption at rest on self-managed clusters, and restrict Secret access with RBAC — by default anyone with namespace admin can read them.
- Restart deliberately: add a checksum annotation of the config content to your Deployment’s pod template so a config change triggers a clean rolling update.
- For serious secrets management, graduate to an external store (Vault, cloud secret managers) with the External Secrets Operator syncing values into Kubernetes Secrets.
- Audit usage:
kubectl describe podshows which ConfigMaps and Secrets a pod consumes; the commands in our kubectl commands cheat sheet help track them down.
Configuration usually lives alongside the workloads it configures — if you manage your Deployments with charts, our Helm chart tutorial shows how values files generate ConfigMaps cleanly per environment.
Immutable ConfigMaps and Secrets
For configuration that should never change after creation, set immutable: true on the object. Immutable ConfigMaps and Secrets cannot be edited in place — you create a new one and point the workload at it — and the kubelet can skip watching them, which noticeably reduces API server load on clusters with thousands of mounted objects. This is a good default for versioned application configuration released together with a specific application version.
Troubleshooting
- Pod stuck in CreateContainerConfigError: a referenced ConfigMap or Secret (or a specific key) does not exist in the pod’s namespace. Objects are namespace-scoped — a ConfigMap in
defaultcannot be used by a pod inproduction. - Values not updating: env vars never refresh; restart the pods with
kubectl rollout restart deployment. - Secret looks garbled with kubectl get: output is base64. Decode intentionally with
kubectl get secret db-credentials -o jsonpath='{.data.DB_PASSWORD}' | base64 -d— and be careful where that plaintext lands. - File mount shows old content: wait for the kubelet sync period, and check the app actually re-reads the file.
Conclusion
A Kubernetes ConfigMap and Secret keep configuration out of your images and give each environment its own values without rebuilds. Use ConfigMaps for ordinary settings, Secrets for credentials, prefer file mounts when live reload matters, and protect Secrets with RBAC and etcd encryption. Master these two objects and your applications become portable across development, staging, and production with a single image. The official ConfigMap documentation and its Secret counterpart cover the remaining details, including immutable objects and binary data.