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.

~/.kube/config

E.g.

https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/#define-clusters-users-and-contexts

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:

Install:

brew install kubectx

List contexts:

kubectx

Switch:

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.

https://kubernetes.io/docs/tasks/administer-cluster/namespaces/#subdividing-your-cluster-using-kubernetes-namespaces

 

Switching namespaces

kubens

(installed at same time as kubectx)

 

Example

Before adding a context with the emojivoto namespace:

and after adding a context with the emojivoto namespace:

Now

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 *