简体   繁体   中英

How to get the list of pods with container name for pods that have restarted

Using this command, I am able to get the container name that have restarted.

kubectl get pods -o jsonpath='{.items[*].status.containerStatuses[?(@.restartCount>0)].name}'

Is there a way to get the pod name as well in the same command?

It's much easier to get json with kubectl and then process it with jq:

#!/usr/bin/env bash

kubectl get pods -o=json |
    jq -r '.items[] |
        "\(.metadata.name) \(.status.containerStatuses[]|select(.restartCount>0).name)"'

I've not tried this.

If (,) it works. it's not quite what you want as it should give you every Pod name and then a list of Container names that match the predicate.

I think you can't use kubectl --output=jsonpath alone to filter only the Pod names that have a Container with restarts.

FILTER='
{range .items[*]}
  {.metadata.name}
  {"\t"}
  [
    {.status.containerStatuses[?(@.restartCount>0)].name}
  ]
  {"\n"}
{end}
'

kubectl get pods \
--output=jsonpath="${FILTER}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM