Skip to content
Hands-on IT IT Drills

Day 6: ConfigMaps and Secrets

Goal: You separate configuration and secrets from the image and hand them to notes-api as a variable or a file. · Duration: approx. 60 min · Prerequisite: Day 2 (image notes-api:1.0 in the registry), Day 5

Briefing

CKAD: Application Environment, Configuration and Security (25 %) – “Understand ConfigMaps”, “Create & consume Secrets”.

ConfigMap / Secret --env / envFrom-->  variable  (fixed from container start)
                   --volume--------->  file      (kubelet updates after ~1 min)

Secrets are only base64-encoded, not encrypted. Anyone allowed to get secret can read them.

mkdir -p ~/ckad/tag06 && cd ~/ckad/tag06
k create ns tag06
k config set-context --current --namespace=tag06

Drill 1: ConfigMaps three ways

k create configmap notes-cfg --from-literal=LOG_LEVEL=info --from-literal=PORT=8080
printf 'motd=Willkommen bei notes-api\n' > app.properties
k create configmap notes-file --from-file=config.properties=app.properties
printf 'COLOR=blue\n# Kommentar\nOWNER=team-a\n' > team.env
k create configmap notes-env --from-env-file=team.env
k get cm notes-file -o jsonpath='{.data}{"\n"}'
k get cm notes-env -o jsonpath='{.data}{"\n"}'

Expected:

{"config.properties":"motd=Willkommen bei notes-api\n"}
{"COLOR":"blue","OWNER":"team-a"}

--from-file turns the whole file into one key (the name before the = is your choice). --from-env-file turns each line into a key; comments are dropped.

Save your progress? Sign up for free

Drill 2: As a variable and as a file

k run envtest --image=busybox:1.37 $do --command -- sh -c 'sleep 3600' > envtest.yaml

Extend the container in envtest.yaml like this:

apiVersion: v1
kind: Pod
metadata:
  name: envtest
spec:
  containers:
  - name: envtest
    image: busybox:1.37
    command: ["sh", "-c", "sleep 3600"]
    env:
    - name: LEVEL
      valueFrom:
        configMapKeyRef:
          name: notes-cfg
          key: LOG_LEVEL
    envFrom:
    - configMapRef:
        name: notes-env
      prefix: CFG_
    volumeMounts:
    - name: cfg
      mountPath: /etc/notes
  volumes:
  - name: cfg
    configMap:
      name: notes-cfg
k apply -f envtest.yaml
k wait --for=condition=Ready pod/envtest
k exec envtest -- env | grep -E 'LEVEL|CFG_' | sort
k exec envtest -- ls /etc/notes

Expected:

CFG_COLOR=blue
CFG_OWNER=team-a
LEVEL=info
LOG_LEVEL
PORT

env fetches one key under a name of your choice, envFrom all keys (optionally with a prefix), and the volume stores each key as a file.

Save your progress? Sign up for free

Drill 3: Who notices a change?

k patch configmap notes-cfg -p '{"data":{"LOG_LEVEL":"debug"}}'

Wait about a minute, then:

k exec envtest -- cat /etc/notes/LOG_LEVEL; echo
k exec envtest -- printenv LEVEL

Expected:

debug
info

The file is new, the variable is not – environment variables are only set at startup. In the test it took a good 70 seconds (kubelet sync interval plus cache). For variables, only a restart helps, e.g. k rollout restart deployment ….

Save your progress? Sign up for free

Drill 4: Create and read Secrets

k create secret generic notes-key --from-literal=api-key=lab-key-123
k get secret notes-key -o yaml | grep -A1 '^data:'
k get secret notes-key -o jsonpath='{.data.api-key}' | base64 -d; echo

Expected:

data:
  api-key: bGFiLWtleS0xMjM=
lab-key-123

lab-key-123 is a lab value only. In a manifest, stringData saves you the encoding:

cat <<'EOF' | k apply -f -
apiVersion: v1
kind: Secret
metadata:
  name: db-cred
type: Opaque
stringData:
  username: notes
  password: lab-pass-456
EOF
k get secret db-cred -o jsonpath='{.data}{"\n"}'

Expected:

{"password":"bGFiLXBhc3MtNDU2","username":"bm90ZXM="}

The API server stores stringData encoded under data. You can encode it yourself with echo -n lab-pass-456 | base64 – without -n, a newline ends up in the value.

Save your progress? Sign up for free

Drill 5: TLS and registry Secrets

openssl req -x509 -newkey rsa:2048 -nodes -days 30 -subj "/CN=notes.lab" \
  -keyout tls.key -out tls.crt
k create secret tls notes-tls --cert=tls.crt --key=tls.key
k create secret docker-registry lab-reg --docker-server=172.16.0.126:30500 \
  --docker-username=dev --docker-password=lab-reg-pass
k get secret

Expected (shortened):

NAME        TYPE                             DATA   AGE
db-cred     Opaque                           2      2s
lab-reg     kubernetes.io/dockerconfigjson   1      0s
notes-key   Opaque                           1      3s
notes-tls   kubernetes.io/tls                2      1s

The type determines the keys: tls.crt/tls.key (for Ingress, Day 16) and .dockerconfigjson. A pod uses the registry Secret via spec.imagePullSecrets: [{name: lab-reg}]; the lab registry does not need one.

Save your progress? Sign up for free

Drill 6: notes-api needs its key

k create deployment notes --image=172.16.0.126:30500/notes-api:1.0 --port=8080
k get pods -l app=notes
k logs deploy/notes

Expected (after about 20 s):

NAME                     READY   STATUS   RESTARTS      AGE
notes-677dbfcdd4-v5g2h   0/1     Error    2 (16s ago)   20s
time=2026-11-18T15:37:17.879Z level=ERROR msg="start abgebrochen" err="NOTES_API_KEY
oder NOTES_API_KEY_FILE fehlt"

Good: an application without its mandatory configuration should fail loudly instead of running halfway. Feed in the key from the Secret, imperatively:

k set env deploy/notes --from=secret/notes-key --prefix=NOTES_
k get deploy notes -o jsonpath='{.spec.template.spec.containers[0].env}{"\n"}'
k rollout status deploy/notes

Expected:

Warning: key api-key transferred to API_KEY
deployment.apps/notes env updated
[{"name":"NOTES_API_KEY","valueFrom":{"secretKeyRef":{"key":"api-key",
"name":"notes-key"}}}]
deployment "notes" successfully rolled out

k set env turns api-key into a valid variable name and sets secretKeyRef – the value does not appear in the Deployment. Now create a note, once with a wrong key:

k exec deploy/notes -- wget -qO- --header 'X-API-Key: lab-key-123' \
  --post-data '{"text":"Hallo"}' http://localhost:8080/notes
k exec deploy/notes -- wget -qO- --header 'X-API-Key: falsch' \
  --post-data '{"text":"x"}' http://localhost:8080/notes

Expected:

{"id":"f9baad70-061a-455b-9782-842940c33a82","text":"Hallo"}
wget: server returned error: HTTP/1.1 401 Unauthorized

Save your progress? Sign up for free

Drill 7: The key as a file

Variables easily end up in crash dumps and env output. That is why notes-api can also read the key from a file (NOTES_API_KEY_FILE).

k delete deploy notes
k create deployment notes --image=172.16.0.126:30500/notes-api:1.0 --port=8080 \
  $do > notes.yaml

Extend the container in notes.yaml:

    spec:
      containers:
      - name: notes-api
        image: 172.16.0.126:30500/notes-api:1.0
        ports:
        - containerPort: 8080
        env:
        - name: NOTES_API_KEY_FILE
          value: /etc/notes/secret/api-key
        volumeMounts:
        - name: key
          mountPath: /etc/notes/secret
          readOnly: true
      volumes:
      - name: key
        secret:
          secretName: notes-key
k apply -f notes.yaml
k rollout status deploy/notes
k logs deploy/notes | head -1

Expected:

time=2026-11-18T15:37:54.644Z level=INFO msg="api key loaded" source=file

Now rotate the key:

k create secret generic notes-key --from-literal=api-key=lab-key-456 $do \
  | k replace -f -

After about a minute:

k exec deploy/notes -- cat /etc/notes/secret/api-key; echo
k exec deploy/notes -- wget -qO- --header 'X-API-Key: lab-key-456' \
  --post-data '{"text":"x"}' http://localhost:8080/notes

Expected:

lab-key-456
wget: server returned error: HTTP/1.1 401 Unauthorized

The file is new, but notes-api only reads it at startup. Only a restart picks up the key:

k rollout restart deploy/notes
k rollout status deploy/notes
k exec deploy/notes -- wget -qO- --header 'X-API-Key: lab-key-456' \
  --post-data '{"text":"x"}' http://localhost:8080/notes

Expected: {"id":"…","text":"x"}

Save your progress? Sign up for free

Drill 8: Immutable Secrets

k patch secret notes-key -p '{"immutable":true}'
k create secret generic notes-key --from-literal=api-key=lab-key-789 $do \
  | k replace -f -
k patch secret notes-key -p '{"immutable":false}'

Expected:

secret/notes-key patched
The Secret "notes-key" is invalid:
* immutable: Forbidden: field is immutable when `immutable` is set
* data: Forbidden: field is immutable when `immutable` is set
The Secret "notes-key" is invalid: immutable: Forbidden: field is immutable when
`immutable` is set

The way back is blocked – only deleting and recreating helps. In return, the kubelet no longer has to watch immutable objects (which takes load off the API server). This also applies to ConfigMaps.

Save your progress? Sign up for free

Exercise: Speed Drills

  1. Secret db with user=notes and pass=lab-pass-456; pod db-client (busybox:1.37, sleep 3600) gets pass as variable DB_PASS. Target time: 2 min
  2. ConfigMap html from a file index.html; pod web (nginx:1.29) serves it under /usr/share/nginx/html. Target time: 2 min
  3. Print the username from Secret db in plain text. Target time: 20 s

Solution

k create secret generic db --from-literal=user=notes --from-literal=pass=lab-pass-456
k run db-client --image=busybox:1.37 $do --command -- sleep 3600 > db-client.yaml
    env:
    - name: DB_PASS
      valueFrom:
        secretKeyRef:
          name: db
          key: pass
echo '<h1>tag06</h1>' > index.html
k create configmap html --from-file=index.html
k run web --image=nginx:1.29 $do > web.yaml
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
  volumes:
  - name: html
    configMap:
      name: html
k get secret db -o jsonpath='{.data.user}' | base64 -d; echo

Expected: notes

Save your progress? Sign up for free

Checkpoint

k exec db-client -- printenv DB_PASS

Expected: lab-pass-456

k exec web -- curl -s localhost

Expected: <h1>tag06</h1>

k get secret notes-key -o jsonpath='{.immutable}{"\n"}'

Expected: true

Challenge

A colleague rolls out broken. The pod never starts. Find the cause and fix the Deployment without changing the Secret.

k create deployment broken --image=172.16.0.126:30500/notes-api:1.0 $do > broken.yaml

Add this under the container in broken.yaml and apply it:

        env:
        - name: NOTES_API_KEY
          valueFrom:
            secretKeyRef:
              name: notes-key
              key: apikey
k apply -f broken.yaml

Hint: The status is not CrashLoopBackOff – the container is never even created.

Solution

k get pods -l app=broken
k get events --field-selector reason=Failed | tail -1

Expected (shortened):

NAME                      READY   STATUS                       RESTARTS   AGE
broken-5c58d874bc-qjdhv   0/1     CreateContainerConfigError   0          10s
7s   Warning   Failed   pod/broken-5c58d874bc-qjdhv   Error: couldn't find key apikey
in Secret tag06/notes-key

The key is called api-key. Fix it:

k set env deploy/broken --from=secret/notes-key --prefix=NOTES_
k rollout status deploy/broken

Expected: deployment "broken" successfully rolled out. k set env overwrites the existing variable NOTES_API_KEY with the correct secretKeyRef.

Clean up:

k config set-context --current --namespace=default
k delete ns tag06

Save your progress? Sign up for free

Exam Tip

  • k create configmap|secret … $do generates YAML in seconds; do not mix up --from-env-file and --from-file.
  • CreateContainerConfigError almost always means: a ConfigMap, Secret or key is missing. optional: true on the reference makes it optional.
  • Docs: kubernetes.io → Tasks → Configure Pods → “Configure a Pod to Use a ConfigMap” and “Distribute Credentials Securely Using Secrets”.

Enterprise Note

  • Encrypt Secrets in etcd with an EncryptionConfiguration (KMS provider).
  • Keep secrets out of Git: External Secrets Operator, Sealed Secrets or Vault (CSI or agent injector) fetch them from the central vault.
  • Keep read access to Secrets tight via RBAC (Day 9) – list is already enough to read them.
  • Plan for rotation: apps re-read files, or a controller (e.g. Reloader) restarts them on change.