Kubectl: Set Default Namespace – Kubernetes

Out of the box, there is a Namespace called ‘default‘ in a Kubernetes cluster.

By default, the kubectl command interacts with this ‘default‘ Namespace, but it can be changed to any other Namespace.

In this note i will show how to set the default Namespace in Kubernetes.

Cool Tip: List & Change Namespaces in Kubernetes! Read more →

Set Default Namespace in Kubernetes

To get the list of all Namespaces in a Kubernetes cluster, run one of the commands below:

$ kubectl get namespaces
- or -
$ kubectl get ns
- sample output -
NAME              STATUS   AGE
default           Active   1d
kube-node-lease   Active   1d
kube-public       Active   1d
kube-system       Active   1d

To find out the current Namespace name:

$ kubectl config view --minify --output 'jsonpath={..namespace}'; echo
- sample output -
default

To change the Namespace (set the default namespace for the current context):

$ kubectl config set-context --current --namespace=<NAME>

The command above sets the default Namespace for the current context, so all the kubectl commands in this context, by default, will be executed in the defined Namespace.

Cool Tip: List Pods in Kubernetes cluster! Read more →

Was it useful? Share this post with the world!

Leave a Reply