I need to find 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 ?
If your use case is making sure you only have a single instance/replica of your application running at any given time, maybe it's better to rely on built-in Kubernetes options to handle that.
You can configure your Deployment object using replicas
, maxUnavailable
and maxSurge
to control how many instances are created of your application.
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 | le
ngth'
This returns the # of pods that are not Terminated
.