Practical Guide β kubectx & kubens
Managing multiple Kubernetes clusters and namespaces with plain kubectl can get repetitive.
Two handy tools β kubectx and kubens β simplify switching contexts and namespaces.
1. π§ Installation
Linux / macOS
# Install both with brew
brew install kubectx
# Or manually
git clone https://github.com/ahmetb/kubectx.git ~/.kubectx
sudo ln -s ~/.kubectx/kubectx /usr/local/bin/kubectx
sudo ln -s ~/.kubectx/kubens /usr/local/bin/kubensWindows (with Chocolatey)
choco install kubectx2. βοΈ Setup & Verify
Ensure kubectl works with clusters:
kubectl config get-contextsThen test:
kubectx --help
kubens --help3. π§ Usage
π Switch Contexts (kubectx)
- List available contexts:
kubectx- Switch to a context:
kubectx my-cluster- Rename a context:
kubectx old-name=new-name- Switch back to previous context:
kubectx -π Switch Namespaces (kubens)
- List all namespaces:
kubens- Switch to namespace:
kubens dev- Switch back to previous namespace:
kubens -4. π Common Workflows
Example: Change Context + Namespace
kubectx production
kubens paymentsNow all kubectl commands run in production cluster, payments namespace.
5. π¨ Bonus Features
- Fuzzy search support if
fzfis installed β lets you pick context/namespace interactively.
kubectx
kubensOpens an interactive menu to choose from.
- Works seamlessly with
k9sβ if you already use it.
6. β Summary
kubectxβ switch between clusters/contexts easily.kubensβ switch between namespaces quickly.- Both save time and reduce human error vs typing full
kubectl configcommands.
β‘ These tools are must-haves for anyone managing multiple Kubernetes clusters/namespaces daily.
| Feature | kubectl | kubectx/kubens |
|------------------------|------------------------------|-------------------------------|
| Context Switching | `kubectl config use-context` | `kubectx <context>` |
| Namespace Switching | `kubectl config set-context --namespace` | `kubens <namespace>` |
| List Contexts | `kubectl config get-contexts`| `kubectx` |
| List Namespaces | `kubectl get namespaces` | `kubens` |
| Interactive Selection | No | Yes (with fzf) |