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.

- context:
    cluster: development
    namespace: storage
    user: developer
  name: dev-storage
- context:
    cluster: scratch
    namespace: default
    user: experimenter
  name: exp-scratch

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.

k create namespace dev
k config get-contexts
CURRENT_CONTEXT=$(kubectl config current-context)
CURRENT_CLUSTER=$(kubectl config get-contexts $CURRENT_CONTEXT | tail -1 | awk '{print $3}')\n
CURRENT_USER=$(kubectl config get-contexts $CURRENT_CONTEXT | tail -1 | awk '{print $4}')\n
kubectl config set-context dev --namespace dev --cluster $CURRENT_CLUSTER --user $CURRENT_USER
k config get-contexts

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:

- context:
    cluster: minikube
    user: minikube
  name: minikube

and after adding a context with the emojivoto namespace:

- context:
    cluster: minikube
    user: minikube
  name: minikube
- context:
    cluster: minikube
    namespace: emojivoto
    user: minikube
  name: minikube-emojivoto

Now

kubectx
Switched to context "minikube".

k get pods
No resources found.

kubectx
Switched to context "minikube-emojivoto".

k get pods
NAME                        READY   STATUS    RESTARTS   AGE
emoji-6fc8785888-gjd8r      2/2     Running   2          13d
vote-bot-59f6685c64-4tmkp   2/2     Running   2          13d
voting-84d7b96f96-b6zpp     2/2     Running   2          13d
web-655dc4c6f4-tf2zl        2/2     Running   2          13d

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

kubectx
Switched to context "minikube".
kubens
Context "minikube" modified.
Active namespace is "default".
k get pods
No resources found.
kubens
Context "minikube" modified.
Active namespace is "emojivoto".
k get pods
NAME                        READY   STATUS    RESTARTS   AGE
emoji-6fc8785888-gjd8r      2/2     Running   2          13d
vote-bot-59f6685c64-4tmkp   2/2     Running   2          13d
voting-84d7b96f96-b6zpp     2/2     Running   2          13d
web-655dc4c6f4-tf2zl        2/2     Running   2          13d

 

See also:

 

 

 

Leave a Reply

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