DevOps.Academy
CoursesCurriculumInterview prepPricing
◔My learningSign in
DevOps.Academy

Free, hands-on DevOps lessons. Made with care in India.

System status

Learn

All coursesLinux roadmapCurriculumInterview questionsCheat sheetsMy learning

Academy

PricingCertificates

Legal

PrivacyTermsContact

devops.rajeev.pro

Kubernetes & HelmLesson 6 of 16
Your progress · 0%
On this page
  1. ConfigMaps
  2. Secrets
↖ Course roadmap
Lesson 6 · Configuration

Configuration: ConfigMaps and Secrets

Keep configuration out of images: ConfigMaps for settings, Secrets for credentials, how they reach containers, and why a Kubernetes Secret is encoded, not encrypted.

25 min read · intermediate · hands-on

By the end of this lesson you'll be able to

0 of 4 complete
Lab status

Verified lab: the ConfigMap, the Secret, decoding it, loading both into shop-web, and reading them back from a running Pod, on the course's EKS cluster. The output shown is real.
Code walkthrough: External Secrets Operator and KMS encryption (explained, not run).
Cloud lab: none needed; works on kind.

The same image should run in dev and prod; only the settings change. If the greeting, the colour or the database address is baked into the image, every change means a new build. Kubernetes keeps settings in two objects: ConfigMaps for ordinary settings and Secrets for credentials.

ConfigMaps

# 06-config.yaml (part 1)
apiVersion: v1
kind: ConfigMap
metadata:
  name: shop-config
data:
  GREETING: "Namaste from a ConfigMap"
  APP_COLOR: "#b23f1f"

A ConfigMap is just key-value data. A Pod can use it two ways:

  • As environment variables (envFrom or env.valueFrom): simple, read once when the container starts.
  • As files in a mounted volume: good for whole config files (an nginx.conf), and updated in the Pod eventually after you change the ConfigMap.

Two routes into a container. The lab used environment variables for both objects.© Diagram: DevOps Academy

Two routes into a container. The lab used environment variables for both objects.© Diagram: DevOps Academy

The lab loaded every key as an environment variable, then called the app:

$ kubectl set env deployment/shop-web --from=configmap/shop-config
deployment.apps/shop-web env updated
$ kubectl run c --image=busybox:1.37 --restart=Never --rm -i --quiet -- wget -qO- http://shop-web/api
{"version": "v1", "pod": "shop-web-55d49ccc4b-5xkmp", "greeting": "Namaste from a ConfigMap", "uptime_seconds": 11}
kubectl set env deployment/shop-web --from=configmap/shop-config
  • kubectltalk to a Kubernetes cluster
kubectl run c --image=busybox:1.37 --restart=Never --rm -i --quiet -- wget -qO- http://shop-web/api
  • --image=busybox:1.37container image

The greeting changed without rebuilding the image. Note uptime_seconds: 11: changing the env meant a new Pod template, so the Deployment rolled out new Pods. That's the rule to remember: environment variables are read only at start. Edit a ConfigMap later and running Pods keep the old values until they restart, for example with kubectl rollout restart deployment/shop-web.

The same change, two outcomes: env vars wait for new Pods, mounted files are updated in place.© Animation: DevOps Academy

The same change, two outcomes: env vars wait for new Pods, mounted files are updated in place.© Animation: DevOps Academy

Secrets

A Secret looks almost the same. stringData lets you write the value in plain text, and Kubernetes stores it base64-encoded:

# 06-config.yaml (part 2)
apiVersion: v1
kind: Secret
metadata:
  name: shop-secret
type: Opaque
stringData:
  DATABASE_URL: "postgres://shop:S3cr3t-demo@db.internal:5432/shop"

Now look at what anyone with read access to Secrets sees:

$ kubectl get secret shop-secret -o yaml
apiVersion: v1
data:
  DATABASE_URL: cG9zdGdyZXM6Ly9zaG9wOlMzY3IzdC1kZW1vQGRiLmludGVybmFsOjU0MzIvc2hvcA==
kind: Secret
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Secret","metadata":{"annotations":{},"name":"shop-secret","namespace":"default"},"stringData":{"DATABASE_URL":"postgres://shop:S3cr3t-demo@db.internal:5432/shop"},"type":"Opaque"}
  ...
type: Opaque
$ kubectl get secret shop-secret -o jsonpath="{.data.DATABASE_URL}" | base64 -d
postgres://shop:S3cr3t-demo@db.internal:5432/shop
kubectl get secret shop-secret -o yaml
  • kubectltalk to a Kubernetes cluster
  • getlist objects
  • -ooutput format
kubectl get secret shop-secret -o jsonpath="{.data.DATABASE_URL}" | base64 -d
  • |pipe: send output to the next command

Two lessons in one output:

  1. base64 is not encryption. One command decodes it.
  2. The password also sits in plain text in an annotation. kubectl apply saves the manifest it applied in last-applied-configuration, stringData and all. Applying real secrets from files leaks them a second time.

Inside the Pod, it's an ordinary environment variable:

$ kubectl set env deployment/shop-web --from=secret/shop-secret
deployment.apps/shop-web env updated
$ kubectl exec shop-web-55d49ccc4b-5xkmp -- printenv GREETING APP_COLOR DATABASE_URL
Namaste from a ConfigMap
#b23f1f
postgres://shop:S3cr3t-demo@db.internal:5432/shop
kubectl set env deployment/shop-web --from=secret/shop-secret
  • kubectltalk to a Kubernetes cluster

Keeping real secrets safe

So why use Secrets at all? Because Kubernetes treats them differently from ConfigMaps: RBAC can grant get secrets separately (lesson 8), they're kept in memory on nodes rather than on disk, and the API server can encrypt them at rest.

The lab found the password in the decoded data, the annotation and the Pod's environment.© Diagram: DevOps Academy

The lab found the password in the decoded data, the annotation and the Pod's environment.© Diagram: DevOps Academy

For real credentials, go further:

  • Encryption at rest with KMS. On EKS, envelope encryption with an AWS KMS key encrypts Secrets in etcd. (The course lab skipped it to save cost; production shouldn't.)
  • Least privilege. Very few people and service accounts should be able to read Secrets.
  • An external secrets store. Keep the real value in AWS Secrets Manager or Vault, and let the External Secrets Operator create the Kubernetes Secret, or mount it directly with the Secrets Store CSI Driver. The value then never lives in Git or in a manifest.
  • Never commit Secret manifests with real values to Git, even private repositories.
⌘
Mini mission

Change the app's behaviour without a rebuild

Create a ConfigMap with your own GREETING and APP_COLOR, and a Secret with a fake DATABASE_URL. Load both into shop-web, call /api to see the new greeting, decode the Secret, and find the second place its value appears in the Secret's YAML.

Input: The shop-web Deployment and Service from lessons 4 and 5Output: ~/config.txt
Reveal one safe solution
$ kubectl apply -f 06-config.yaml
$ kubectl set env deployment/shop-web --from=configmap/shop-config --from=secret/shop-secret
$ kubectl rollout status deployment/shop-web
$ kubectl run c --image=busybox:1.37 --restart=Never --rm -i --quiet -- wget -qO- http://shop-web/api > ~/config.txt
$ kubectl get secret shop-secret -o jsonpath="{.data.DATABASE_URL}" | base64 -d >> ~/config.txt
$ kubectl get secret shop-secret -o yaml | grep -c S3cr3t
kubectl apply -f 06-config.yaml
  • kubectltalk to a Kubernetes cluster
  • applycreate or update from a file
  • -ffrom this file
kubectl rollout status deployment/shop-web
  • rolloutmanage a rollout
kubectl run c --image=busybox:1.37 --restart=Never --rm -i --quiet -- wget -qO- http://shop-web/api > ~/config.txt
  • --image=busybox:1.37container image
  • >write output to a file, replacing it
kubectl get secret shop-secret -o jsonpath="{.data.DATABASE_URL}" | base64 -d >> ~/config.txt
  • getlist objects
  • -ooutput format
  • |pipe: send output to the next command
  • >>append output to a file
kubectl get secret shop-secret -o yaml | grep -c S3cr3t
  • grepfind lines that match a pattern
  • -ccount matching lines

The last command finds the plain-text password in the annotation. If the greeting doesn't change, the Pods weren't restarted: check kubectl rollout status.

Try it yourself

0 of 5 steps done

Run each task in your own account or terminal, then tick it off.

Knowledge check

Quick check

Question 1 of 3

What protects the value inside a Kubernetes Secret by default?

Go deeper

Official reading for this lesson

  • DocsConfigMapsKubernetes ↗
  • DocsSecretsKubernetes ↗
  • DocsGood practices for Kubernetes SecretsKubernetes ↗
  • ToolExternal Secrets OperatorExternal Secrets ↗
  • DocsEnvelope encryption of Kubernetes secrets on EKSAWS Docs ↗

Or get every quiz answer right and it completes itself.

Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.

← PreviousServices, Ingress and load balancingNext lesson →Resource management: requests, limits, namespaces, quotas