Kubernetes: creating a new namespace and using it in a new context

Think of namespaces as folders to organize objects in your cluster.

You specify a namespace temporarily with -n.

Or more permanently with a context.

Contexts and namespaces are tracked locally in your ~/.kube/config file.




If you edit it manually take a backup.

Or use kubectl. E.g.

kubectl config set-context ...


Isolating work with namespaces

Here’s how to isolate your work using a new namespace called dev in a new context, also called dev:

Note: I’m aliasing kubectl to k.

List namespaces with: kubectl get namespaces --show-labels


And to quickly switch between contexts use kubectx:


brew install kubectx

List contexts:



kubectx <name>


Delete namespaces with:

kubectl delete namespaces <name>

Note: this will not delete it from your config file – https://stackoverflow.com/questions/53283120/kubernetes-cant-delete-namespace/53283273#53283273


To do this declaratively, use a config file. E.g.



Switching namespaces


(installed at same time as kubectx)



Before adding a context with the emojivoto namespace:

and after adding a context with the emojivoto namespace:


Note you can do this using kubens(https://github.com/ahmetb/kubectx) – e.g.


See also:




Leave a Reply

Your email address will not be published. Required fields are marked *