How to fix namespaces not found in Kubernetes

KubernetesBEGINNERLOWUpdated Dec 6, 2025· 175 views

The "namespaces not found" error occurs when kubectl tries to access a Kubernetes namespace that doesn't exist. This typically happens due to typos in namespace names, misconfigured kubeconfig contexts, or the namespace being deleted.

Quick fix

Fixes namespaces not found

kubectl get namespaces
kubectl get ns  # Shorter form
  1. List all available namespaces
  2. Create the missing namespace
  3. Check kubeconfig context namespace
  4. Verify YAML manifests reference existing namespaces
kubernetes — error
kubectl get namespaces
kubectl get ns # Shorter form
namespaces not found

What this error means

When you run kubectl commands targeting a non-existent namespace, you get "Error from server (NotFound): the server could not find the requested resource". Kubernetes organizes resources into namespaces (like folders), and all resources are scoped to a namespace. Attempting to access a namespace that doesn't exist fails at the API server level. This is different from resource not found (like "deployment not found")—this is the namespace itself that doesn't exist in the cluster.

Typical symptoms

  • Commands like `kubectl get pods -n <namespace>` fail with "namespaces not found"
  • Deployments refuse to apply to non-existent namespaces
  • Context switching fails when the context references a deleted namespace
  • kubectl get namespaces doesn't show the expected namespace
  • CI/CD pipelines fail during resource deployment steps
  • kubectl commands work in default namespace but fail when specifying custom namespace
  • Kubeconfig context point to a namespace that no longer exists

Common causes

  1. Namespace was never created in the cluster
  2. Namespace was deleted by another user or automated cleanup
  3. Typo or misspelling in namespace name (Kubernetes names are case-sensitive)
  4. Kubeconfig context configured with a non-existent default namespace
  5. YAML manifest references a namespace that doesn't exist
  6. Connected to wrong Kubernetes cluster (different clusters may have different namespaces)
  7. Namespace was created in a different cluster than you're connected to

How to fix "namespaces not found"

1List all available namespaces

See what namespaces exist in your cluster:

bash
kubectl get namespaces
kubectl get ns  # Shorter form

This shows all namespaces. Look for the one you're trying to use. If it's missing, you need to create it.

2Create the missing namespace

If the namespace doesn't exist, create it:

bash
kubectl create namespace <namespace-name>

Example:

bash
kubectl create namespace myapp

Verify it was created:

bash
kubectl get namespace myapp
kubectl describe namespace myapp
3Check kubeconfig context namespace

View your current kubeconfig:

bash
kubectl config view

Look for the contexts section. Find your current context:

bash
kubectl config current-context

Then find the context definition in the output. It may specify a namespace. If that namespace doesn't exist, either:
- Create the namespace
- Update the context to use a valid namespace:

bash
kubectl config set-context <context-name> --namespace=<valid-namespace>
4Verify YAML manifests reference existing namespaces

Check your deployment manifests:

bash
cat deployment.yaml | grep namespace

Look for metadata.namespace field. Ensure it points to a real namespace:

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  namespace: myapp-ns  # This must exist
spec:
  # ...

Create namespace if needed:

bash
kubectl create namespace myapp-ns
5Verify you're connected to correct cluster

Ensure you're on the right cluster:

bash
kubectl cluster-info
kubectl config current-context
kubectl config get-contexts  # List all contexts

If you need to switch clusters:

bash
kubectl config use-context <different-context>

Different clusters may have different namespaces. Check what namespaces exist in each cluster.

6Set namespace as default in kubeconfig

To avoid typing -n <namespace> every time, set a default:

bash
kubectl config set-context --current --namespace=<namespace-name>

Now commands default to that namespace:

bash
kubectl get pods  # Uses default namespace
kubectl get pods -n other-ns  # Override with explicit -n

Verify the change:

bash
kubectl config view | grep namespace
7Use kubens tool for easier switching

The kubens utility makes namespace switching easier. Install it:

bash
# macOS
brew install kubectx

# Linux
git clone https://github.com/ahmetb/kubectx /opt/kubectx
sudo ln -s /opt/kubectx/kubens /usr/local/bin/kubens

Then use it:

bash
kubens  # Interactive menu to choose namespace
kubens myapp-ns  # Switch to specific namespace
kubens -  # Switch to previous namespace
8Pre-create namespace in YAML before deploying resources

Best practice: include namespace creation in your deployment manifests:

yaml
---
apiVersion: v1
kind: Namespace
metadata:
  name: myapp
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
  namespace: myapp  # Reference the namespace created above
spec:
  # ...

Apply the manifest:

bash
kubectl apply -f manifests.yaml

The namespace will be created first, then other resources deployed into it.

9For CI/CD pipelines, ensure namespace exists

In automated deployments, create namespace before deploying resources:

bash
#!/bin/bash
NAMESPACE="myapp"

# Create namespace if it doesn't exist
kubectl create namespace $NAMESPACE --dry-run=client -o yaml | kubectl apply -f -

# Deploy resources
kubectl apply -f deployment.yaml -n $NAMESPACE

Alternatively, include Namespace in your manifests and apply everything:

bash
kubectl apply -f manifests/

Advanced notes

Namespaces are logical isolation boundaries in Kubernetes—multiple teams can use the same cluster with separate namespaces. By default, kubectl uses the "default" namespace if not specified. Namespace names must follow DNS rules (lowercase letters, hyphens, no special characters, max 63 chars). Some system namespaces ("kube-system", "kube-public", "kube-node-lease") are created automatically. When deleting a namespace, all resources in it are deleted too. For multi-tenant clusters, use ResourceQuotas and NetworkPolicies per namespace to enforce limits. In GitOps workflows, ensure namespace creation precedes resource deployment in the pipeline order. WSL2 and Docker Desktop share the same Kubernetes instance, so namespaces are cluster-wide, not machine-specific.

Frequently asked questions

What does "namespaces not found" mean?
When you run kubectl commands targeting a non-existent namespace, you get "Error from server (NotFound): the server could not find the requested resource". Kubernetes organizes resources into namespaces (like folders), and all resources are scoped to a namespace. Attempting to access a namespace that doesn't exist fails at the API server level.
How do I fix "namespaces not found"?
The "namespaces not found" error occurs when kubectl tries to access a Kubernetes namespace that doesn't exist. This typically happens due to typos in namespace names, misconfigured kubeconfig contexts, or the namespace being deleted.
What causes "namespaces not found"?
Namespace was never created in the cluster Namespace was deleted by another user or automated cleanup Typo or misspelling in namespace name (Kubernetes names are case-sensitive)
Is the "namespaces not found" error serious?
It is a low-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading