Skip to content

Instantly share code, notes, and snippets.

@zulhfreelancer
Last active March 2, 2024 15:38
Show Gist options
  • Save zulhfreelancer/d280a8eaeb2995b931d911e3ebc1eef1 to your computer and use it in GitHub Desktop.
Save zulhfreelancer/d280a8eaeb2995b931d911e3ebc1eef1 to your computer and use it in GitHub Desktop.
Get all pods with restart count more than zero / 0

Get all pods with restart count more than zero / 0, with headers, node name and restart count

k get pods -A -o json | jq -r '["NAMESPACE", "POD_NAME", "NODE_NAME", "RESTART_COUNT"], (.items[] | select(.status.containerStatuses[].restartCount > 0) | [.metadata.namespace, .metadata.name, .spec.nodeName, .status.containerStatuses[].restartCount]) | @tsv' | sort -k1,1 | column -t -s $'\t'

Get all pods with restart count more than zero / 0, without headers, just namespace and pod name

k get pods -A -o json | jq -r '(.items[] | select(.status.containerStatuses[].restartCount > 0) | [.metadata.namespace, .metadata.name]) | @tsv' | sort -k1,1 | column -t -s $'\t'

To confirm the output above

for n v in `k get pods -A -o json | jq -r '(.items[] | select(.status.containerStatuses[].restartCount > 0) | [.metadata.namespace, .metadata.name]) | @tsv' | sort -k1,1 | column -t -s $'\t'`; do k -n $n get pod $v -o wide --no-headers | awk '{print $1" "$4}'; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment