I am searching for a way to get the numbers of currently healthy running pods, with this command:
kubectl get hpa -o=jsonpath='{.items[0].status.currentReplicas}'
I get the count of all pods regardless of whether the pod is running healthy or for example a pod has Insufficient CPU.
Pretty much its, well, pointless. In case of Insufficient CPU
, if its default limits for node or limits described in spec.resources
, the pod is going to be terminated. You can request CPU consumption from the pod, and implement your own logic behind this, but what for?
# Current running pods and their last status
k get pods -o json | \
jq '.items[] | { name:.metadata.name, status: .status.conditions[-1].type }'
Request you have in your question if returning the current replicas count of the pod auto scaler, and it's already tracking currentMetricValue
to desiredMetricValue
ratio. No need to tracked separately.
If you need the running pods, you can do this:
kubectl get pods --field-selector=status.phase=Running
If what you want is the number, like integer number of running pods, you can add:
kubectl get pods --field-selector=status.phase=Running --no-headers=true | wc -l