Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

Kubernetes CreateContainerConfigError: secret "x" not found (or configmap "x" not found)

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

A pod that sits in CreateContainerConfigError, with the event Error: secret "db-credentials" not found or Error: configmap "app-settings" not found, is waiting for a Secret or ConfigMap that does not exist in the pod's own namespace. The kubelet looks the object up while it builds the container's environment variables and stops there. Create the object in the right namespace, or correct the name in the workload. The Secrets page says the kubelet periodically retries a pod whose Secret could not be fetched.

What the error means

Before the kubelet asks the container runtime to create a container, it builds the container's configuration. That includes the environment variables from env entries that use secretKeyRef or configMapKeyRef, and from envFrom. In the kubelet source at tag v1.37.3, each lookup uses the pod's own namespace (pod.Namespace) and returns the error unless the reference is marked optional. The ConfigMap page says the same about placement: "The Pod and the ConfigMap must be in the same namespace."

The same source (with the reason string defined in pkg/kubelet/events/event.go) then records a warning event with the reason Failed and the message Error: followed by the error text, and reports the container status CreateContainerConfigError.

The Secrets page says the kubelet's default strategy for its Secret cache is Watch. With that strategy the kubelet source builds the not-found error from the singular resource name, so the event reads:

Error: secret "db-credentials" not found
Error: configmap "app-settings" not found

With another cache strategy the text may differ, because the lookup then goes through other code. An object that exists but lacks the named key produces a different message, with ConfigMap in place of Secret for a ConfigMap:

Error: couldn't find key api-key in Secret shop/db-credentials

Two rules from the documentation matter here. By default a Secret is required: "None of a Pod's containers will start until all non-optional Secrets are available." And a reference marked optional: true is ignored when the object is missing ("If an optional Secret doesn't exist, Kubernetes ignores it").

A Secret or ConfigMap used as a volume goes a different way. A reading of the kubelet source is that a failure there is reported in an event beginning MountVolume.SetUp failed, from the volume code and not from the container-configuration code above. That fault does not show CreateContainerConfigError.

Likely causes

Cause What you see How to confirm
The Secret or ConfigMap was never created, or is created after the workload secret "x" not found, often right after a deployment kubectl get secret x -n shop answers that it was not found
It exists in a different namespace The same message, while the object is visible elsewhere kubectl get secrets --all-namespaces lists the name under another namespace
The name is misspelt in the pod spec The same message, and a similar name exists Compare the name in the workload with the list of names
The object exists but the key does not couldn't find key ... in Secret ns/name kubectl describe secret x -n shop lists the key names

Prove the cause before changing anything

These checks only read. They do not print a Secret's contents; a Deployment's YAML may hold literal values, so read it before pasting it anywhere.

  1. Read the events of the stuck pod. The Debug Pods page makes this the first step:
kubectl describe pod web-6d5f9c7b8-x2k4q -n shop

The warning at the bottom gives the object type and name, or the key. A pod in the Waiting state is scheduled and cannot start, which is what the page describes.

  1. Look for the object in the pod's namespace:
kubectl get secret db-credentials -n shop
kubectl get configmap app-settings -n shop

A good result lists the object. A bad result is a not-found answer.

  1. If it is not there, look in every namespace for the same name:
kubectl get secrets --all-namespaces
  1. If it is there but the key is the problem, list the key names. The Managing Secrets page says kubectl get and kubectl describe "avoid showing the contents of a Secret by default", so this is safe to run and paste:
kubectl describe secret db-credentials -n shop

Avoid kubectl get secret ... -o yaml for this check, because its output carries the values.

  1. Read what the workload refers to. A Deployment's YAML normally holds names, but it can carry literal value: entries, so keep the output private:
kubectl get deployment web -n shop -o yaml

Find secretKeyRef, configMapKeyRef and envFrom, and compare each name and key with steps 2 to 4.

Fix

Which fix applies depends on step 2 to step 5. If the object is missing, create it (fix A). If the reference is wrong, correct the workload (fix B). Marking the reference optional: true hides the message and leaves the variable unset (the ConfigMap task page says the configuration "will be empty"), so use it only where the application runs without the value.

Fix A: create the missing Secret or ConfigMap

First save the current state:

kubectl get secrets -n shop > secrets-before.txt
kubectl get configmaps -n shop > configmaps-before.txt

Risk: the next step creates the missing Secret or ConfigMap in the namespace shop, and every pod there that refers to it starts using its values when the kubelet retries.

Create it from the real values, which come from whoever owns the application. This example reads a Secret's values from a file named db.env:

kubectl create secret generic db-credentials -n shop --from-env-file=db.env

For a ConfigMap, apply its manifest instead, for example kubectl apply -f app-settings.yaml. The file db.env holds the values in plain text. Removing it is for the owner of the values, once they are stored in the owner's password store.

To undo it: delete only the object you just created, then compare with the saved list.

kubectl delete secret db-credentials -n shop
kubectl get secrets -n shop

For a ConfigMap, run kubectl delete configmap app-settings -n shop and then kubectl get configmaps -n shop. The listing should match the saved file (secrets-before.txt or configmaps-before.txt). Pods that start after the delete fail again with the same message.

Fix B: correct the reference in the workload

First save the current state:

kubectl get deployment web -n shop -o yaml > web-deployment-before.yaml
kubectl rollout history deployment/web -n shop

Note the current revision number from the history output (called N below). The saved file may hold literal values, so keep it private.

Disruptive step: the next command edits the pod template of the Deployment web, so the Deployment replaces its pods with new ones.

Open the Deployment in the default editor and correct the name or key:

kubectl edit deployment/web -n shop

Then wait for the rollout:

kubectl rollout status deployment/web -n shop

To back out: return to the revision noted before the edit (replace N with that number), wait for it, and check that the pod template and the env references match the saved file. Fields such as resourceVersion and generation will differ.

kubectl rollout undo deployment/web -n shop --to-revision=N
kubectl rollout status deployment/web -n shop
kubectl get deployment web -n shop -o yaml

Example (illustrative): the Deployment web in namespace shop refers to the Secret db-credentials. The new pod web-6d5f9c7b8-x2k4q, scheduled on k8s-node1, shows CreateContainerConfigError, and kubectl describe pod ends with:

Type     Reason  Age                From     Message
----     ------  ----               ----     -------
Warning  Failed  8s (x4 over 41s)   kubelet  Error: secret "db-credentials" not found

kubectl get secret db-credentials -n shop answers that it was not found, and kubectl get secrets --all-namespaces lists db-credentials in the namespace staging. The Secret was created in the wrong namespace. The engineer saves the list of Secrets in shop, creates db-credentials in shop with fix A, and within a retry cycle the pod starts. The copy in staging is left alone.

Confirm the fix

  1. kubectl get pods -n shop shows the pod Running.
  2. kubectl describe pod shows no new Failed event with Error: after the change.
  3. For fix B, kubectl rollout status reports that the rollout is complete.

If the container now starts and then exits, the fault has moved on to a different state. See Kubernetes CrashLoopBackOff: find out why the container keeps exiting. If the pod cannot pull its image, see Kubernetes ImagePullBackOff and ErrImagePull: read the pod events, then fix the image or the credentials.

What to watch afterwards

  • Apply Secrets and ConfigMaps before the workloads that use them. The Secrets page says a Secret "needs to be created before any Pods that depend on it".
  • When manifests are copied between environments, check the namespace of every Secret and ConfigMap in the set, since a pod only reads its own namespace.
  • In a pipeline, follow each deployment with kubectl rollout status, which watches the rollout until it is done. Its --timeout option sets how long to wait before the watch ends.
  • Do not add optional: true to silence a failing start. The application then runs without the value.

Risks and limits

  • These commands have not been run or lab-tested.
  • The message texts were read from the kubelet source at tag v1.37.3. The documentation pages give no version range for them, so other releases may word them differently.
  • The kubelet code at v1.37.3 covers environment variables. A missing object used as a volume produces a different event and is not covered.
  • Deleting the object you created in fix A does not undo anything the application did while it ran with the values.
  • Never paste Secret values into a ticket or a chat. Use kubectl describe secret, which lists names and sizes only.

Sources