ProdCraft
Subscribe
Kubernetes

Kubernetes ConfigMaps and Secrets: Safe Application Configuration Patterns

Use Kubernetes ConfigMaps and Secrets safely, understand env vs volume updates, RBAC, encryption and rotation strategies.

ProdCraft5 min read
ProdCraft featured image — Kubernetes ConfigMaps and Secrets: Safe Application Configuration Patterns

This guide takes a practical look at Kubernetes ConfigMap Secret for engineers running or learning Kubernetes. Commands are examples for a cluster you administer; verify your current context, namespace, Kubernetes version and available controllers before applying configuration. The aim is to understand the system’s behavior well enough to diagnose a failure, not merely copy a manifest from a screenshot.

Separate deployable code from runtime configuration

An image should not require rebuilding every time an API base URL changes or a staging token rotates. Kubernetes ConfigMaps store non-sensitive settings; Secrets represent sensitive configuration values with a separate resource type and API access controls. A Secret is not automatically encrypted simply because it is named Secret: base64 encoding is not encryption. The operational challenge is controlling who can read values, how they reach Pods, and how changes propagate to running applications without breaking traffic.

Create a simple non-secret ConfigMap

For a local exercise, run kubectl create configmap demo-settings --from-literal=LOG_LEVEL=info in a dedicated namespace. A Deployment can project the value using env.valueFrom.configMapKeyRef, or mount ConfigMap entries into files. Prefer clear key names and include defaults in the application where sensible. Inspect kubectl describe configmap demo-settings to confirm non-sensitive content. Never put database passwords into a ConfigMap simply because it is easier to inspect from kubectl.

Create a demo Secret without confusing encoding with safety

For a disposable lab, kubectl create secret generic demo-credentials --from-literal=API_TOKEN=not-a-real-token illustrates the resource; do not use a real production secret in shell history. In production, prefer a secret manager integration or a protected, auditable delivery method. Use secretKeyRef to make credentials available to the container only when it starts. Restrict who can get, list or watch Secrets, and avoid pasting entire Secret YAML into bug reports or logs.

Understand refresh behavior

Environment variables injected from ConfigMaps or Secrets are evaluated when a container starts and do not automatically update for an already running process. Projected Secret or ConfigMap volumes can refresh eventually under normal conditions, but the application may need to reload the file. A subPath mount behaves differently and does not receive automatic updates in the same way. Plan controlled rollouts for credential rotation and implement overlapping credentials where the backend supports it. Test both new and old Pod behavior during the rotation window.

Strengthen authorization and storage protections

Give workloads their own ServiceAccounts with narrowly scoped RBAC. Enable encryption at rest for Kubernetes Secrets where supported and required; confirm the cluster’s etcd protection and backup handling. Cluster administrators and principals with powerful Pod-creation rights may still access secrets through workload creation, so evaluate effective permissions, not simply direct Secret reads. Avoid exposing sensitive values in CI logs and Helm release histories. Consider external secret stores for organizations that already have approved rotation and audit systems.

Test the failure and rollback path

Intentionally remove a referenced ConfigMap key in a disposable namespace and inspect Pod events; the error often appears before the app launches. Test an expired API credential and confirm errors are detected without printing token values. Roll back a configuration deployment separately from the application image if necessary. Maintain a clear map from deployment versions to configuration revisions. A healthy configuration system is one where changing an environment value doesn’t require finding credentials inside an image archive.

Hands-on lab: make your verification repeatable

For a useful practice session, create one disposable namespace, record the current Kubernetes context, and define the initial healthy state. Apply one configuration change at a time and verify both resource status and a real application operation. Check events and controller conditions after each change. Recreate a failure deliberately in the lab, capture the same evidence an on-call engineer would need, and restore the last known-good version. This exercise matters because successful manifest application only proves API admission, not that the app is reachable, authorized, or correct.

Use resource labels and stable names so command output can be traced to the particular lab. Keep redacted logs or screenshots of the healthy and unhealthy states for comparison. When cleanup is complete, verify that no unexpected public load balancers, persistent volumes or privileged service accounts remain; a forgotten demo can still create expense or risk.

Production readiness checklist

Confirm namespace and RBAC ownership, immutable or carefully governed image references, sensible CPU and memory requests, availability probes, resource quotas and appropriate NetworkPolicies. Plan upgrades with a rollback procedure and separate database migration rules. Observe errors, tail latency, saturation and controller status, not only Pod count. Restrict who can alter cluster-wide resources and keep external certificates or credentials out of public Git history. Run a smoke test after every deployment and evaluate whether failures are immediately actionable from logs, metrics and events.

Frequently asked questions

Can I try this safely on kind or another local cluster?

Most concepts can be practiced on a disposable local cluster. Cloud load balancers, storage drivers and selected controllers may behave differently, so follow the deployment platform’s documentation before treating a local result as production proof.

Which command should I start with when it fails?

Check the active context, then inspect the affected resource’s status and events with kubectl. Narrow the failure to admission, scheduling, container startup, networking or application behavior before making a corrective change.

Official references

Read the upstream Kubernetes or project documentation for API compatibility and detailed options. For a complementary guide on production access boundaries, see our enterprise security patterns; authorization and traceability matter equally in Kubernetes and AI systems.

Credential rotation scenario

Suppose a support API has a token injected through a Kubernetes Secret as an environment variable. Create a non-sensitive dummy value in a test namespace, start the Pod and log only whether the value is present, never its contents. Change the Secret, then verify that an already running process may still see the original environment value until restarted. Roll out a new Pod and confirm it receives the new dummy value. Repeat with a projected volume to understand when application reload logic is necessary.

This simple experiment makes rotation planning concrete. Document who owns the Secret, the overlap window between old and new credentials, and how consumers can recover from invalid credentials. For similar authorization boundaries in model applications, read the enterprise RAG security guide.

Share this article

Written by

ProdCraft

ProdCraft publishes practical engineering guides on DevOps, Kubernetes, Docker, Linux, databases, cloud and LLMs — written for engineers who ship.

227 articles View profile

The weekly ops & AI brief

One email a week with the best new guides, tools and incident write-ups. No spam, unsubscribe anytime.

Leave a Reply

Your email address will not be published. Required fields are marked *