Issue#
After deploying an application, the pod never starts. It doesn't crash, it just sits in CreateContainerConfigError:
$ kubectl get pods -n app
NAME READY STATUS RESTARTS AGE
api-6d9f7c8b5d-x2lkq 0/1 CreateContainerConfigError 0 3m
The pod events show one of these messages:
Warning Failed 12s (x15 over 3m) kubelet Error: secret "db-credentials" not found
Warning Failed 12s (x15 over 3m) kubelet Error: configmap "app-config" not found
Warning Failed 12s (x15 over 3m) kubelet Error: couldn't find key DB_PASSWORD in Secret app/db-credentials
Cause#
The container spec references a Secret or ConfigMap through env.valueFrom or envFrom, and the kubelet cannot resolve it before starting the container. The usual reasons are:
- The Secret/ConfigMap was never created (for example, it is created by a separate pipeline or Helm chart that hasn't run yet).
- It exists, but in a different namespace. Secrets and ConfigMaps are namespaced, and a pod can only read the ones in its own namespace.
- It exists, but the key name in
secretKeyRef/configMapKeyRefdoesn't match. Key names are case-sensitive.
Solution#
- Find exactly what the pod is missing:
kubectl describe pod api-6d9f7c8b5d-x2lkq -n app | sed -n '/Events:/,$p'
- Check whether the object exists in the pod's namespace, and look for it in other namespaces too:
kubectl get secret db-credentials -n app
kubectl get secrets -A --field-selector metadata.name=db-credentials
- If it is missing, create it in the correct namespace:
kubectl create secret generic db-credentials -n app \
--from-literal=DB_USER=api \
--from-literal=DB_PASSWORD='S3cr3t!'
Or, if it already exists in another namespace, copy it over (requires jq):
kubectl get secret db-credentials -n staging -o json \
| jq 'del(.metadata.namespace,.metadata.uid,.metadata.resourceVersion,.metadata.creationTimestamp,.metadata.ownerReferences)' \
| kubectl apply -n app -f -
- If the error is
couldn't find key, list the keys the Secret actually contains:
kubectl describe secret db-credentials -n app
Then make the reference in the Deployment match one of those keys exactly:
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-credentials
key: DB_PASSWORD
- Restart the pods. The kubelet retries on its own, but restarting them makes them pick up the fix immediately:
kubectl rollout restart deployment/api -n app
Tip: If the value is genuinely optional, add
optional: trueundersecretKeyRef,configMapKeyRef,secretReforconfigMapRef. The container will then start even when the object or key is missing.
Verify#
kubectl rollout status deployment/api -n app
kubectl get pods -n app
kubectl exec -n app deploy/api -- printenv DB_USER
The pods should show Running, and the variable should print its value instead of an error.