You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Kubernetes集群中当前健康运行的Pod数量?现有命令统计不准确

How to Count Healthy Running Pods in Kubernetes (Excluding Unhealthy Ones like CPU-starved Pods)

Hey there, I get exactly what you're dealing with—using kubectl get hpa to pull currentReplicas only gives you the total number of replicas the HPA is managing, but it doesn't account for whether those pods are actually healthy (like ones struggling with CPU shortages). Let's walk through a few reliable ways to get the count of truly healthy pods:

1. Filter Pods by "Ready" Status (Most Common Approach)

The simplest way is to count pods where the Ready condition is marked as True. This ensures you're only counting pods that are fully operational and ready to serve traffic.

Using JSONPath to Directly Count

Run this command (replace <your-namespace> and <your-app-label> with your actual values):

kubectl get pods -n <your-namespace> -l <your-app-label> -o jsonpath='{range .items[?(@.status.conditions[?(@.type=="Ready" && @.status=="True")])]}{@.metadata.name}{"\n"}{end}' | wc -l
  • The JSONPath filters pods where the Ready condition status is True.
  • We output each matching pod's name, then pipe to wc -l to get the final count.

Using Custom Columns for Clarity

If you want to visualize the status first before counting, use custom columns to list pod names and their readiness status:

kubectl get pods -n <your-namespace> -l <your-app-label> -o custom-columns="NAME:.metadata.name,READY:.status.conditions[?(@.type=='Ready')].status"

Then count the True entries with:

kubectl get pods -n <your-namespace> -l <your-app-label> -o custom-columns="READY:.status.conditions[?(@.type=='Ready')].status" | grep True | wc -l

2. Exclude Pods with High CPU Usage (For CPU-starved Scenarios)

If you specifically want to exclude pods under CPU pressure (like throttled or overutilized pods), you'll need the metrics-server installed in your cluster. Then you can filter by CPU usage:

kubectl top pods -n <your-namespace> -l <your-app-label> --no-headers | awk '$3+0 < 80' | wc -l
  • kubectl top pods pulls real-time CPU/memory usage metrics.
  • The awk command filters out pods where CPU usage is 80% or higher (adjust the 80 to match your threshold for "unhealthy").
  • wc -l gives the count of pods staying under the CPU threshold.

3. Combine Readiness and Health Checks

If your pods use liveness/readiness probes, you can narrow down to pods that pass both checks. For example, to count pods where Ready is True and no recent liveness probe failures exist:

kubectl get pods -n <your-namespace> -l <your-app-label> -o jsonpath='{range .items[?(@.status.conditions[?(@.type=="Ready" && @.status=="True")] && !@.status.containerStatuses[?(@.lastLivenessProbeFailed==true)])]}{@.metadata.name}{"\n"}{end}' | wc -l

A Quick Note on HPA's currentReplicas

Just to clarify: currentReplicas from HPA only reflects the number of replicas the Deployment/StatefulSet is configured to run—it doesn't validate if those pods are actually healthy. That's why we need to query the pods directly to get an accurate count of fully functional instances.

内容的提问来源于stack exchange,提问作者Java

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:37:29