How to list the kubernetes pods based on any particular exit code, for example i need to list all the pods which has exitCode value 255

9/11/2019

How to list the kubenetes pods based on any particular exitCode value. For example i need to list all the pods which has exitCode value = 255.

I have tried below command and it will give all pods along with all exitcodes.

kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{'\t'}{..exitCode}{'\n'}{end}"

kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{'\t'}{..exitCode}{'\n'}{end}"
-- Ankit
go-templates
kubernetes

2 Answers

9/12/2019

Below is the command to get the list of pod based on particular error code:

kubectl get pods $(for i in 0 255; do kubectl get pods -o=jsonpath={.items[?(@..exitCode==$i)].metadata.name} ; echo ;done)

And we can use below command to delete pods based on particular error codes:

kubectl delete pods $(for i in 0 255; do kubectl get pods -o=jsonpath={.items[?(@..exitCode==$i)].metadata.name} ; echo ;done)

where value in for loop i.e 0 255 etc is the error code, you can give any error code here and then run this command.

-- Ankit
Source: StackOverflow

9/12/2019

if I understand you correctly you may want to check out the Field Selectors.

Field selectors let you select Kubernetes resources based on the value of one or more resource fields. Here are some example field selector queries:

  • metadata.name=my-service
  • metadata.namespace!=default
  • status.phase=Pending

This kubectl command selects all Pods for which the value of the status.phase field is Running:

kubectl get pods --field-selector status.phase=Running

Here is some more documentation regarding this.

Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector key1=value1,key2=value2). The server only supports a limited number of field queries per type.

Please let me know if that helped.

-- OhHiMark
Source: StackOverflow