Sub topic 10
Using Secrets in Pods
This chapter assumes you can write Pod manifests, know what a Secret is, and have already consumed a ConfigMap as an environment variable and as a volume. Here you apply the same two patterns to Secrets and add a third, imagePullSecrets.
Secrets as environment variables
Use secretKeyRef to copy one key into one variable. Kubernetes decodes the base64 value before it reaches the container, so the process sees plain text.
Secrets as mounted files
A secret volume writes each key as a file. Kubernetes stores the contents in a memory-backed tmpfs, so they are not written to the node's disk. defaultMode sets the file permissions. Mounting read-only is a good habit.
apiVersion: v1
kind: Pod
metadata:
name: db-client
spec:
containers:
- name: client
image: busybox:1.36
command: ["sh", "-c", "ls /etc/db && sleep 3600"]
env:
- name: DB_USER
valueFrom:
secretKeyRef:
name: db-credentials
key: username
volumeMounts:
- name: creds
mountPath: /etc/db
readOnly: true
volumes:
- name: creds
secret:
secretName: db-credentials
defaultMode: 0400This Pod gets DB_USER as a variable and two files, /etc/db/username and /etc/db/password, each readable only by their owner. To mount only some keys, add an items list, as you did for ConfigMaps.
Choosing between the two
| Aspect | Environment variable | Mounted file |
|---|---|---|
| Picks up Secret changes | No, needs a Pod restart | Yes, after a delay |
| Exposure risk | Appears in `kubectl describe` output of the Pod spec, crash dumps, and child processes | Limited by file permissions |
| Best for | Simple values the app reads at startup | Certificates, key files, rotating credentials |
Mounted files update through the same kubelet sync that updates ConfigMap volumes. A mount that uses subPath never updates.
Pulling images from a private registry
Container registries that require login need a Secret of type kubernetes.io/dockerconfigjson. Create it, then reference it from the Pod.
kubectl create secret docker-registry regcred \
--docker-server=registry.example.com \
--docker-username=app \
--docker-password='S3cr3t!'apiVersion: v1
kind: Pod
metadata:
name: private-app
spec:
imagePullSecrets:
- name: regcred
containers:
- name: app
image: registry.example.com/team/app:1.0The kubelet uses this credential only to pull the image. The application inside the container never sees it. To avoid repeating the field in every Pod, add imagePullSecrets to a ServiceAccount, and every Pod that uses that account inherits it.
How a Pod reaches a Secret
Common errors
- A Secret or key that does not exist leaves the Pod in
CreateContainerConfigError. Check the Events section ofkubectl describe pod. - A missing Secret used as a volume keeps the Pod in
ContainerCreating. Settingoptional: truelets the Pod start without it. - A Secret lives in one namespace. A Pod can only reference Secrets in its own namespace.
- Wrong or missing registry credentials show up as
ImagePullBackOff.
Exercise
A Pod reads a database password from an environment variable using secretKeyRef. You update the Secret with a new password. The Pod keeps running. Which password does the application see, and what are two ways to make it use the new one?
Show answer
The application still sees the old password, because environment variables are fixed when the container starts. You can restart the Pod, for example with kubectl rollout restart deployment. Or you can mount the Secret as a volume and have the application re-read the file, which updates in a running Pod after a short delay (unless the mount uses subPath).