Home > Software engineering >  Find out all the pods that are using default service account
Find out all the pods that are using default service account

Time:12-15

We have a k8s cluster with 10 workers. we run hundreds of pods in the cluster. we want to avoid running pods with default service account. Need to find out the pods that are running with default service account. am able to find the number of pods using default service account with grep command but also need the pod name and the image it is using. Let us know your thoughts

CodePudding user response:

I used the below command to identify the pods from each namespace that is using default service account

kubectl get pods --all-namespaces -o json | jq '.items[] | select(.spec.serviceAccountName?=="default") | "\(.metadata.namespace) \(.metadata.name)"' | cut -d'"' -f2 | sort

CodePudding user response:

  • In Case if you want use just kubectl without jq :
 kubectl get pods -o jsonpath='{range .items[?(@.spec.serviceAccountName == "default")]}{.metadata.name}{"\n"}{end}' 2>/dev/null
  • i have added 2>/dev/null to avoid printing whole json template in case if no field was found
  • Related