Kubectl Set Namespace File
kubectl config get-contexts Now the NAMESPACE column should show my-namespace . kubectl get pods This will list pods only in my-namespace , not all namespaces. Important notes | Feature | Behavior | |---------|----------| | Works on | Current context only | | Persistent | Yes — saved in kubeconfig | | Overwrites | Existing namespace in current context | | No effect on | Existing resources, deployments, services | Examples Set namespace for current context:
❌ → In older versions (<1.19), you needed --current . Modern versions assume --current by default. kubectl set namespace
Namespace set to "my-namespace". kubectl config view --minify | grep namespace or kubectl config get-contexts Now the NAMESPACE column should
kubectl set namespace dev-env
kubectl set namespace --current <namespace> 1. Check current namespace context kubectl config get-contexts Look under the NAMESPACE column for your current context. If empty, it means default namespace is used. 2. Change to a different namespace kubectl set namespace my-namespace Output example: Modern versions assume --current by default
❌ → Make sure your context is set first with kubectl config use-context . When to use kubectl set namespace ✅ You frequently work in one namespace and are tired of -n flag. ✅ You’re switching between namespaces often. ✅ You want to avoid accidentally running commands in the wrong namespace. Would you like a comparison with kubens (from kubectx) or tips for scripting namespace changes?