Brad Parks
Brad Parks

Reputation: 71961

kubernetes - how to find pods that are running and ready?

I'd like to get the list of kubernetes pods for a service that are "Running" and fully "Ready".

And by fully ready, I mean shows a full "READY" count in k9s, so if there are 4 conditions for the pod to run, I see "READY" with "4/4" listed in k9s.

How can I do this?

Upvotes: 0

Views: 1007

Answers (1)

Brad Parks
Brad Parks

Reputation: 71961

For a particular service, my-service, this only shows pods that are fully ready

$ kubectl get pods --selector=app=my-service -o json  | select_ready_pods.py

Similar idea for all pods

$ kubectl get pods --all-namespaces -o json  | select_ready_pods.py

List pods that are NOT ready

$ kubectl get pods --selector=app=my-service -o json  | select_ready_pods.py --not_ready

select_ready_pods.py

#!/usr/bin/env python

import sys
import json

try:
    a = json.load(sys.stdin)
except:
    print("The data from stdin doesnt appear to be valid json. Fix this!")
    sys.exit(1)


def main(args):
    for i in a['items']:
        length = len(i['status']['conditions'])
        count = 0
        for j in i['status']['conditions']:
            if (j['status'] == "True"):
                count=count+1

        if (args.not_ready):
            if (count != length):
                print(i['metadata']['name'])
        else:
            if (count == length):
                print(i['metadata']['name'])

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--not_ready", help="show pods that are NOT ready", action="store_true")
args = parser.parse_args()

main(args)

Upvotes: 2

Related Questions