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:
$ KUBECONFIG=~/.kube/cluster-1.yml # Gets pods in cluster-1 $ kubectl get pods
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.
apiVersion: v1 clusters: - cluster: name: demo-cluster insecure-skip-tls-verify: true server: https://127.0.0.1 users: - name: demo user: client-certificate: /path/to/cert client-key: /path/to/key contexts: - context: name: demo-cluster-ns-1 cluster: demo-cluster user: demo namespace: first-namespace - context: name: demo-cluster-ns-2 cluster: demo-cluster user: demo namespace: second-namespace
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.
# Gets pods in first-namespace within demo-cluster $ kubectl config use-context demo-cluster-ns-1 $ kubectl get pods # Gets pods in second-namespace within demo-cluster $ kubectl config use-context demo-cluster-ns-2 $ kubectl get pods
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.
# Gets pods in first-namespace within demo-cluster $ kubectx demo-cluster-ns-1 $ kubectl get pods # Overrides the context's namespace to get pods in second-namespace $ kubens second-namespace $ kubectl get pods
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:
# Kubectx $ kubectl krew install ctx # Kubens $ kubectl krew install ns
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
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.
# Switch to first-context $ kubectx first-context $ kubectl get pods
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
- as the context name to quickly jump back to the previously selected context. This matches the behavior of familiar CLI tools like
git, making it easier to work with multiple contexts concurrently.
$ kubectx first $ kubectx second $ kubectx - # Gets the pods in the first context $ kubectl get pods
Kubectx can also create aliases for your contexts. These let you reference descriptive context names using a more convenient tag.
$ kubectx production=gce-web-app-production-2022 # Switches to gce-web-app-production-2022 $ kubectx production $ kubectl get pods
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.
$ kubectx production $ kubens api # Gets pods in the "api" namespace of the "production" context $ kubectl get pods
kubectx, you can use
- to jump back into your previous namespace:
$ kubens -
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:
# Reference all your config files so Kubectl load them all $ export KUBECONFIG=~/.kube/cluster-1:~/.kube/cluster-2:~/.kube/cluster-3 # Save a merged version of the current config to a new file $ kubectl config view --flatten > ~/.kube/.config
Putting your contexts into a single
~/.kube/config makes them all available to
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:
$ export KUBECONFIG=~/.kube/config:~/new-config-file $ kubectl config view --flatten > ~/.kube/.config
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:
$ kubectl krew install konfig $ kubectl konfig import -s ~/new-config-file
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.
- › 7 Features Android Should Steal From iPhone
- › 10 Hidden Android 13 Features You Might Have Missed
- › Keychron Q8 Mechanical Keyboard Review: An Advanced Keyboard for All Uses
- › Shift+Enter Is a Secret Shortcut Everyone Should Know
- › Lenovo ThinkPad Z13 Gen 1 Review: A Vegan Leather Laptop That Means Business
- › 10 Awesome iPad Features You Should Be Using