Switching Namespaces Using the kubectl Command

Kubernetes namespace provides the isolation for the different types of resources within the cluster you can use the following command to switch the namespace in the Kubernetes cluster,

kubectl config set-context ā€“current ā€“namespace=<desired-namespace>

  • kubectl: Kubectl is a command-line interface (CLI) tool for interacting with Kubernetes clusters.
  • config: This is a subcommand of kubectl used for managing kubeconfig files. Kubernetes uses a kubeconfig file to store information about clusters, users, and contexts.
  • set-context: This is another subcommand of the kubectl config used to modify the current context in the kubeconfig file.
  • ā€“current: This flag specifies that you want to modify the current context in the kubeconfig file. The current context is the set of cluster, user, and namespace information that kubectl uses by default for executing commands.
  • ā€“namespace=<desired-namespace>: This flag sets the namespace to the specified <desired-namespace>. Replace <desired-namespace> with the name of the namespace you want to switch to.

Steps To Switch Namespace

Step 1: List all the namespaces in Kubernetes.

kubectl get namespaces

Step 2: Select the namespace that you want to switch to and want to set it as a default and use the command shown in below. Here I am using ā€œkube-publicā€.

Step 3: Know get the current namespace you are in using the given command below.

kubectl config view | grep namespace:

How to Change Namespace in Kubernetes ?

Kubernetes namespaces offer an indispensable instrument for resource organization and isolation in the ever-changing realm of container orchestration. Like distinct districts in a busy metropolis, each namespace in your Kubernetes cluster serves as a dedicated place where you may organize related storage, services, and applications into logical units.

Similar Reads

What Is Kubernetes Namespace?

Kubernetes Namespace is a mechanism that enables you to organize resources. It is like a virtual cluster inside the cluster. A namespace isolates the resources from the resources of other namespaces. For example, You need to have different names for deployments/services in a namespace but you can have the same name for deployment in two different namespaces....

Switching Namespaces Using the kubectl Command

Kubernetes namespace provides the isolation for the different types of resources within the cluster you can use the following command to switch the namespace in the Kubernetes cluster,...

Using the kubectl Command With the ā€“namespace Flag

The namespace for which you wish to perform operations can be specified by using the kubectl command with the ā€“namespace flag. The basic syntax is as follows:...

Setting Namespace in Kubernetes Configuration Files

In some organization they frequently work on some namespace for example ā€œdev-nsā€,ā€prod-nsā€ and etc in that cases you can set the particular namespace as a default to the cluster for that follow the steps below....

Change Namespace in Kubernetes ā€“ FAQs

How to communicate one namespace to another namespace in Kubernetes?...

Contact Us