Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number of pods (available or terminating) in kubernetes?

Tags:

kubernetes

I need to find the number of pods currently NOT terminated (can be available, terminating, etc.) in order to prevent deployment from starting if there are still some pods not terminated.

UPDATE: If there are no pods available, I should get "0". Is it possible ?

like image 475
tumm Avatar asked Jun 12 '19 09:06

tumm


People also ask

How do you know if Kubernetes have terminated pods?

To check the state of a Pod's containers, you can use kubectl describe pod <name-of-pod> . The output shows the state for each container within that Pod.

How many pods are in Kubernetes?

With the default maximum of 110 Pods per node for Standard clusters, Kubernetes assigns a /24 CIDR block (256 addresses) to each of the nodes.


1 Answers

You can try:

kubectl get pods --field-selector=status.phase!=Succeeded,status.Phase!=Failed

If you look at the Pod Phases you can see that this covers all possible pods where all containers are terminated (either failed or succeeded)

If you specifically want the count you could use a bit of jq and use:

kubectl get pods --field-selector=status.phase!=Succeeded,status.Phase!=Failed --output json | jq -j '.items | length'

This returns the # of pods that are not Terminated.

like image 130
Blokje5 Avatar answered Sep 18 '22 22:09

Blokje5