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.
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.
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/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.
Service port already allocated
How to fix "Service port already allocated" in Kubernetes
minimum cpu usage per Container
How to fix "minimum cpu usage per Container" in Kubernetes
Failed to connect to server: connection refused (HTTP/2)
How to fix "HTTP/2 connection refused" error in Kubernetes
No subnets found for EKS cluster
How to fix "eks subnet not found" in Kubernetes
missing request for cpu in container
How to fix "missing request for cpu in container" in Kubernetes HPA