Kubernetes: Secrets
A Secret is a place to store a password or a key for pods to use. It keeps sensitive values out of the image, but base64 encoding is not encryption.
A Secret holds a password
A Secret stores values in base64. The value here is s3cret-pass, encoded. Anyone who can read the Secret can decode that value in one step.
apiVersion: v1
kind: Secret
metadata:
name: db-password
type: Opaque
data:
password: czNjcmV0LXBhc3M=Base64 is encoding, not encryption
Base64 only changes how the value looks. It hides nothing from anyone who can read it. A real cluster needs encryption at rest for Secrets.
echo -n 's3cret-pass' | base64Give the password to a pod
A pod reads a Secret key as an env var. The password never sits in the image or the pod YAML. It lives in the Secret and the pod reads it at run time.
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-password
key: passwordExample
kubectl apply -f db-secret.yaml
kubectl exec -it web-7d9f4b6c8d-abcde -- printenv DB_PASSWORDKey points
- Secret: a named set of sensitive values that pods can read
- Use a Secret for passwords, keys and tokens, never for plain settings
- Use base64 encoding in a Secret, and know that anyone can decode it
- Use a Secret as an env var or as a mounted file, like a ConfigMap
- Sealed secret: a Secret that stays encrypted until it reaches the cluster.
- Use sealed secrets as the honest next step for storing Secrets in Git
In this example, the first command creates a Secret called db-password from the file. The second command reads the DB_PASSWORD variable inside the web pod. It shows s3cret-pass, which proves the pod received its password from the Secret.
Note
a Secret is only as safe as who can read it, so keep access tight
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
Why is a Kubernetes Secret not safe on its own for a real password?