How to fix namespaces not found in Kubernetes
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- List all available namespaces
- Create the missing namespace
- Check kubeconfig context namespace
- Verify YAML manifests reference existing namespaces
kubectl get namespaceskubectl get ns # Shorter formnamespaces 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
- 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)
- Kubeconfig context configured with a non-existent default namespace
- YAML manifest references a namespace that doesn't exist
- Connected to wrong Kubernetes cluster (different clusters may have different namespaces)
- Namespace was created in a different cluster than you're connected to
How to fix "namespaces not found"
See what namespaces exist in your cluster:
kubectl get namespaces
kubectl get ns # Shorter formThis shows all namespaces. Look for the one you're trying to use. If it's missing, you need to create it.
If the namespace doesn't exist, create it:
kubectl create namespace <namespace-name>Example:
kubectl create namespace myappVerify it was created:
kubectl get namespace myapp
kubectl describe namespace myappView your current kubeconfig:
kubectl config viewLook for the contexts section. Find your current context:
kubectl config current-contextThen 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:
kubectl config set-context <context-name> --namespace=<valid-namespace>Check your deployment manifests:
cat deployment.yaml | grep namespaceLook for metadata.namespace field. Ensure it points to a real namespace:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
namespace: myapp-ns # This must exist
spec:
# ...Create namespace if needed:
kubectl create namespace myapp-nsEnsure you're on the right cluster:
kubectl cluster-info
kubectl config current-context
kubectl config get-contexts # List all contextsIf you need to switch clusters:
kubectl config use-context <different-context>Different clusters may have different namespaces. Check what namespaces exist in each cluster.
To avoid typing -n <namespace> every time, set a default:
kubectl config set-context --current --namespace=<namespace-name>Now commands default to that namespace:
kubectl get pods # Uses default namespace
kubectl get pods -n other-ns # Override with explicit -nVerify the change:
kubectl config view | grep namespaceThe kubens utility makes namespace switching easier. Install it:
# macOS
brew install kubectx
# Linux
git clone https://github.com/ahmetb/kubectx /opt/kubectx
sudo ln -s /opt/kubectx/kubens /usr/local/bin/kubensThen use it:
kubens # Interactive menu to choose namespace
kubens myapp-ns # Switch to specific namespace
kubens - # Switch to previous namespaceBest practice: include namespace creation in your deployment manifests:
---
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:
kubectl apply -f manifests.yamlThe namespace will be created first, then other resources deployed into it.
In automated deployments, create namespace before deploying resources:
#!/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 $NAMESPACEAlternatively, include Namespace in your manifests and apply everything:
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
- 📚Kubernetes Official Documentation
- 📖K8s: Namespaces(Official Docs)
- 🐙K8s GitHub Issues(GitHub)
- 💬SO: K8s Namespaces(Stack Overflow)