DevOps engineers and Kubernetes platform administrators frequently manage multiple clusters spanning local development environments (such as Minikube or Kind), cloud staging clusters in AWS, GCP, or Azure, and production environments. Standard kubectl commands for switching clusters and namespaces require long, unwieldy commands like kubectl config use-context <context-name> and kubectl config set-context --current --namespace=<namespace>. The open-source companions kubectx and kubens streamline these operations into instant, interactive single-word terminal commands with full interactive fuzzy-finder support.
Why Use kubectx and kubens?
Developed by Ahmet Alp Balkan, these utilities eliminate human error and repetitive keystrokes in daily cloud operations:
- Fast Context Switching: Switch between active Kubernetes clusters simply by typing
kubectx <cluster-name>. - Fast Namespace Switching: Change your current target namespace across deployments with
kubens <namespace-name>. - Toggle to Previous Target: Typing
kubectx -orkubens -instantly returns to your previous context or namespace, mimicking the behavior ofcd -in UNIX shells. - Interactive FZF Search: Integrating with
fzfprovides an interactive, keyboard-navigable search menu directly inside your terminal window.
Step 1: Install kubectx and kubens on Linux
You can install the tools via system package managers or download the standalone shell scripts directly into your execution path:
Method A: Standalone Binary Installation (All Distributions)
# Download the latest kubectx binary
sudo curl -sSLf "https://raw.githubusercontent.com/ahmetb/kubectx/master/kubectx" -o /usr/local/bin/kubectx
sudo chmod +x /usr/local/bin/kubectx
# Download the latest kubens binary
sudo curl -sSLf "https://raw.githubusercontent.com/ahmetb/kubectx/master/kubens" -o /usr/local/bin/kubens
sudo chmod +x /usr/local/bin/kubens
Method B: Install on Arch Linux
sudo pacman -S kubectx
Method C: Install via Homebrew on Linux
brew install kubectx
Method D: Install via krew (kubectl plugin manager)
kubectl krew install ctx
kubectl krew install ns
Step 2: Install fzf for Interactive Fuzzy Filtering
While kubectx and kubens work seamlessly with direct string arguments, installing fzf activates an interactive visual selection menu:
# On Ubuntu / Debian
sudo apt update && sudo apt install fzf -y
# On Fedora / RHEL
sudo dnf install fzf -y
Step 3: Managing Kubernetes Contexts with kubectx
With your ~/.kube/config populated with cluster endpoints:
List Available Contexts
Run kubectx without arguments to display all configured cluster contexts. The currently active context is highlighted in bold yellow font with an accent marker:
kubectx
Switch to a Named Context
kubectx production-cluster-eu-west-1
The active context in your kubeconfig updates immediately.
Switch Back to the Previous Context
To toggle back to the cluster you were working on previously:
kubectx -
Rename a Complex Context
Cloud providers like Google GKE and AWS EKS often generate verbose ARN context names (such as arn:aws:eks:eu-west-1:123456789:cluster/prod-eks). You can rename them to short, memorable aliases:
kubectx prod=arn:aws:eks:eu-west-1:123456789:cluster/prod-eks
Step 4: Managing Kubernetes Namespaces with kubens
Standard kubectl calls require specifying -n <namespace> with every invocation unless you change the context default. kubens streamlines this workflow:
List Active Namespaces
Run kubens without arguments to list all active namespaces within your current cluster:
kubens
Switch Active Namespace
To set your default namespace to ingress-nginx or monitoring:
kubens monitoring
Any subsequent commands (e.g. kubectl get pods or kubectl logs) will execute against the monitoring namespace without requiring the -n flag.
Toggle Back to Previous Namespace
kubens -
Step 5: Configure Shell Completion for Bash and Zsh
To enable Tab-completion for cluster names and namespaces, source the completion scripts in your shell profile:
For Bash (~/.bashrc)
# Download bash completion scripts
sudo curl -sSLf "https://raw.githubusercontent.com/ahmetb/kubectx/master/completion/kubectx.bash" -o /etc/bash_completion.d/kubectx
sudo curl -sSLf "https://raw.githubusercontent.com/ahmetb/kubectx/master/completion/kubens.bash" -o /etc/bash_completion.d/kubens
source ~/.bashrc
For Zsh (~/.zshrc)
# If using Oh My Zsh, add the kubectx plugin
plugins=(... kubectx)
Operational Safety in Production Environments
When working across multi-tenant environments, switching contexts unintentionally can lead to running destructive commands on production infrastructure. Pair kubectx with a dynamic prompt tool like starship or kube-ps1 to prominently display your active Kubernetes cluster and namespace directly within your shell prompt prompt string.