Hardcoding configuration into a container image is one of the fastest ways to create a deployment that works in one environment and breaks in every other. Kubernetes ConfigMaps and Secrets solve this by separating configuration from code: the same image runs in development, staging, and production, and only the injected configuration changes. In this guide you will create ConfigMaps and Secrets, mount them as files and environment variables, update them safely, and avoid the security mistakes that expose credentials.
The rule is simple: non-sensitive settings belong in a ConfigMap, and anything a stranger could abuse, such as passwords, API keys, and TLS private keys, belongs in a Secret. Both are namespaced objects, both are consumed the same way, and the difference that matters is how Kubernetes handles them: Secrets are stored base64-encoded, are never written to disk on nodes by default when mounted as volumes with tmpfs, and can be restricted with role-based access control.

Kubernetes ConfigMaps Explained
A ConfigMap is a key-value store for configuration data. Values can be short strings, such as a log level, or whole files, such as an nginx.conf. You create one from literal values, from a file, or from a directory of files:
kubectl create configmap app-config --from-literal=LOG_LEVEL=info --from-literal=APP_ENV=production
kubectl create configmap nginx-config --from-file=nginx.conf
kubectl get configmap app-config -o yaml
Declarative YAML is better for anything you keep in version control. A ConfigMap manifest is short, readable, and reviewable in a pull request, which is exactly what you want for configuration that changes behaviour in production.
One more habit worth building early: treat ConfigMaps as code. Store the manifest in Git next to your Deployment, review changes in pull requests, and apply them with kubectl apply rather than imperative create commands. That way a configuration change has the same audit trail as a code change, and rolling back is a git revert instead of a guess about what the previous value was.
Prerequisites
- Access to a Kubernetes cluster (local kind, minikube, or a cloud cluster) with kubectl configured.
- Basic familiarity with Deployments and pods.
- A namespace you can experiment in, separate from production workloads.
Step 1: Inject Kubernetes ConfigMaps as Environment Variables
The simplest consumption pattern is environment variables. Reference individual keys with valueFrom, or import the whole ConfigMap with envFrom:
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-config
key: LOG_LEVEL
envFrom:
- configMapRef:
name: app-config
Prefer explicit valueFrom references for the handful of settings your application actually reads, and reserve envFrom for cases where the application is designed to read its whole environment. Explicit references fail visibly if a key is renamed, while a bulk import silently drops the missing value.
Step 2: Mount Kubernetes ConfigMaps as Files
Some applications only read configuration from files. Mount the ConfigMap as a volume and each key becomes a file in the mount directory:
volumes:
- name: config
configMap:
name: nginx-config
containers:
- name: nginx
image: nginx:1.27
volumeMounts:
- name: config
mountPath: /etc/nginx/conf.d
Mounted ConfigMaps update automatically: when you edit the ConfigMap, kubelet refreshes the files within a minute or so, although the application may need a reload or restart to pick up the new values. Tools such as Reloader automate that restart by watching for ConfigMap changes.
Step 3: Create and Use Secrets Safely
Create a Secret from literals, keeping in mind that the values are base64-encoded, not encrypted, in etcd unless encryption at rest is enabled:
kubectl create secret generic db-credentials --from-literal=username=appuser --from-literal=password='change-me-please'
kubectl create secret tls web-tls --cert=fullchain.pem --key=privkey.pem
Mount Secrets the same way as ConfigMaps, either as environment variables via secretKeyRef or as files via a secret volume. Mounting as files is often the safer choice: environment variables leak into process listings, crash dumps, and child processes more easily than files with restricted permissions.
Troubleshooting Kubernetes ConfigMaps and Secrets
Pod fails with ConfigMap not found. ConfigMaps are namespaced. A pod can only use ConfigMaps in its own namespace; check kubectl get configmap -n <namespace> and confirm the name matches exactly, including case.
Application ignores a ConfigMap change. Environment variables are frozen at pod start and never update. Recreate the pod, or switch to a volume mount plus a reload mechanism.
Secret values look garbled. The stored value is base64. Decode it with kubectl get secret db-credentials -o jsonpath='{.data.password}' | base64 -d to verify you stored what you intended.
Secrets appear in logs or Git. Never commit Secret manifests with real values. Use Sealed Secrets, External Secrets Operator, or your cloud secret manager, and rotate any credential that has ever touched version control.
Conclusion
Separating configuration from images with Kubernetes ConfigMaps and Secrets is a small discipline that pays off on every deployment: one image, many environments, no credentials baked into layers. Use ConfigMaps for ordinary settings, Secrets for sensitive data, volume mounts for file-based configuration, and a proper secret manager once real credentials are involved. For more Kubernetes practice, see our guides on Kubernetes PodDisruptionBudgets and Kubernetes StatefulSets for databases. The official reference is Kubernetes ConfigMaps documentation.