Home > database >  is it possible to get all pods from a list of namescapes?
is it possible to get all pods from a list of namescapes?

Time:11-27

I have a lot of namespaces and I want to get all pods from a sub-list of namespaces.

For getting all the pods from all namespace the command is:

kubectl get pods --all-namespaces   

To get all pods from a spesific namespace the command is:

kubectl get pods -n namespace-name

However I can't find a way to get all pods from a list of namespaces, something like:

kubectl get pods -n namespace-name1, namespace-name2, namespace-name3 

what is the right command for that?

CodePudding user response:

kubectl does not support this. You can use egrep to filter the list of all pods by namespaces:

kubectl get pods -A | egrep '^(namespace-name1|namespace-name2|namespace-name3)'

Because kubectl prints the namespace at the beginning of the line, it greps for a line start ^ followed by one of the namespace names.

CodePudding user response:

You can iterate over the subset of namespaces:

Either:

for NAMESPACE in "namespace-1" "namespace-2"
do
  kubectl get pods \
  --namespace=${NAMESPACE} \
  --output=name
done

Or:

NAMESPACE=$(
  "namespace-1"
  "namespace-2"
)
for NAMESPACE in "${NAMESPACES[@]}"
do
  kubectl get pods \
  --namespace=${NAMESPACE} \
  --output=name
done
  • Related