Quickly switch Kubernetes cluster and namespaces with kubectx and kubensDec 29, 2020 | Utkarsh Shigihalli | 2 min read Kubernetes
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.
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
For example, to get pods in different cluster than your default context, you use below command.
1 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
1 2 3 4 5 6 # 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.
1 $ kubectl config get-contexts
You will see context information as below
1 2 3 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
1 $ 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.
1 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
1 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?
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.
If you are into Kubernetes, both
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 👋🏼