Quickly switch Kubernetes cluster and namespaces with kubectx and kubens

Dec 29, 2020 | Utkarsh Shigihalli Utkarsh Shigihalli | 2 min read


Often when working with Kubernetes you might find yourself switching between your clusters or namespaces. I am doing this numerous times lately and I was slow switching using the regular commands. In this post, I would like to highlight two productivity utilities when working with Kubernetes which will make it simple when working with multiple clusters.

Cluster contexts and namespaces

Kubernetes stores the cluster information in ~/.kube/config file and stores in as a context information. If you are working against multiple clusters ideally you would have different config files and can reference them when running commands with kubectl.

For example, to get pods in different cluster than your default context, you use below command.

kubectl get pods --namespace dev-ns --kubeconfig ~/.kube/devClusterConfig

As you can see, typing namespace information and providing the config for every command is cumbersome.

Merging multiple config files in to one

If you already have multiple config files to connect to different clusters, you would like to merge them first. You can merge both the config file using syntax below

# Merge ~/.kube/config and ~/.kube/devClusterConfig in to new config /tmp/config
$ KUBECONFIG=~/.kube/config:~/.kube/devClusterConfig kubectl config view --flatten > /tmp/config

# Replace old config with new merged config
$ mv /tmp/config ~/.kube/config

Switching the Kubernetes clusters using kubectl commands

Kubernetes already has a concept of contexts and once you merge the config, you will be able to see the contexts information.

$ kubectl config get-contexts

You will see context information as below

CURRENT   NAME              CLUSTER           AUTHINFO                                        NAMESPACE
*         dev-aks-cluster   dev-aks-cluster   clusterUser_aksdemo_dev-aks-cluster             dev-ns
          qa-aks-cluster    qa-aks-cluster    clusterUser_aksdemo_qa-aks-cluster

You can then switch to the different cluster (in my case its qa-aks-cluster) using below command

$ kubectl config use-context qa-aks-cluster

Switching the namespaces using kubectl commands

Now if you do not want to add --namespace <namespace-name> for every command you type, you can also set it as default namespace for the currently selected context.

kubectl config set-context --current --namespace=<insert-namespace-name-here>

Using kubectx and kubens


kubectx is a great open-source tool to switch clusters. Just type kubectx and it lists the contexts you have. Then you can select one by typing

kubectx dev-aks-cluster

If you have fzf installed, you can also get a nice picker and you can use arrows to select one.

If you keep alternating between contexts, you can also use kubectx - to quickly go back to previous context. Isn’t it cool?

kubectx - Source: https://github.com/ahmetb/kubectx/


Similar to kubectx, kubens is another great utility from the same creator. It performs pretty much similar to kubectx but lets you switch namespaces. I regularly use this since we have couple of namespaces within our cluster which we deploy to. It has certainly saved me lots of typing.

kubens - Source: https://github.com/ahmetb/kubectx/


If you are into Kubernetes, both kubectx and kubectl are great productivity tools and will save you lots of time. I certainly am finding them really useful.

That’s it for this post, thanks for reading, until next time time 👋🏼

Related Posts
About author
Utkarsh Shigihalli
Utkarsh Shigihalli
Utkarsh is passionate about software development and has experience in the areas of Azure, Azure DevOps, C# and TypeScript. Over the years he has worked as an architect, independent consultant and manager in many countries including India, United States, Netherlands and United Kingdom. He is a Microsoft MVP and has developed numerous extensions for Visual Studio and Azure DevOps.