how to get the numbers of currently healthy running pods

11/19/2019

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.

-- Java
kubernetes

2 Answers

11/19/2019

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.

-- Oleg Butuzov
Source: StackOverflow

11/19/2019

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

-- suren
Source: StackOverflow