kubectx
kubectx is a tool to switch between Kubernetes contexts (clusters) quickly. It also includes kubens for switching between namespaces.
Prerequisites
- kubectl - Must be installed
- Configuration - Cluster access configured
Installation
Official installation guide: github.com/ahmetb/kubectx
Interactive Mode
Install fzf alongside kubectx for interactive fuzzy-search selection of contexts and namespaces. See the Interactive Mode documentation.
- Windows
- macOS
- Linux
# Using Chocolatey
choco install kubectx
# Using scoop
scoop install kubectx
Or download from GitHub Releases.
# Using Homebrew (includes kubens)
brew install kubectx
# Using snap
sudo snap install kubectx --classic
# Using apt (Ubuntu 20.04+)
sudo apt install kubectx
Verify Installation
kubectx --help
kubens --help
kubectx Usage
Switch between Kubernetes contexts (clusters):
# List all contexts (current is highlighted)
kubectx
# Switch to a specific context
kubectx my-cluster
# Switch to previous context
kubectx -
kubens Usage
kubens changes the default namespace for kubectl commands, so you don't have to specify -n <namespace> every time.
Example
# Without kubens - must specify namespace each time
kubectl get pods -n demo-app
kubectl get services -n demo-app
kubectl logs my-pod -n demo-app
# With kubens - set namespace once
kubens demo-app
kubectl get pods # returns pods in demo-app namespace
kubectl get services # returns services in demo-app namespace
kubectl logs my-pod # gets logs from demo-app namespace
Common Commands
# List all namespaces (current is highlighted)
kubens
# Switch to a specific namespace
kubens demo-app
# Switch to previous namespace
kubens -
Related
- kubectx GitHub - Source code and full documentation
- fzf - Fuzzy finder for interactive mode
- kubectl - Kubernetes CLI