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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With