Kubernetes 'couldn't find key in Secret': How to Fix It
The event 'couldn't find key API_KEY in Secret' means the Secret exists, but the key named in secretKeyRef.key is not one of its keys. Secret keys are case-sensitive and often use different spelling than the env var. List the Secret's real keys with kubectl describe secret and correct the key in the Deployment.
What you see
kubectl get pods -l app=web
NAME READY STATUS RESTARTS AGE
web-7b5c9d6f4c-qz8lm 0/1 CreateContainerConfigError 0 40s
The container never starts, so kubectl logs shows nothing useful. The reason
is in the Events.
Step 1: read the Events
kubectl describe pod -l app=web
Warning Failed 5s (x4 over 40s) kubelet Error: couldn't find key API_KEY in Secret default/app-secret
This tells you three things: the Secret name (app-secret), its namespace,
and the key the pod asked for (API_KEY). The Secret itself was found -
otherwise the error would be secret "app-secret" not found.
Step 2: list the keys the Secret really has
kubectl describe secret app-secret
Data
====
api-key: 12 bytes
describe shows key names and sizes but hides the values. To print all keys
as JSON:
kubectl get secret app-secret -o jsonpath='{.data}'
Here the Secret has api-key, while the pod asks for API_KEY. Keys are
case-sensitive, and - and _ are different characters.
Step 3: correct secretKeyRef.key
The env var name and the Secret key are two separate things. The env var name is what your app reads; the key is what the Secret stores:
env:
- name: API_KEY # the env var the app reads - keep it
valueFrom:
secretKeyRef:
name: app-secret # the Secret
key: api-key # was: API_KEY - must match a real key
Apply and wait for the new pod:
kubectl apply -f deployment.yaml
kubectl rollout status deploy/web --timeout=90s
Step 4: verify inside the container
kubectl exec deploy/web -- printenv API_KEY
If it prints the value, the reference is correct. Do not paste real secret values into shared chat or tickets while you debug.
Should you change the Deployment or the Secret?
Either side can be wrong. Change the Deployment when the Secret is shared or managed elsewhere (other apps may use the same key). Change the Secret when it was created by hand with a typo and nothing else uses it:
kubectl create secret generic app-secret \
--from-literal=API_KEY='value' \
--dry-run=client -o yaml | kubectl apply -f -
Why apply does not catch this
Kubernetes does not check Secret names or keys when you apply a Deployment. The kubelet checks them when it creates the container. So a wrong key applies cleanly and only fails on the pod. After every apply, check pod status and Events, not only the apply output.
Making a key optional
If the app can run without the value, mark the reference optional:
secretKeyRef:
name: app-secret
key: api-key
optional: true
The pod starts and the env var is simply not set when the key is missing. Use this only for truly optional settings.
Prevent it in the future
- Use the same key names in every environment (dev, staging, production).
- When a tool such as External Secrets or Sealed Secrets generates the Secret, check which key names it writes.
- If you want every key as an env var with the same name, use
envFromwithsecretRefinstead of listing keys one by one.
Do this project yourself. It's ready in a live cloud workspace in your browser - when you finish, it lands on your portfolio.
Start this project - freeWhat you'll practice
- Reading a couldn't find key in Secret event with kubectl describe pod
- Listing a Secret's keys with kubectl describe secret and jsonpath
- Correcting secretKeyRef.key in a Deployment and verifying with printenv
FAQ
What does 'couldn't find key in Secret' mean in Kubernetes?
The Secret exists, but it has no key with the name in secretKeyRef.key. Run kubectl describe secret <name> to list its real keys and correct the reference.
How do I see the keys in a Kubernetes Secret?
kubectl describe secret <name> lists the key names and sizes without the values. kubectl get secret <name> -o jsonpath='{.data}' prints keys with base64-encoded values.
Are Kubernetes Secret keys case-sensitive?
Yes. API_KEY, api_key, and api-key are three different keys.
Does the env var name have to match the Secret key?
No. The name of the env var can be anything your app reads. Only secretKeyRef.key must match a key in the Secret.
Keep learning
Do this project yourself. It's ready in a live cloud workspace in your browser - when you finish, it lands on your portfolio.
Start this project - free