Secrets: passwords and keys
Like a ConfigMap, handled with more care.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/k8s-fund/secrets.tar.gz && tar -xzf secrets.tar.gz, which creates ~/lab/secrets/. SHA-256: aaaed3c85d6cea618282e7f20e9ed2d32ac694416e036f6a1bcc18bfe9204ebaThe ConfigMap in lesson 11 held a colour. Now the app also needs a database password. You could put it in the same ConfigMap, but ConfigMaps are meant to be read freely, by people and by tools. Kubernetes has a separate object for values like this, the Secret.
A Secret works almost exactly like a ConfigMap. The differences are in how the cluster handles it: access to Secrets can be locked down on its own, a mounted Secret is kept off the node's disk, and the cluster can encrypt Secrets where it stores them. What a Secret does not do, by default, is encrypt anything, and more people can read it than you might expect.
Create a Secret without a file
--from-literal puts the value straight into the command, so no file with the password ends up on your disk or in a Git repository by accident. (It does end up in your shell history. Real teams fetch the value from a password manager or let a tool create the Secret.)
generic makes a Secret of type Opaque, meaning "any keys you like". describe is careful: it shows only how many bytes each value has, 13 for the password and 3 for the username, never the values themselves.
Use it in a Pod
A Pod reads a Secret the same two ways as a ConfigMap, as a variable or as files:
apiVersion: v1kind: Podmetadata:name: clientspec:containers:- name: clientimage: busybox:1.38.0command: ["sh", "-c", "trap 'exit 0' TERM; while true; do sleep 1; done"]env:- name: DB_USER # one key as an environment variablevalueFrom:secretKeyRef:name: db-loginkey: usernamevolumeMounts:- name: db # every key as a file in /etc/dbmountPath: /etc/dbreadOnly: truevolumes:- name: dbsecret:secretName: db-login
The variable DB_USER holds app, and /etc/db has one file per key, holding the plain value. The files are links through ..data, just like a ConfigMap volume, so a mounted Secret also updates after a change while a variable does not. The last command shows where the files live: a tmpfs, which is memory, mounted read-only. The kubelet keeps Secret volumes in memory so the values are never written to the node's disk.
base64 is not encryption
describe hid the values, but anyone allowed to read the Secret object gets them in full:
The stored value looks scrambled, but it is only base64, a way to write any bytes as plain letters. base64 -d turns it straight back into the password. No key is needed, and none is involved. Treat a Secret's YAML exactly like the password itself: never commit it to Git and never paste it into a ticket.
Who can read Secrets
So what protects a Secret is the API server deciding who may read it. That decision is made by RBAC (role-based access control), which gets its own course later in the track. Two quick checks with kubectl auth can-i, which asks the API server "would this be allowed?":
You are the cluster admin, so yes. The default service account, the identity every Pod in the namespace gets unless told otherwise, gets no. That is good: an app that is broken into cannot simply list the namespace's Secrets.
Permission to read Secrets is not the whole story, though. This Role lets a user called dev create and read Pods, and nothing more:
# A user called "dev" may run Pods and read their logs in this namespace. Nothing about Secrets.apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata:name: pod-makerrules:- apiGroups: [""]resources: ["pods", "pods/log"]verbs: ["create", "get", "list", "watch"]---apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata:name: dev-pod-makersubjects:- kind: Username: devapiGroup: rbac.authorization.k8s.ioroleRef:kind: Rolename: pod-makerapiGroup: rbac.authorization.k8s.io
--as=dev lets the admin act as dev to test this. Reading the Secret is Forbidden, as expected. But dev may create Pods, and a Pod can mount any Secret in its namespace. So dev writes one that prints the password:
# Created by "dev", who cannot read Secrets through the API.apiVersion: v1kind: Podmetadata:name: peekspec:restartPolicy: Nevercontainers:- name: peekimage: busybox:1.38.0command: ["cat", "/s/password"]volumeMounts:- name: smountPath: /svolumes:- name: ssecret:secretName: db-login
Apply it as dev, then run kubectl get pod peek --as=dev until STATUS shows Completed, which takes a few seconds, and read its log:
The password came back through the Pod's log, read by a user who was refused the Secret a moment earlier. Kubernetes does not check whether the person creating a Pod may read the Secrets it mounts. In practice, anyone who can create Pods (or Deployments, or Jobs) in a namespace can read every Secret in it. Put Secrets that need different readers in different namespaces.
Encryption at rest
The API server stores every object in etcd, the cluster's database. By default Secrets are stored there unencrypted, so a copy of the etcd data or of a backup holds every password. The fix is encryption at rest: the cluster admin gives the API server an encryption configuration, ideally backed by a key management service, and from then on Secrets are encrypted before they reach etcd. It is a setting on the API server, not something you switch on per Secret, so this lab does not change it. Managed clouds often turn it on for you. Check yours.
Hands-on
1. Create a second Secret: kubectl create secret generic api-key --from-literal=token='lab-token-123'.
2. Read it back and decode it: kubectl get secret api-key -o jsonpath='{.data.token}' | base64 -d; echo.
3. Check whether dev may list Secrets: kubectl auth can-i list secrets --as=dev.
4. Clean up with kubectl delete pod client peek, kubectl delete -f pod-maker.yaml and kubectl delete secret db-login api-key, then check with kubectl get secrets.
What you should see: step 1 prints secret/api-key created, and step 2 prints exactly lab-token-123. Step 3 answers no, the same as for get, even though dev read a Secret through the peek Pod. Step 4 deletes two Pods, the Role, the RoleBinding and two Secrets, and kubectl get secrets ends with No resources found in default namespace.
Remember
A Secret is base64, not encryption. It is protected by who may read it and by who may create Pods in its namespace, and encrypting it in etcd is a separate cluster setting you have to check.
base64 -d returned the password with no key at all.kubectl get secret db-login but may create Pods in the same namespace. Can they read the password?dev got the password.dev had only create, get, list and watch on Pods, plus Pod logs.peek Pod printed the password to its log, created by dev.peek. Both ways expose the value.tmpfs /etc/db mounted read-only.