Quickly switch Kubernetes cluster and namespaces with kubectx and kubens
Tags: kubernetes, tools, productivity, k8s
Published: 2021-02-26 23:01:56

---
title: Quickly switch Kubernetes cluster and namespaces with kubectx and kubens
published: true
date: 2020-12-29 00:00:00 UTC
tags: 
- kubernetes
- tools
- productivity
- k8s
canonical_url: https://www.visualstudiogeeks.com/kubernetes/utilities/using-kubectx-kubens
---

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.

<!--more-->

![](https://www.visualstudiogeeks.com/images/screenshots/utkarsh/2020-12-30-using-kubectx-kubens/cluster-ns.png)<figcaption>Cluster contexts and namespaces</figcaption>

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.

```bash
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

```bash
# 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.

```bash
$ kubectl config get-contexts
```

You will see context information as below

```bash
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

```bash
$ 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.

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

### Using kubectx and kubens

#### kubectx

`kubectx` is a great [open-source tool](https://github.com/ahmetb/kubectx/) to switch clusters. Just type `kubectx` and it lists the contexts you have. Then you can select one by typing

```bash
kubectx dev-aks-cluster
```

If you have [`fzf`](https://github.com/junegunn/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?

![](https://www.visualstudiogeeks.com/images/screenshots/utkarsh/2020-12-30-using-kubectx-kubens/kubectx-demo.gif)<figcaption>kubectx - Source: https://github.com/ahmetb/kubectx/</figcaption>

#### kubens

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.

![](https://www.visualstudiogeeks.com/images/screenshots/utkarsh/2020-12-30-using-kubectx-kubens/kubens-demo.gif)<figcaption>kubens - Source: https://github.com/ahmetb/kubectx/</figcaption>

### Conclusion

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 👋🏼