Kubernetes · Chapter 10 of 28

Secrets

By the end of this chapter you can keep passwords and keys in Secrets, hand them to a container as environment variables or files, connect the orders service to its PostgreSQL with a password it never sees in a manifest, read why a pod cannot find a Secret, and say who can read one and what base64 does not do. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Running apps
  • 4 tasks
  • Kubernetes
  • In the all labs plan

The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.

Why you need this

Orders is the service that loses Jhatpat money when it stops: at ₹412 a basket, the 2,300 orders that failed last Dhanteras were close to ₹9.5 lakh. Orders needs its database password; payments needs the card gateway's key, which can move money; notifier needs the SMS gateway's token. Tushar's first manifests had the password written in them, and those manifests are in git, where every developer and every laptop that clones the repository can read them.

A ConfigMap (chapter 9) would take the values out of the manifests, but a ConfigMap is meant to be read: anyone allowed to see the app's settings sees it, kubectl describe prints its values, and nothing about it says "handle with care". Kubernetes keeps sensitive values in a separate kind, the Secret, so the cluster can treat them differently: who may read them, whether they are encrypted in etcd, where the kubelet puts them on a node.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    orders.yaml: orders' database URL from a Secret

    Give orders its database URL from a Secret.

    1. Show the Secret postgres-auth as YAML. Decode the user, the password and the database from base64.

    2. Make a Secret named orders-db with one key, url. Its value is postgres://<user>:<password>@postgres:5432/<database>, with the three decoded values in it.

    3. Open orders.yaml with nano. Give the orders container an env var DATABASE_URL from the key url of orders-db, with secretKeyRef.

    4. Apply orders.yaml, and read the new orders pod's logs.

    To pass: orders-db has the right URL as plain text (not base64), DATABASE_URL comes from orders-db with secretKeyRef, and every ready orders pod logs that it connected to the database.

  2. Task 2

    payments.yaml: the gateway key as a file

    Give payments its gateway key as a file from a Secret.

    1. Make a Secret named payments-gateway with one key, gateway-key. Its value is exactly demo_gw_LKO_7c41b9e2

    2. Open payments.yaml with nano. Add a volume from that Secret to the pod.

    3. Mount the volume in the payments container at the folder /etc/payments. The file is then /etc/payments/gateway-key.

    4. Apply payments.yaml, and read the new payments pod's logs.

    To pass: payments-gateway has that exact key and value, the Deployment mounts it, and every ready payments pod logs that it loaded the key from /etc/payments/gateway-key (it ends ...b9e2).

  3. Task 3

    why.txt: why notifier is stuck, and the fix

    Find out why the notifier pod never starts, and fix it.

    1. List the pods. Read the notifier pod's STATUS.

    2. Describe the notifier pod, and read its last events.

    3. Copy the kubelet's reason (the message after Error:, as kubectl prints it) into why.txt, below the notes.

    4. Fix it, in notifier.yaml or in the Secret, and apply. Keep SMS_TOKEN coming from the Secret sms-gateway, with secretKeyRef.

    To pass: why.txt has the kubelet's reason, SMS_TOKEN still comes from sms-gateway with the token that is in it now (demo-sms-5521), and a notifier pod is ready.

  4. Task 4 · interview question

    Interview: who can read a Secret

    Interview question: who can read the Secret postgres-auth, and what really protects it?

    1. Say who can read a Secret in the namespace. Think of what the API server checks before it gives one out.

    2. Say what base64 protects the value from. You decoded it yourself in task 1.

    3. Write one or two sentences of your own below the notes in answer.txt.

    To pass: answer.txt says what decides who may read a Secret, and what base64 does (and does not do) for the value.

What you will learn

  • Secrets: an object for passwords, tokens and keys; type Opaque.
  • base64: how data stores values, and why it is not encryption; stringData for plain text.
  • Making one: kubectl create secret generic, or YAML.
  • As env vars and as files: secretKeyRef and a secret volume.
  • A database password from a Secret, and the real error when it is wrong.
  • Registry Secrets: kubernetes.io/dockerconfigjson and imagePullSecrets.
  • Who can read a Secret: RBAC and encryption at rest.

More in Running apps

Build "Secrets" yourself

Jhatpat delivers groceries in ten minutes from 14 dark stores across Lucknow, and its hand-run servers fall over on festival evenings. You move it onto Kubernetes. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.