Kubernetes: Switch between namespaces and contexts effortlessness

over 1 year ago

There will be a series of learning and tips on Kubernetes (K8s) based on my experience with the on-going development of NetIQ Access Manager (NAM) to make it fit for the Kubernetes ecosystem. I hope this will benefit people in the field when deploying NAM dockers in the K8s environment in the coming months.

This post is about an effective tool I have been using that helps in moving between Kubernetes namespaces and contexts instantly.

On a high level what's a namespace and a context:

  • Namespaces are a way to divide cluster resources between multiple users.
  • Context is a group of access parameters. Each context contains a Kubernetes cluster, a user, and a namespace. 

Let's get started.

We will use the Krew kubectl plugin manager to get kubectx and kubens. If you are using Linux, go to this URL to see the instructions on how to install Krew.



Once you have krew plugin installed, use the following commands

kubectl krew install ctx


kubectl krew install ns


Post-installation, the tools will be available as kubectl ctx and kubectl ns.

  • To get the list of all the namespaces:  kubectl-ns
  • To switch the namespace: kubectl-ns <namespace you want to switch to>

In the following example, I am switching from default namespace to development.



  • To get the list of all the contexts:  kubectl-ctx
  • To switch the context: kubectl-ctx <context you want to switch to>

In the following example, I am switching the context.


 If you are not using the above tool, you will have to use below kubectl commands to get going

  • To get the list of all the namespaces: kubectl get namespaces
  • To get the list of all the contexts:  kubectl config get-contexts
  • To see the current context: kubectl config current-context
  • To switch the context:  kubectl config use-context <context you want to switch to>

If you are fresh to docker and Kubernetes world, this is the introductory post on the K8s series which highlights some key terms we require to know before a deep dive-in.


How To-Best Practice
Comment List
Related Discussions