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:




Kubernetes: Namespaces

Kubernetes supports multiple virtual clusters backed by the same physical cluster. These virtual clusters are called namespaces.

More info here: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/


List namespaces: 

kubectl get namespaces

but handy to see labels attached so run:

kubectl get namespaces --show-labels



error: the server doesn’t have a resource type “namespaces”

Check what context you’re using. E.g. kubectx and then check if that cluster is running. E.g. minikube status.



More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/


Practical examples: Deploy a WordPress app using Kubernetes on AWS

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