0

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?

0

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

maybe it's also useful to say that if you'd like to print out all pods which have one of their conditions set to "False" you just have to change if (count == length): to if (count != length): can take you a little while to figure that out if you're not that used to work with arrays and stuff like this, other than that great answer!
@alex - good idea! I've added what I think should work for your use case, an option to show the nodes that aren't ready

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.