Post Image

Kubernetes Set Working Namespace

When using the kubectl command to manage your Kubernetes cluster you will quickly realize that you need to work with different namespaces and this can lead to long commands or extra output. In order to efficiently manage this you can specify the namespace in the command you are entering using -n flag, or if you have a bunch of commands to run in a specific namespace you can set the working namespace so you don't need to specify the namespace in each command which is much more efficient if working in a specific namespace for a while. In this post I will demonstrate with examples on how to work with namespaces, view your working namespace, change your working namespace, issue commands in all namespaces, and issuing commands in a specific namespace.

 

Return Data from All Namespaces

You can run commands to get data from all namespaces but this can lead to a bunch of data that you don't need and is hard to look at. An example getting pods from all namespaces is below.

$ kubectl get pods --all-namespaces

This will lead to a lot of results if you have a lot of pods running so this is often not the best way to get the data you are looking for.

 

Listing Namespaces

To know what namespaces to use you need to know what namespaces are on your cluster. Below is the command to list your clusters namespaces.

$ kubectl get namespaces
NAME              STATUS   AGE
kube-system       Active   29h
kube-public       Active   29h
kube-node-lease   Active   29h
default           Active   29h
metallb-system    Active   29h
ingress           Active   29h
$

 

Check working namespace

To know what namespace you are currently in on your cluster you can issue the following command and under the NAMESPACE column will show what namespace you are in.

$ kubectl config get-contexts
CURRENT   NAME       CLUSTER            AUTHINFO   NAMESPACE
*         microk8s   microk8s-cluster   admin      default
$

As you can see I am in the default namespace so after I am done I may want to return to that namespace.

 

Specify Namespace in Command

For one off commands you can specify the namespace directly in the command with the -n flag. Example of getting pods is below.

$ kubectk get pods -n metallb-system

This will get all pods running in the metallb-system namespace.

 

Set Working Namespace

If you have multiple things you need to do in a specific namespace it is beneficial to set the working namespace so that all commands entered using kubectl afterward will run in that namespace and you dont need to specify it in each command.

Below is a command to change the working namespace to metallb-system

$ kubectl config set-context --current --namespace=metallb-system

We can confirm by using the kubectl config get-contexts command

$ kubectl config get-contexts
CURRENT   NAME       CLUSTER            AUTHINFO   NAMESPACE
*         microk8s   microk8s-cluster   admin      metallb-system
$

So at this point any command that we run without specifying a namespace will run in the metallb-system namespace. Now if I get pods it will only list pods running in the metallb-system namespace and any other commands will run in that namespace.

$ kubectl get pods

 

 

 



Comments (0)
Leave a Comment