kubectx | kubens
Efficient context and namespace switching is essential for DevOps engineers working with multiple Kubernetes clusters across AWS, Azure, GCP, or hybrid environments. kubectx and kubens streamline this workflow, making it easy to manage clusters and namespaces from the command line.
What are kubectx and kubens?
- kubectx: Quickly switch between multiple Kubernetes cluster contexts with a single command.
- kubens: Instantly switch the active namespace for your current context.
Both tools are invaluable for engineers managing dev, staging, and production clusters, or working with multiple cloud providers.
Installation
macOS (Homebrew):
brew install kubectx
Linux (Debian/Ubuntu):
sudo apt-get install kubectx
Or install via GitHub:
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/kubens
NixOS (declarative):
Add to your /etc/nixos/configuration.nix:
environment.systemPackages = with pkgs; [ kubectx kubens ];
Then run:
sudo nixos-rebuild switch
Usage Examples
Switch Kubernetes Context
kubectx dev-cluster
Switch Namespace
kubens devops-tools
Now, all kubectl commands will use the selected context and namespace by default, without needing --context or --namespace flags.
Real-World DevOps Example
Scenario: You manage multiple clusters (dev, staging, prod) across AWS EKS and GCP GKE. Use kubectx and kubens to quickly switch between them:
kubectx aws-prod
kubens monitoring
kubectl get pods
Aliases (if you don’t want to install extra tools)
You can achieve similar functionality with bash aliases:
alias kubens='kubectl config set-context --current --namespace '
alias kubectx='kubectl config use-context '
Best Practices
- Use
kubectxandkubensto avoid mistakes when working with multiple clusters/namespaces. - Add context/namespace info to your shell prompt for safety (see kube-ps1).
- Store your kubeconfigs securely and use tools like aws-iam-authenticator for cloud auth.
References
Tip: Integrate
kubectxandkubensinto your shell profile or tmux for even faster context switching in cloud-native workflows.