kubectx
Switch between Kubernetes contexts
TLDR
List the contexts
Switch to a named context
Switch to the previous context
Rename a named context
Show the current named context
Delete a named context
SYNOPSIS
kubectx [NAME | - | <pattern>]
kubectx -d <NAME>
kubectx -c <NAME>
kubectx -u
kubectx -V
kubectx -h
kubens [NAME | - | <pattern>]
kubens -c <NAME>
kubens -V
kubens -h
PARAMETERS
NAME | <pattern>
Specifies the Kubernetes context or namespace to switch to. Both kubectx and kubens support fuzzy matching, allowing you to provide a partial name or pattern.
-
Switches back to the previously active Kubernetes context (for kubectx) or namespace (for kubens). This acts as a convenient toggle.
-d <NAME>
(kubectx only) Deletes the specified context from your kubeconfig file. If the deleted context was the currently active one, kubectx will automatically switch to another available context.
-c <NAME>
Copies the name of the specified context or namespace to your system's clipboard, making it easy to paste elsewhere.
-u
(kubectx only) Interactively updates the name of the current context. This is useful for renaming contexts without manual kubeconfig file editing.
-V
Displays the version information for the kubectx or kubens command being used.
-h
Displays a help message, providing a concise summary of the command's usage and available options.
DESCRIPTION
kubectx is a command-line tool that significantly enhances the Kubernetes kubectl experience by providing a rapid and intuitive way to switch between Kubernetes contexts. For users managing multiple Kubernetes clusters, environments, or projects, frequently changing the active context in their kubeconfig file is a common task. Without kubectx, this involves typing out lengthy kubectl config use-context commands.
kubectx streamlines this by allowing users to list all available contexts, switch to a specific one by name (with support for fuzzy matching), or quickly revert to the previous context with a single command. It is typically paired with kubens, its counterpart for managing Kubernetes namespaces, which offers similar functionality for switching namespaces within the current context. Both tools feature robust tab-completion for popular shells, drastically improving productivity for Kubernetes administrators and developers.
CAVEATS
kubectx and kubens directly modify your ~/.kube/config file by updating the current-context or current-namespace fields. While generally safe, it's always recommended to back up your kubeconfig file periodically. These tools are designed for efficient switching and do not manage the creation, deletion (other than `-d` option), or configuration details of Kubernetes contexts or clusters themselves.
FUZZY MATCHING
A standout feature of both kubectx and kubens is their intelligent fuzzy matching capability. Instead of requiring the exact context or namespace name, you can provide a partial string or pattern, and the tool will intelligently suggest or switch to the most probable match. This greatly reduces typing errors and accelerates context/namespace switching.
AUTO-COMPLETION
kubectx and kubens provide robust shell auto-completion scripts for popular shells like Bash, Zsh, and Fish. Once installed, pressing the 'Tab' key after typing kubectx or kubens will display available contexts or namespaces, enabling users to select desired configurations with minimal typing and maximum accuracy.
INSTALLATION
These tools are not part of the official kubectl distribution but are widely available through various channels. Common installation methods include using package managers like Homebrew (on macOS), installing via the Kubernetes Krew plugin manager (e.g., kubectl krew install kubectx), or simply cloning the Git repository and adding the scripts to your system's PATH.
HISTORY
kubectx was originally created by Ahmet Alp Balkan to address the common challenge faced by Kubernetes users of frequently switching between multiple contexts and namespaces. Its design focused on simplicity and speed, quickly gaining popularity for its ability to significantly streamline workflows. kubens was developed as a direct complement, extending the same user-friendly switching capabilities to Kubernetes namespaces.