kubectx and kubens
Managing multiple Kubernetes clusters and namespaces with raw kubectl context commands is tedious. kubectx and kubens are lightweight tools that make switching fast and intuitive.
Installation
# macOS with Homebrew
brew install kubectx
# This installs both kubectx and kubens
# Verify installation
kubectx --help
kubens --help
Switching Contexts with kubectx
# List all available contexts
kubectx
# Switch to a context
kubectx production-cluster
# Switch back to the previous context
kubectx -
# Rename a context for convenience
kubectx prod=arn:aws:eks:us-east-1:123456:cluster/production
# Delete a context
kubectx -d old-cluster
# Show current context
kubectx -c
Switching Namespaces with kubens
# List all namespaces
kubens
# Switch to a namespace
kubens kube-system
# Switch back to the previous namespace
kubens -
# Show current namespace
kubens -c
fzf Integration
When fzf (fuzzy finder) is installed, kubectx and kubens provide interactive selection menus:
# Install fzf
brew install fzf
# Now kubectx and kubens launch interactive selection
kubectx # Opens fuzzy search for contexts
kubens # Opens fuzzy search for namespaces
Type a few characters to filter the list and press Enter to select. This is invaluable when managing dozens of clusters.
Shell Prompt Integration
Display the current context and namespace in your shell prompt:
# Add to ~/.zshrc or ~/.bashrc
# Using kube-ps1
source "/opt/homebrew/opt/kube-ps1/share/kube-ps1.sh"
PROMPT='$(kube_ps1) '$PROMPT
This prevents accidental operations on the wrong cluster by making the active context always visible.