How can I get the number of running pods for an app using Python Kubernetes Package

12/6/2018

I have an app deployed on Kubernetes. I'd like to get the number of running pods of that deployment using kubernetes package in Python. Any leads appreciated. Thanks :)

-- Vatsal Gosaliya
containers
docker
kubernetes
kubernetes-pod
python

1 Answer

12/6/2018

Have you considered reading the docs from the package? it is the first sample!

https://github.com/kubernetes-client/python

from kubernetes import client, config

# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()

v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
    print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))

https://github.com/mnubo/kubernetes-py

from kubernetes import K8sPod

pod = K8sPod(config=cfg_token, name='redis')
pod.list()
-- Diego Mendes
Source: StackOverflow