GKE pod replica count in cluster

1/20/2021

How can we obtain the gke pod counts running in the cluster? I found there are ways to get node count but we needed pod count as well. it will be better if we can use something with no logging needed in gcp operations.

-- user9063834
google-kubernetes-engine
kubernetes

2 Answers

1/20/2021

You can do it with Kubernetes Python Client library as shown in this question, posted by Pradeep Padmanaban C, where he was looking for more effective way of doing it, but his example is actually the best what you can do to perform such operation as there is no specific method which would allow you just to count pods without retrieving their entire json manifests:

from  kubernetes import client , config 

config.load_kube_config()
v1= client.CoreV1Api()
ret_pod = v1.list_pod_for_all_namespaces(watch=False)
print(len(ret_pod.items))

You can also use a different method, which allows to retrieve pods only from specific namespace e.g.:

list_namespaced_pod("default")

In kubectl way you can do it as follows (as proposed here by RammusXu):

kubectl get pods --all-namespaces --no-headers | wc -l
-- mario
Source: StackOverflow

1/21/2021

You can directly access the kubernetes API using a restful API call. You will need to make sure you provide the authentication token in your call by including a bearer token.

Once you are able to query the api server directly, you can use GET <master_endpoint>/api/v1/pods to list all the pods in the cluster. You can also search for specific namespaces by specifying the namespace /api/v1/namespaces/<namespace>/pods.

Keep in mind that the kubectl cli tool is just a wrapper for API calls, each kubectl command will form a RESTful API call in a similar format to the one listed above, so any interaction you have with the cluster using kubectl can also be achieved through RESTful API calls

-- Patrick W
Source: StackOverflow