How to switch between namespaces in Kubernetes

In my previous two articles I demonstrated how to create new namespaces with quotas.  However, someone asked is it possible to switch to the new namespace instead of always being in the default namespace.  Yes, yes you can.

My previous two articles:

PowerShell showing kubectl get pods returning a running Redis pod in the default Kubernetes namespace.

As you can see when I issue the get pods commands it returns me the default namespaces information.

PowerShell showing kubectl returning three running hello-world pods from the selected Kubernetes namespace.

As you can see when I issue it against the desired namespace I get the information I am looking for.

How to switch namespaces you are in.

PowerShell showing the kubectl context changed to another namespace and three running hello-world pods listed without a namespace flag.

$ kubectl config set-context $(kubectl config current-context) –namespace=<namespace>

This command will switch you over to the other namespace and as you can see from running just the get pods command I am able to see the 3 I was expecting.

Switch back to default

PowerShell showing the kubectl context changed back to the default namespace and the running Redis pod listed.

$ kubectl config set-context $(kubectl config current-context) –namespace=default 

As you can see I am back in default

Summary:
Not much to say here because this was a simple one.  As always, I hope y’all found this helpful.

Keep exploring

Choose your next step

Continue with the path that best matches the architecture or operating challenge in front of you.

Leave a Reply

Discover more from Digital Thought Disruption

Subscribe now to keep reading and get access to the full archive.

Continue reading