Skip to main content

A Secret Mounted With subPath Never Receives the Rotated Value, so Pods Keep Using the Old Credential

High severityKubernetes About 20 minutes to fix

Overview

The Secret shows the new password, yet the Pod still reads the old one. subPath mounts are not updated, so a restart must finish every rotation.

At a glance

Symptom
After a rotation the Secret holds the new password while one Deployment's Pods still read the old one.
Likely cause
A subPath mount sits outside the kubelet's update mechanism, so the file the container sees is the one written when the container started.
Impact
A stale mount leaves the credential you meant to retire live in running containers.
Verification signal
A rotation is complete when no running Pod holds the old value and the old credential no longer works.
Safe correction
Restart the workload after the Secret edit, with both credentials valid, and revoke the old one last.
Rollback or recovery
Because the old credential is still valid at step 3, rolling back is cheap.

Rotating a credential held in a Kubernetes Secret looks finished the moment kubectl accepts the new value. If a container mounts that Secret with subPath, it is not finished. Kubernetes documents that a container using a Secret as a subPath volume mount does not receive automated Secret updates, so the Pod carries on reading the old value until it is recreated.

The fix is to restart the workload after every rotation, in an order that keeps the old credential valid until the new one is proven, and then to stop using subPath for anything that rotates.

Symptom

After a rotation the Secret holds the new password while one Deployment’s Pods still read the old one. The new database password went into the Secret app-db and kubectl get secret shows it. Pods that mount the Secret as a whole volume pick the change up on their own, after a delay. One Deployment never does. Its container still reads the old password from /etc/app/db-password, and nothing in its status says anything is wrong.

The trouble surfaces later. If you revoke the old password on the database, the next time those Pods open a connection they fail to authenticate and may drop into CrashLoopBackOff. If you do not revoke it, the credential you meant to retire stays live in running containers.

Why a Secret edit looks safe

The Kubernetes documentation says that when a volume contains Secret data and the Secret is updated, Kubernetes tracks this and updates the data in the volume, using an eventually-consistent approach. A reasonable reader concludes that every file mounted from the Secret follows the edit. The documentation then adds a note that carves out one case, and it is easy to miss because the Secret and the Pod both look correct.

The subPath mount is usually chosen for a good reason. Mounting the volume directly at /etc/app would hide the other files in that directory, so someone adds subPath: db-password to place a single key as a single file. The Pod starts, the application works, and the choice is never revisited.

Root cause

A subPath mount sits outside the kubelet’s update mechanism, so the file the container sees is the one written when the container started.

For a Secret mounted as a normal volume, the kubelet keeps a cache of the current keys and values and projects changes into the Pod. By default it detects changes through an API watch. The documented delay from the Secret update to the new keys appearing in the Pod can be as long as the kubelet sync period plus the cache propagation delay. The ConfigMap page carries the same note for subPath mounts of a ConfigMap.

Even a mount that does update only changes the file; an application that reads the file once at start-up needs a restart anyway, which is a second reason to plan one.

Impact

A stale mount leaves the credential you meant to retire live in running containers. A rotation exists to make an exposed credential useless, and a stale mount means the old one is still in use somewhere you did not check. Rotating after a leak and then reading a clean change record gives a false sense that the exposure window has closed.

The opposite failure is an outage. Teams that revoke the old credential straight after editing the Secret break every Pod that still holds it. The failure may appear on the next restart or node drain, long after anyone connects it to the rotation.

How to confirm which Pods hold the old value

Three read-only checks settle it. First, find out whether any container mounts the Secret through subPath:

kubectl -n shop get deployment app -o jsonpath='{.spec.template.spec.containers[*].volumeMounts}'

Any entry with a subPath field whose volume is backed by the Secret is affected. Next, hash the value held by the Secret. Values in the data field are base64-encoded, so decode before hashing and never print the password itself:

kubectl -n shop get secret app-db -o jsonpath='{.data.password}' | base64 -d | sha256sum

Then hash the file inside a running Pod, using a Pod name from kubectl -n shop get pods:

kubectl -n shop exec app-7c9d8b6f5-abcde -- sha256sum /etc/app/db-password

If the two hashes differ, that Pod is running on the old credential. The container image must provide sha256sum; if it does not, run the check from a temporary debugging container or compare file modification times instead.

The shortcuts that make it worse

Under pressure people reach for three things. The first is leaving the old credential valid indefinitely so nothing breaks, which keeps the secret you wanted gone alive and defeats the rotation. The second is deleting every Pod at once to force a reload, which removes all capacity together and, if the new value is wrong, leaves nothing running on the old one either.

The third is editing the Secret again in the hope that the kubelet will notice. It will not, because the mount type is the cause, not the timing.

Correction

Restart the workload after the Secret edit, with both credentials valid, and revoke the old one last. The rule is to have both credentials valid at the same moment, move the Pods across, prove it, and only then retire the old one. Work in a non-production namespace first, and confirm the database supports two valid passwords or two accounts at once.

  1. Create the new credential beside the old one. On the database, create the new password or a second account while the old one still works. Precondition: you can restore the old Secret value from your secret store. Rollback: remove the new credential; nothing in the cluster has changed yet.
  2. Update the Secret. Edit app-db with your usual process, for example from a manifest in version control. Keep the previous manifest revision so the old value can be restored.
  3. Restart the workload. Run kubectl -n shop rollout restart deployment/app. This restarts the Deployment’s Pods through a rolling update, so only some are down at a time. Stop condition: if new Pods do not become ready, stop and go to the rollback section.
  4. Wait for the rollout. Run kubectl -n shop rollout status deployment/app. It watches the latest rollout until it is done.
  5. Check the new Pods, then retire the old credential. Repeat the hash comparison against a new Pod. Once it matches and the application is healthy, revoke the old credential on the database.

Following along? Tap a step’s number to tick it off.

Repeat the restart for every other workload that mounts the same Secret with subPath, including CronJobs and sidecars, which the first check will list if you run it for each namespace.

Validation

A rotation is complete when no running Pod holds the old value and the old credential no longer works. Confirm the first by running the hash comparison on every Pod of the Deployment, not just one. Confirm the second by attempting a connection with the old credential from a test client; it should be refused. Only then close the change record.

Rollback

Because the old credential is still valid at step 3, rolling back is cheap. Restore the previous Secret value from version control or your secret store, then run kubectl -n shop rollout restart deployment/app again so the Pods read it. Check the result with kubectl -n shop rollout status deployment/app. Never revoke the old credential until the new Pods have been healthy and hashed correctly.

If the old credential has already been revoked and Pods are failing, the rollback is to recreate it on the database first, then restore the Secret value and restart. That sequence is why revoking comes last.

How to prevent it coming back

Do not use subPath for any Secret that rotates. Mount the Secret as a normal volume at its own directory, for example /etc/app-secrets, and point the application at the file there.

If the application needs a different file name or directory layout, the Secret volume’s items field changes the path of each key, so a key can be projected to a chosen path without subPath. A normal volume mount follows later edits of the Secret on its own, though the application still has to re-read the file.

For credentials that should not change in place, Kubernetes lets you mark a Secret immutable. An immutable Secret cannot be edited or reverted to mutable, so a rotation means creating a new Secret with a new name and pointing the Deployment at it, which triggers a normal rollout. Keep file permissions in view as well: the fsGroup behaviour on hostPath volumes and the hostPath readOnly split are the neighbouring ways a volume mount does less than its spec implies.

Finally, add a restart to your rotation runbook as a numbered step, and record which Deployments mount which Secret. If a Pod does fall over after a rotation, the CrashLoopBackOff reference covers reading its events and logs. Confirm version-specific kubelet behaviour on the Kubernetes Secrets page for your release.

3

Apply the safer control

Before you change production

Confirm the affected scope, export the current configuration, and test the replacement control in a non-production environment first.

Fix commands and configuration

kubectl
subPath
app-db
4

When to escalate

Validation and rollback are covered in the sections above.

Escalate

Escalate when the blast radius is uncertain, the control cannot be tested safely, or remediation requires an outage or security exception.

The KBY Brief

Fixed? Get the next one before it hits you: The KBY Brief

Practical fixes for the systems you run, by email about twice a week. Unsubscribe at any time.

About this page

Credited to

Dates

Published

Corrections

No corrections are listed for this page. Some review sweeps corrected pages without naming each one; those are in the corrections log.

Was this useful?

Reports go to the site owner and are used to correct pages. Corrections are listed at /corrections.

Found this useful? Follow KBY for more like it.

After remediation

Further reading stays below the corrective workflow and is selected by platform, category and shared technical keywords.

More from KBY that goes with this config trap.

Discover more

Connected KBY resources