Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Kubernetes: MountVolume.SetUp failed, secret not found

The kubelet cannot mount a volume because the Secret or ConfigMap is missing, in another namespace, or named differently. Create it, then restart pods.

~5 min readKubernetes
#Kubernetes#Secrets#Volumes#Troubleshooting
A Kubernetes pod crate waiting on a cyan volume mount, an amber secret card missing from its slot

Introduction & Quick Summary

A pod sits in ContainerCreating and the event list repeats:

code
Warning FailedMount kubelet MountVolume.SetUp failed for volume "app-config" : secret "app-config" not found

The kubelet tried to mount the volume that references Secret app-config, asked the API server for it in the pod's namespace, and got a 404. It will retry with backoff, so the pod is not dead, but it will never start until the object exists at that exact name in that exact namespace.

The four causes worth checking first:

  • The Secret does not exist at all, because it is created by a separate manifest that has not applied.
  • It exists in another namespace; Secrets are namespaced and a pod can only mount its own.
  • The name differs: Helm prefixes release names, Kustomize adds suffixes, and a copied YAML keeps the old name.
  • The volume references a key that is missing, which produces a different but related event.

Quick answer: kubectl get secret app-config -n <pod-namespace>. If it is absent, create it in that namespace and the kubelet mounts it on the next retry without recreating the pod. If it exists and the pod still fails, read the next event: the error changes from not found to couldn't find key or permission denied.

Core Mechanics, Signatures & Reproducible Setup

A Secret volume is resolved by the kubelet, not the scheduler. The scheduler only checks that a volume can be satisfied by a node; the kubelet then fetches the Secret through its own API credentials and writes the data into the pod's mount namespace at /var/lib/kubelet/pods/<uid>/volumes/kubernetes.io~secret/<name>.

apiVersion: apps/v1 kind: Deployment metadata: name: api namespace: shop spec: replicas: 1 selector: matchLabels: { app: api } template: metadata: labels: { app: api } spec: containers: - name: api image: ghcr.io/example/api:1.4.0 volumeMounts: - name: app-config mountPath: /etc/api readOnly: true volumes: - name: app-config secret: secretName: app-config defaultMode: 0400 optional: false

Reproduce the failure deliberately to see both events in order:

bash
kubectl -n shop create deployment api --image=nginx kubectl -n shop get pods -w kubectl -n shop describe pod <pod> | sed -n '/Events/,$p'

The event sequence is informative. FailedMount ... not found means the object is missing. FailedMount ... couldn't find key foo in Secret shop/app-config means the object exists and its data does not contain the requested key. FailedMount ... forbidden means the kubelet's access or a policy layer rejects the read.

EventCause
secret "x" not foundobject absent in the pod namespace
couldn't find key k in Secret ns/xitems or secretKeyRef names a missing key
configmap "x" not foundsame failure for a ConfigMap volume
is forbiddenadmission policy or RBAC blocks the kubelet
has no such file or directorymount path conflict with another volume

Key Comparison Table

SourceRefreshes in running podNamespacedBest for
Secret volumeyes, on kubelet syncyescredentials read as files
Secret envFrom or env.valueFromno, values fixed at startyessimple environment configuration
ConfigMap volumeyes, on kubelet syncyesnon-secret configuration files
Projected volumeyesyescombining Secret, ConfigMap, and token
CSI secret storedepends on driveryesexternal vaults, rotation

The choice changes the failure mode. A volume mount retries forever and reflects updates; environment variables are resolved once at container start, so a missing Secret produces CreateContainerConfigError instead of FailedMount, and a fixed Secret requires a pod restart.

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. Check the namespace, not just the name

A Secret in default is invisible to a pod in shop. Describe the pod and read its namespace from the metadata rather than from your current context.

bash
POD=$(kubectl -n shop get pod -l app=api -o jsonpath='{.items[0].metadata.name}') NS=$(kubectl -n shop get pod "$POD" -o jsonpath='{.metadata.namespace}') kubectl -n "$NS" get secret app-config kubectl -n "$NS" get secret app-config -o jsonpath='{.data}' | head -c 200

Listing -A and finding the Secret elsewhere confirms the diagnosis immediately.

2. Generated names from Helm and Kustomize

Templates often render {{ .Release.Name }}-config, so the object is shop-prod-config while the volume still says app-config. Reference the same helper in both places.

{{- define "api.secretName" -}} {{ .Release.Name }}-config {{- end -}} volumes: - name: app-config secret: secretName: {{ include "api.secretName" . }}

For Kustomize, set namePrefix consistently and let nameReference rewrite the volume field, or the prefix applies to the Secret only.

3. A key inside the Secret is missing

The object exists, so the not found message disappears, and a different event takes its place.

volumes: - name: app-config secret: secretName: app-config items: - key: app.yaml # must exist in .data path: app.yaml

Verify the keys before changing the manifest:

bash
kubectl -n shop get secret app-config -o go-template='{{range $k,$v := .data}}{{$k}}{{"\n"}}{{end}}'

Remember that stringData is merged into data on write, so keys created with stringData are present under data when read back.

4. optional: true and the stuck pod

optional: true turns a missing Secret into an empty mount instead of an error. That is useful for feature flags and dangerous for credentials, because the container starts and fails later with a confusing configuration error.

volumes: - name: app-config secret: secretName: app-config optional: true

Use it only when the application can genuinely run with no files at all, and log clearly when the directory is empty.

5. Creating the Secret after the pod

Volume mounts recover automatically: once the Secret exists, the kubelet completes the mount on its next sync, usually within a minute, and the pod proceeds. Environment variables do not recover, because the container spec was already resolved.

bash
kubectl -n shop create secret generic app-config \ --from-file=app.yaml=./config/prod.yaml \ --dry-run=client -o yaml | kubectl apply -f -

Applying with kubectl apply rather than create keeps repeated runs idempotent and avoids AlreadyExists errors in automation.

6. Immutable Secrets and rotation

A Secret marked immutable: true cannot be updated; a new value requires a new object and a pod restart. That protects against accidental edits but breaks rotation scripts that patch in place.

apiVersion: v1 kind: Secret metadata: name: app-config-v2 namespace: shop immutable: true

Rotate by creating a new name, updating the volume reference, and rolling the deployment. Pods that mounted the old object keep the old files until they are recreated.

Common Compilation/Runtime Mistakes & Version Gotchas

  • Editing a Secret and expecting the app to reload. Volume files update, but most applications read configuration once at start. Restart the pods or add a reload trigger.
  • envFrom for optional credentials. A missing key in envFrom is skipped silently unless optional is set deliberately; the container starts without the variable.
  • Admission policies. Pod Security Admission and policy engines can deny Secret volumes. The event then says forbidden, not not found, and the fix is a policy change rather than a new Secret.
  • GitOps ordering. Argo CD and Flux apply manifests in waves; a Deployment before its Secret produces a burst of FailedMount events that resolve on their own. Sync waves or a dependency hint avoid the noise.
  • Namespaced defaults. kubectl create secret without -n writes to the current context namespace, which is the most common way a correct manifest meets an empty namespace.
SymptomCauseFix
CreateContainerConfigErrormissing env sourcecreate Secret or restart pod
FailedMount repeats every few secondsobject absentcreate it in the pod namespace
mounts but app cannot read filedefaultMode too restrictiveset mode readable by the container user
works in dev, fails in proddifferent namespace or prefixuse the same helper for both

Base64 values are a frequent source of quiet mistakes, so decode a suspicious entry with the Base64 tool before comparing it to the source file, and validate the manifest with the YAML formatter to catch indentation that puts secretName under the wrong key.

The behaviour of Secret volumes, including optional and immutability, is documented in Kubernetes Secrets, and the volume type itself is described under Volumes. Both pages are short and cover the exact fields used above.

Official references

primary documentation

The Kubernetes documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Kubernetes work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Kubernetes guides

More in Kubernetes

All Kubernetes guides

The guides filed next to this one in Kubernetes, in library order.