Skip to content
Latchkey

Kubernetes "namespaces \"<name>\" not found" on deploy in CI - Fix it

kubectl tried to create or operate on resources in a namespace that has not been created. Namespaces are not auto-created on apply, so resources targeting a missing namespace are rejected.

What this error means

kubectl apply -n <name> (or objects with that namespace) fails with Error from server (NotFound): namespaces "<name>" not found.

kubectl
Error from server (NotFound): namespaces "staging" not found

Common causes

Namespace never created

The target namespace is referenced but no Namespace object was applied first.

Apply ordering

The Namespace and the workloads are in one bundle but the workloads sort ahead of the Namespace.

How to fix it

Create the namespace first

Ensure the namespace exists before applying namespaced resources.

Terminal
kubectl create namespace staging --dry-run=client -o yaml | kubectl apply -f -
kubectl apply -f manifests/ -n staging

Order namespace ahead of workloads

  1. Include the Namespace object and apply it before dependent resources.
  2. With kustomize/Helm, ensure the namespace is created in the same release.
  3. Use --namespace consistently across the pipeline.

How to prevent it

  • Always provision the namespace as the first step of a deploy.
  • Keep a Namespace manifest in the bundle (or helm install --create-namespace).
  • Pin the namespace via a single CI variable.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →