Get Only Names Of All Namespaces Kubectl - Kubernetes

Viewed 4117

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
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
Related