Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Only Names Of All Namespaces Kubectl - Kubernetes

I want to get a list of the names of all namespaces in a Kubernetes cluster using a kubectl command.

Currently, I am running the below kubectl command which gives me a list of all namespaces.

However, I don't want to see the 'ACTIVE' and 'AGE' columns. I just want the NAME column.

The reason behind this is so that I can add all of the items from the NAME column into an array for further processing.

> kubectl get namespaces
NAME                      STATUS   AGE
dread-gorge               Active   284d
dread-lagoon              Active   210d
carncier-basin            Active   164d
chantague-shallows        Active   164d
hilraine-loch             Active   311d
stangrave-waters          Active   271d
like image 592
Kerbol Avatar asked Feb 03 '26 23:02

Kerbol


2 Answers

Solution-1:

Here is an alternate way:

 kubectl get ns  --no-headers -o custom-columns=":metadata.name"

-o=custom-columns= Print a table using a comma separated list of custom columns

Solution-2:

Another way of getting the name with the type of resource:

kubectl get ns -o name
like image 156
P.... Avatar answered Feb 05 '26 14:02

P....


using jsonpath from kubectl cheatsheet

kubectl get ns -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'
default
gitlab-runner
kube-node-lease
kube-public
kube-system
like image 20
confused genius Avatar answered Feb 05 '26 12:02

confused genius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!