Sub topic 7
What a Secret is and what base64 does not do
This chapter assumes you can use kubectl, read YAML manifests, and know what a Pod and a namespace are. You should also know how a ConfigMap holds data. A Secret has the same shape, but it is meant for sensitive values such as passwords, tokens, and keys.
What a Secret is
A Secret is a namespaced API object that stores small pieces of sensitive data. Like a ConfigMap, it holds key-value pairs and can be consumed by Pods as environment variables or files. It differs in how Kubernetes treats it:
- The kubelet keeps Secret volumes in memory-backed storage (tmpfs) on the node, so they are not written to the node's disk.
- Secrets can be given their own RBAC rules, separate from ConfigMaps.
- Secrets can be encrypted at rest in etcd, if you enable it.
- Tools and
kubectl describehide the values by default.
A Secret is limited to 1 MiB, the same as a ConfigMap.
Anatomy: data and stringData
A Secret has two fields for its values.
dataholds values that are already base64-encoded.stringDatais a write-only convenience. You supply plain text, and the API server encodes it and stores it underdata.
apiVersion: v1
kind: Secret
metadata:
name: db-credentials
type: Opaque
stringData:
username: app
password: S3cr3t!When you read this object back, stringData is gone. Only data remains, with encoded values. If the same key appears in both fields, stringData wins.
The type: Opaque line marks a generic Secret with arbitrary keys. The next chapter covers other types.
What base64 does not do
Base64 is an encoding. It turns bytes into a text-safe alphabet so binary values fit into JSON and YAML. It uses no key, so anyone can reverse it.
Create the Secret and read the password back:
kubectl create secret generic db-credentials \
--from-literal=username=app \
--from-literal=password='S3cr3t!'
kubectl get secret db-credentials -o jsonpath='{.data.password}' | base64 --decodeThe first command stores the value. The second prints S3cr3t!. Anyone with get permission on that Secret can do the same.
Two common mistakes follow from this:
- Encoding a value with
echo "S3cr3t!" | base64. Theechoadds a trailing newline, which becomes part of the password. Useecho -n, or letstringDataorkubectl create secretdo the encoding. - Committing a
data:manifest to version control and treating it as safe.
Where the protection comes from
The Secret object does not protect a value by itself. The protection comes from controls around it.
| Path to the value | Control |
|---|---|
| `kubectl get secret` or API calls | RBAC rules on `secrets` |
| etcd data files and backups | Encryption at rest |
| Another workload | Namespace scoping and RBAC on Pod creation |
Later chapters cover RBAC and encryption at rest in detail. Remember that anyone who can create a Pod in a namespace can mount any Secret there, so Pod creation rights are also Secret read rights.
Exercise
A teammate writes echo "hunter2" | base64 and pastes the output into the data field of a Secret. The application rejects the password. What is the likely cause, and how would you avoid it?
Show answer
echo appends a newline, so the encoded value decodes to hunter2 followed by a newline. Use echo -n "hunter2" | base64, or use stringData or kubectl create secret generic --from-literal, which encode the value without adding a newline.
Exercise
Why is it unsafe to treat a base64-encoded Secret manifest as protected in a Git repository?
Show answer
Base64 is a reversible encoding with no key. Anyone who can read the file can decode the values with base64 --decode, so the manifest exposes the secrets in plain form.