kubectx — Fast Kubernetes Context & Namespace Switching
Switch between Kubernetes clusters and namespaces instantly with kubectx and kubens, essential tools for multi-cluster workflows.
What it is
kubectx is a pair of CLI tools (kubectx for cluster contexts, kubens for namespaces) that simplify switching between Kubernetes clusters and namespaces. Instead of typing kubectl config use-context my-long-cluster-name, you type kubectx my-cluster. Tab completion and fuzzy matching make navigation even faster across many clusters.
DevOps engineers and developers working with multiple Kubernetes clusters benefit most from kubectx. If you manage staging, production, and development clusters, kubectx eliminates the friction of context switching.
How it saves time or tokens
kubectx saves seconds on every context switch, which adds up to minutes per day for multi-cluster operators. The previous context shortcut (kubectx -) lets you toggle between two clusters like cd - in a shell. kubens provides the same speed for namespace switching, avoiding the verbose kubectl config set-context commands.
How to use
- Install kubectx via Homebrew or download the script
- Run
kubectxto list contexts orkubectx <name>to switch - Run
kubensto list namespaces orkubens <name>to switch
Example
# Install
brew install kubectx
# List all contexts
kubectx
# Output: dev-cluster, staging-cluster, prod-cluster
# Switch context
kubectx prod-cluster
# Switched to context 'prod-cluster'
# Switch back to previous
kubectx -
# Switched to context 'dev-cluster'
# Switch namespace
kubens monitoring
# Context 'dev-cluster' set to namespace 'monitoring'
# Interactive fuzzy selection (with fzf)
kubectx # launches fzf picker if installed
Related on TokRepo
- DevOps tools — Browse Kubernetes and infrastructure tools
- Featured workflows — Discover top-rated workflows
Common pitfalls
- kubectx modifies your kubeconfig context; running commands in another terminal will use the switched context too
- Renaming contexts with
kubectx new=oldis convenient but can confuse automation that relies on original names - kubens only works within the current context; switch context first if you need a different cluster's namespace
Frequently Asked Questions
kubectx switches between Kubernetes cluster contexts (which cluster kubectl talks to). kubens switches between namespaces within the current context (which namespace kubectl targets by default). Both ship together in the kubectx package.
Yes. If fzf (fuzzy finder) is installed, running kubectx without arguments opens an interactive fuzzy picker. This is useful when you have many clusters with similar names.
Yes. Use `kubectx new-name=old-name` to rename a context. This is helpful for giving short, memorable names to clusters with auto-generated long names from cloud providers.
Yes. kubectx works with any kubeconfig file, regardless of the Kubernetes distribution. EKS, GKE, AKS, k3s, minikube, and kind are all supported.
kubectx is safe in that it only modifies which context is active in your kubeconfig. However, switching to a production context means subsequent kubectl commands affect production. Use caution and consider visual indicators like kube-ps1 to show the current context in your prompt.
Citations (3)
- kubectx GitHub— Fast Kubernetes context and namespace switching
- kubectx README— kubectx and kubens CLI utilities
- Kubernetes Documentation— Kubernetes context management via kubeconfig
Related on TokRepo
Discussion
Related Assets
Flax — Neural Network Library for JAX
A high-performance neural network library built on JAX, providing a flexible module system used extensively across Google DeepMind and the JAX research community.
PyCaret — Low-Code Machine Learning in Python
An open-source AutoML library that wraps scikit-learn, XGBoost, LightGBM, CatBoost, and other ML libraries into a unified low-code interface for rapid experimentation.
DGL — Deep Graph Library for Scalable Graph Neural Networks
A high-performance framework for building graph neural networks on top of PyTorch, TensorFlow, or MXNet, designed for both research prototyping and production-scale graph learning.