⚡ ~/naveed K8s Hub
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
Phase 1 — Fundamentals Module 04 of 24 Free & Open Access

Kubernetes ConfigMaps & Secrets

ConfigMaps and Secrets keep configuration out of container images. Both can be injected as environment variables or mounted files, but Secrets are only base64-encoded by default, not encrypted. This module covers both patterns, how updates reach running Pods, and how to protect Secrets at rest.

Last updated: · By Naveed Ahmed, Certified Kubernetes Administrator

04 - ConfigMaps & Secrets

Why this matters

This is how you separate config from container images — the same image should be deployable to dev/staging/prod with different config, no rebuild.

Read this first — Definitions & Explanations

ConfigMap

Stores non-confidential configuration as key/value data (or files). Inject into Pods as:

Use ConfigMaps for app config, feature flags, non-secret URLs, templates.

Secret

Stores sensitive data (passwords, tokens, TLS keys). In etcd, Secrets are base64-encoded by default, not strongly encrypted unless you enable encryption at rest. Still treat them as sensitive and restrict RBAC.

How Pods consume them

  1. env / envFrom — values become environment variables
  2. volumeMounts — keys become files in a directory
    File mounts are often preferred for credentials rotation patterns.

Updates and restarts

Changing a ConfigMap/Secret does not always restart Pods automatically. Env-based injection typically needs a rollout. Some volume mounts can update files in place (with caveats and delays).

Good practice

Official docs (read for detail)

Key Concepts

YouTube search terms

Hands-on lab (on prod-sim)

# ConfigMap as env vars
kubectl create configmap app-config --from-literal=LOG_LEVEL=debug --from-literal=FEATURE_X=on
kubectl run cfgtest --image=busybox --restart=Never --env-from=configmap/app-config -- env
kubectl logs cfgtest | grep -E 'LOG_LEVEL|FEATURE_X'

# ConfigMap as mounted volume
kubectl create configmap nginx-conf --from-file=nginx.conf=<(echo "worker_processes 2;")
kubectl run cfgvol --image=nginx --restart=Never --overrides='
{"spec":{"volumes":[{"name":"conf","configMap":{"name":"nginx-conf"}}],
"containers":[{"name":"cfgvol","image":"nginx","volumeMounts":[{"name":"conf","mountPath":"/etc/nginx/conf.d/custom.conf","subPath":"nginx.conf"}]}]}}'
kubectl exec cfgvol -- cat /etc/nginx/conf.d/custom.conf

# Secrets — prove they're only base64, not encrypted
kubectl create secret generic db-creds --from-literal=password=SuperSecret123
kubectl get secret db-creds -o jsonpath='{.data.password}' | base64 -d
kubectl get secret db-creds -o yaml   # see it's just base64 in etcd unless encryption-at-rest is on

# Check if this cluster has encryption-at-rest configured (it likely does NOT by default)
minikube ssh -p prod-sim
  sudo cat /etc/kubernetes/manifests/kube-apiserver.yaml | grep -i encryption
  exit

Common mistakes and how to avoid them

  • Believing Secrets are encrypted. Values are only base64-encoded in the API object unless encryption at rest is enabled for etcd. Anyone who can read the Secret can decode it.
  • Expecting env vars to change after editing a ConfigMap. Environment variables are read when the container starts. Mounted files update eventually, but env vars need a Pod restart, for example kubectl rollout restart.
  • Mounting a ConfigMap over a directory that already has files. The mount hides what the image had in that path. Use subPath to add a single file, remembering that subPath mounts do not receive updates.
  • Committing Secret manifests to Git. base64 is not protection. Use Sealed Secrets, SOPS or an external secrets operator backed by a secret manager.
  • Putting large data in a ConfigMap. Objects are limited to about 1 MiB because they live in etcd. Keep large files on a volume or in object storage.

Quick answers

What is the difference between a ConfigMap and a Secret?

Both hold key-value data for Pods. A ConfigMap is for non-sensitive configuration, while a Secret is meant for credentials and can be locked down with RBAC and encrypted at rest. They are consumed the same way: as environment variables or as mounted files.

Do Pods see ConfigMap changes automatically?

Mounted ConfigMap files update after a short delay, but environment variables never change in a running container, and files mounted with subPath do not update either. If the app reads its config only at startup, restart the Pods with kubectl rollout restart.

How do you protect Secrets in Kubernetes?

Enable encryption at rest for etcd, restrict get and list on Secrets with RBAC, and keep manifests out of Git. Prefer an external secret manager with an operator such as External Secrets, and avoid printing secret values in logs or environment dumps.

Notes

(fill in your own words after watching + labbing)

📋 Self-Assessment Mastery Checklist (4 Competencies)
🧠 Practice Exam Questions (Module 04 MCQs)
⚡ Take Quiz & Save Progress in Tracker

Review these sample exam questions out loud, test your retrieval, and then unlock official scoring in the interactive tracker.

Question 1: ConfigMaps are intended for:
  • A. Sensitive passwords only
  • B. Non-confidential configuration data
  • C. TLS private keys exclusively
  • D. etcd snapshots
✓ Correct Answer: B (Non-confidential configuration data)
Option B ('Non-confidential configuration data') is the standard production architectural best practice.
Question 2: Secrets in Kubernetes are by default:
  • A. Encrypted with a customer KMS key always
  • B. Base64-encoded (not strongly encrypted at rest unless configured)
  • C. Never stored in etcd
  • D. Visible only to kubelet
✓ Correct Answer: B (Base64-encoded (not strongly encrypted at rest unless configured))
Option B ('Base64-encoded (not strongly encrypted at rest unless configured)') is the standard production architectural best practice.
Question 3: You can mount ConfigMap data into a Pod as:
  • A. Only environment variables
  • B. Only files
  • C. Environment variables and/or volume files
  • D. Only annotations
✓ Correct Answer: C (Environment variables and/or volume files)
Option C ('Environment variables and/or volume files') is the standard production architectural best practice.
Question 4: Updating a ConfigMap mounted as a volume typically:
  • A. Never reaches running Pods
  • B. Can update files for many volume mounts (with caveats)
  • C. Restarts the whole cluster
  • D. Deletes the Secret
✓ Correct Answer: B (Can update files for many volume mounts (with caveats))
Option B ('Can update files for many volume mounts (with caveats)') is the standard production architectural best practice.
Question 5: Best practice for production secrets is to:
  • A. Commit them to git
  • B. Use external secret managers / encryption + least privilege RBAC
  • C. Put them in ConfigMaps
  • D. Hardcode in the image
✓ Correct Answer: B (Use external secret managers / encryption + least privilege RBAC)
Option B ('Use external secret managers / encryption + least privilege RBAC') is the standard production architectural best practice.
← Previous Module (03) Services & Cluster Networking Basics Next Module (05) → Storage: Volumes, PV, PVC, StorageClasses