Kubernetes is a powerful container management platform that can be used to manage multiple containers on different systems. However, if you want to quickly switch contexts between Kubernetes and another container management platform, you may need to use Kubectx or Kubens. Kubernetes uses the kube-proxy and kube-controller-proxy to manage containers. If you want to use another container management platform, you need to create a new Kubernetes context and add the appropriate components. To quickly switch contexts between Kubernetes and another container management platform, use Kubectx or Kubens.
Kubectx and Kubens are two tools which accelerate your Kubernetes management experience. They address some of the shortcomings of Kubectl by making it quicker and more convenient to switch between cluster environments.
Both commands are developed by the same author and bundled into one package. Kubectx changes the active Kubernetes context. This is the combination of server URL and user credentials defining the cluster you’re connected to. Kubens switches between namespaces within that cluster.
Why Are They Useful?
Switching between clusters using plain Kubectl can be cumbersome. Although the tool supports a couple of methods for config switching, they aren’t particularly quick or intuitive for this use case.
One way is to create a separate config file for each of your clusters. You can then set the KUBECONFIG environment variable to switch between them:
This requires you to memorize the name and path of each of your config files. It doesn’t help you define namespaces either so you must use Kubectl’s –namespace flag to override the default with each command.
Kubectl also offers an integrated context management experience. This lets you define multiple clusters and user credentials in a single config file. You can issue kubectl commands to switch between them.
This config file sets up two contexts. They both connect to the same cluster using a single set of credentials. The contexts are configured to use different namespaces within the cluster.
This is a more streamlined experience but it’s still relatively verbose. Kubectx and Kubens abstract away all the complexity, giving you simple short commands to rapidly switch contexts and namespaces.
Installing The Tools
Kubectx and Kubens are distributed in several different formats. You can download plain pre-compiled binaries from the project’s GitHub releases page, install from the Apt, Pacman, or Homebrew package repositories, or use Kubectl’s plugin manager, Krew. We’ll focus on Krew as it’s a good way to work with all your Kubectl accessories.
Make sure you’ve got Krew installed and can use the kubectl krew command. You can add Krew to your system using the installation script on the project’s website.
Next use Krew to install the Kubectx and Kubens components:
You can optionally enable shell completion support for Bash, Fish and Zsh by running the relevant scripts provided in the project’s README file.
Kubectl and Kubens are also compatible with fzf to generate interactive menus with fuzzy search support. This integration is automatically enabled when you’ve got fzf in your PATH. You can turn it off it by setting the KUBECTX_IGNORE_FZF environment variable to 1.
Changing Contexts
The Kubectx command is used to change between available Kubectl contexts. It takes the name of the target context as its only parameter. Contexts must already exist in your active Kubectl config file. Use regular kubectl config commands to create your contexts before using the command.
kubectx is a wrapper around kubectl config use-context. The context will be targeted by all subsequent Kubectl commands until you select a different one with either kubectx or kubectl.
Kubectx accepts - as the context name to quickly jump back to the previously selected context. This matches the behavior of familiar CLI tools like cd and git, making it easier to work with multiple contexts concurrently.
Kubectx can also create aliases for your contexts. These let you reference descriptive context names using a more convenient tag.
Changing Namespaces
kubens switches between namespaces. It has the same effect as changing the namespace of the active context. Kubectl commands will be executed against the specified namespace, inside the currently selected context.
Similarly to kubectx, you can use - to jump back into your previous namespace:
Migrating to Kubectx and Kubens From KUBECONFIG
Kubectl contexts, Kubectx, and Kubens work best when you’re storing all your contexts and clusters in one Kubeconfig file. This is usually located at ~/.kube/config. If you’ve previously been working with multiple config files, you can merge them together using Kubectl:
Putting your contexts into a single ~/.kube/config makes them all available to kubectx and kubens. You can stop using KUBECONFIG to manually juggle multiple files.
When you add a new cluster to your fleet, you can combine its config file with your existing one using a variation of the sequence shown above:
An alternative simpler option is the konfig config file manager, available as a Kubectl plugin via Krew. This includes an import command that automatically merges a new config file into the one Kubectl’s currently using:
Summary
Kubectx and Kubens are two convenience utilities to streamline your Kubectl experience. While Kubectl contexts are adequate for occasional use, they still tend to feel clunky when you’re rapidly changing between environments. Kubectx and Kubens help you inspect several clusters and namespaces without being impeded by lengthy terminal commands.
The appeal of these tools lie in their simplicity. They also add a few unique capabilities on top of Kubernetes’ context management, such as quick backtracking with - and context aliasing to shorten lengthy names.