【问题标题】:Get count of Kubernetes pods that aren't running获取未运行的 Kubernetes pod 数量
【发布时间】:2020-05-06 10:30:37
【问题描述】:

我有这个命令来列出未运行的Kubernetes pod:

sudo kubectl get pods -n my-name-space | grep -v Running

是否有返回未运行 pod 计数的命令?

【问题讨论】:

    标签: kubernetes kubernetes-pod


    【解决方案1】:

    如果您将... | wc -l 添加到该命令的末尾,它将打印grep 命令输出的行数。这可能包括标题行,但你可以取消它。

    kubectl get pods -n my-name-space --no-headers \
      | grep -v Running \
      | wc -l
    

    如果您有像 jq 这样的 JSON 处理工具可用,您可以获得更可靠的输出(如果 Evicted pod 恰好在其姓名)。你应该能够做类似(未经测试)的事情

    kubectl get pods -n my-namespace -o json \
      | jq '.items | map(select(.status.phase != "Running")) | length'
    

    如果你会做很多这样的事情,使用Kubernetes API 编写一个非 shell 程序会更加健壮;您通常可以使用 SDK 调用执行“获取 pod”之类的操作,并获取您可以过滤的 pod 对象列表。

    【讨论】:

      【解决方案2】:

      无需任何外部工具即可完成:

      kubectl get po \
        --field-selector=status.phase!=Running \
        -o go-template='{{len .items}}'
      
      • 过滤是用field-selectors完成的
      • 使用 go-template 完成计数:{{ len .items }}

      【讨论】:

        猜你喜欢
        • 2019-05-08
        • 2020-03-30
        • 1970-01-01
        • 1970-01-01
        • 2022-08-23
        • 1970-01-01
        • 1970-01-01
        • 2019-05-04
        • 2021-12-24
        相关资源
        最近更新 更多