Sub topic 12
Using the synced Secret in a Pod
This chapter assumes you can read Pod manifests and that an ExternalSecret named db-credentials already produces a Kubernetes Secret of the same name in the app namespace, with a password key. You should also know how a kubelet mounts volumes.
The synced Secret is an ordinary Secret
ESO writes a native Kubernetes Secret. A Pod does not need to know ESO exists. It references the Secret by name, as it would any other Secret. Two things matter for the workload: the Secret must exist before the container starts, and the way you consume it decides whether rotated values ever reach the process.
Environment variable and volume
The manifest below consumes the same Secret in both ways.
apiVersion: v1
kind: Pod
metadata:
name: app
namespace: app
spec:
containers:
- name: app
image: busybox:1.36
command: ["sh", "-c", "sleep 3600"]
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-credentials
key: password
volumeMounts:
- name: creds
mountPath: /etc/creds
readOnly: true
volumes:
- name: creds
secret:
secretName: db-credentialsApply it, then check both paths:
kubectl exec -n app app -- printenv DB_PASSWORD
kubectl exec -n app app -- cat /etc/creds/passwordBoth commands print the same value at first.
How rotation reaches the workload
Rotation takes several steps, and each one adds delay.
- Environment variables are read once, when the container starts. A changed Secret has no effect on a running container. You must restart the Pod.
- Mounted volumes are updated by the kubelet after the Secret changes. The update is not instant. It depends on the kubelet sync period and its cache, so expect a delay of up to a minute or more on top of
refreshInterval. subPathmounts never update. If you mount a single key withsubPath, the file stays at its original value until the Pod restarts.
A mounted file changing does not mean the application sees it. The process must re-read the file, for example on each connection attempt or on a reload signal. Many applications read configuration once at startup and ignore later changes.
Choosing an approach
| Approach | Picks up rotation | Typical use |
|---|---|---|
| `env` with `secretKeyRef` | No, needs restart | Simple apps that read config at startup |
| Volume mount | Yes, files change | Apps that re-read files, or that use a reload sidecar |
| `subPath` mount | No, needs restart | Placing one file in a directory with other files |
If your application only reads environment variables, rotation requires a rollout. Tools such as Reloader can watch a Secret and restart the Deployment when it changes. They are separate from ESO.
Common errors
CreateContainerConfigErrorwithsecret "db-credentials" not found. The ExternalSecret has not synced yet, or it failed. Runkubectl get externalsecret -n appand check that the status isSecretSynced. Debugging is covered in a later chapter.couldn't find key password in Secret. The key insecretKeyRefdoes not match thesecretKeyin the ExternalSecret. Key names are case-sensitive.- Wrong namespace. A Pod can only use Secrets from its own namespace. The ExternalSecret and the Pod must live in the same one.
Exercise
A Deployment reads DB_PASSWORD from an environment variable via secretKeyRef. The password is rotated in the provider, and ESO updates the Kubernetes Secret after the refreshInterval. The running Pods still use the old password. Why, and what are two ways to fix it?
Show answer
Environment variables are set only when the container starts, so a running container never sees the new Secret value. Fix it by restarting the Pods, for example with kubectl rollout restart deployment, or by an automated tool such as Reloader. Alternatively, mount the Secret as a volume (not with subPath) and make the application re-read the file.