kubectx
Faster way to switch between clusters and namespaces in kubectl
Two tiny command-line tools that let you switch between Kubernetes clusters and namespaces with a single short command, instead of typing long kubectl configuration strings each time.
kubectx and kubens are two small command-line tools that make it faster and less error-prone to work with Kubernetes, a platform for running containerized applications. Kubernetes organizes workloads into clusters (a group of machines running together) and namespaces (logical subdivisions within a cluster used to separate environments or teams). When managing multiple clusters or environments — for example, a development cluster, a staging cluster, and a production cluster — switching between them with the default kubectl tool involves typing long commands with full configuration details.
kubectx simplifies cluster switching to a single short command. Running kubectx minikube switches to a local development cluster. Running kubectx - switches back to whichever cluster you were in before, like pressing the back button. You can also rename contexts to short friendly names, and spawn isolated read-only shells scoped to a single cluster to avoid accidentally making changes to the wrong environment.
kubens does the same thing for namespaces within a cluster — switching the active namespace with a single command and allowing you to jump back to the previous one with kubens -.
Both tools support Tab completion on bash, zsh, and fish shells so you never have to remember exact names. If you install fzf (a fuzzy-finder tool), both commands also gain an interactive menu where you can type a few characters to filter and select from your list of clusters or namespaces. They are written in Go and available via Homebrew, apt, pacman, Chocolatey, and other package managers, as well as through the Kubernetes plugin manager Krew.
Where it fits
- Switch between dev, staging, and production Kubernetes clusters with a single kubectx command instead of long kubectl flags.
- Jump back to the previously active cluster or namespace instantly with kubectx - or kubens -.
- Use the interactive fzf menu to fuzzy-search and select from a long list of clusters or namespaces by typing a few characters.
- Rename long auto-generated Kubernetes context names to short friendly aliases for faster daily use.