Skip to main content
Kubernetes

How to Switch Namespaces in Kubernetes

Learn how to switch namespaces in Kubernetes to run kubectl commands in the desired namespace.

LHB Community

Warp Terminal

In Kubernetes, switching namespaces is helpful when managing multiple applications in the same cluster.

For example, you might have Application A running in the dev namespace and Application B in the test namespace. If you're working on Application A but need to troubleshoot Application B, you can switch to the test namespace to access its resources without affecting Application A.

This allows you to work within the specific environment of each application, keeping things organized and preventing accidental interference between projects.

In this article, we’ll walk through how to change namespaces in Kubernetes using kubectl command.

List all available namespaces

To view all namespaces in your Kubernetes cluster, you can use the kubectl get namespaces command. This command will return a list of all the namespaces currently available in the cluster.

kubectl get namespaces

Output.

NAME              STATUS   AGE
default           Active   10d
kube-node-lease   Active   10d
kube-public       Active   10d
kube-system       Active   10d
my-namespace      Active   5d

In this example, you can see five namespaces, including the default Kubernetes namespaces default, kube-system, kube-public and a custom namespace my-namespace.

Running kubectl command on a specific namespace

You can switch between namespaces by using the kubectl command with the --namespace flag. This is particularly useful when you want to perform a one-off command targeting a specific namespace.

For example, to list the pods in the my-namespace namespace:

kubectl get pods --namespace=my-namespace

Output:

NAME                        READY   STATUS    RESTARTS   AGE
my-app-69b58b4c88-6gd4m     1/1     Running   0          2d
my-app-69b58b4c88-wzmgt     1/1     Running   0          2d

To perform any kubectl command for a specific namespace without specifying the --namespace flag each time, you can change the current context to set the default namespace for your kubectl commands. I explain that in the next section.

Setting default namespace for commands

If you find yourself repeatedly working within a specific namespace, you can set a default namespace in your current context. This avoids having to specify --namespace with every command.

To set the default namespace for your current kubectl context, use the kubectl config set-context command.

kubectl config set-context --current --namespace=my-namespace

Output.

Context "my-context" modified.

Now, when you run kubectl commands, the default namespace will be my-namespace, and you won’t need to specify --namespace=my-namespace anymore.

To verify the current context’s namespace, you can use the following command:

kubectl config view --minify | grep namespace:

Output:

namespace: my-namespace

Switching namespace with context in Kubernetes

Kubernetes allows you to define and switch between multiple contexts. Each context includes the cluster, user, and namespace information. You can switch between contexts to change both the namespace and cluster.

To list all available contexts, use:

kubectl config get-contexts

Output:

CURRENT   NAME           CLUSTER         AUTHINFO        NAMESPACE
*         my-context     my-cluster      my-user         my-namespace
          other-context  other-cluster   other-user      other-namespace

You can switch to a different context (and namespace) using:

kubectl config use-context other-context

Output:

Switched to context "other-context".

After switching, all kubectl commands will be executed in the context’s namespace.

Define namespace in Kubernetes configuration files

You can define a default namespace for Kubernetes commands in the ~/.kube/config file by adding the namespace field under a specific context.

For example, here’s how you can define the namespace in your Kubernetes configuration file:

apiVersion: v1
kind: Config
clusters:
- name: my-cluster
  cluster:
    server: https://my-cluster-server
contexts:
- name: my-context
  context:
    cluster: my-cluster
    user: my-user
    namespace: my-namespace
current-context: my-context

In this configuration, the namespace: my-namespace line ensures that kubectl commands are executed in the my-namespace namespace by default when the my-context context is active.

To check if the changes took effect, you can run the following command:

kubectl config view --minify

Output.

contexts:
- context:
    cluster: my-cluster
    user: my-user
    namespace: my-namespace
  name: my-context
current-context: my-context

Conclusion

In this article, we explored various ways to switch namespaces in Kubernetes with practical examples. You can also easily set default namespaces in your kubectl commands or directly in your Kubernetes configuration file.

✍️
Author: Hitesh Jethwa has more than 15+ years of experience with Linux system administration and DevOps. He likes to explain complicated topics in easy to understand way.
LHB Community