GKE : How to get number of nodes and pods using API

1/21/2020

Currently, I obtaine various information from the GoogleCloudPlatform management console screen, but in the future I would like to obtain it using API. The information obtained is as follows.

Kubernetes Engine>Clusters>Cluster Size
Kubernetes Engine>Workloads>Pods

Please teach the API corresponding to each information acquisition.

-- asaka
google-kubernetes-engine
kubernetes

3 Answers

1/22/2020

Speaking about Python, Kubernetes Engine API could be used in this case.

Kubernetes Engine > Clusters > Cluster Size

In particular, a method get(projectId=None, zone=None, clusterId=None, name=None, x__xgafv=None) returns an object that contains "currentNodeCount" value.

Kubernetes Engine > Workloads > Pods

A code example for listing pods could be found here: Access Clusters Using the Kubernetes API

-- mebius99
Source: StackOverflow

1/22/2020

You need to:

  1. install your command line
  2. connect to your project
  3. connect to your cluster
  4. retrieve the number of pod inside your cluster

Install your command line

You can use your prefered command line or you can use the active cloud shell of your browser (the online command line interface integrated to Google Cloud Platform).

Option A) Using your own command line program, you need to install Google Cloud command (gcloud) on your machine.

Option B) Otherwise if you use the active cloud shell, just click on the active cloud shell button on the top of the page.

enter image description here

Connect to your project

(only for option A)

  • Login to your gcloud platform: gcloud auth login

$ gcloud auth login Your browser has been opened to visit:

https://accounts.google.com/signin/oauth/oauthchooseaccount?client_id=65654645461.apps.googleusercontent.com&as=yJ_pR_9VSHEGFKSDhzpiw&destination=http%3A%2F%2Flocalhost%3A8085&approval_state=!ChRVVHYTE11IxY2FVbTIxb2xhbTk0SBIfczcxb2xyQ3hfSFVXNEJxcmlYbTVkb21pNVlhOF9CWQ%E2%88%99AJDr988AKKKKKky48vyl43SPBJ-gsNQf8w57Djasdasd&oauthgdpr=1&oauthriskyscope=1&xsrfsig=ChkAASDasdmanZsdasdNF9sDcdEftdfECwCAt5Eg5hcHByb3ZhbF9zdGF0ZRILZGVzdGluYXRpb24ASDfsdf1Eg9vYXV0aHJpc2t5c2NvcGU&flowName=GeneralOAuthFlow

  • Connect to your project: gcloud config set project your_project_id

$ gcloud projects list

PROJECT_ID NAME PROJECT_NUMBER first-project-265905 My Project 117684542848 second-project-435504 test 895475526863

$ gcloud config set project first-project-265905

Connect to your cluster

Connected to your project, you need to connect to your cluster.

gcloud container clusters get-credentials your_cluster_name

$ gcloud container clusters list NAME LOCATION MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS test-cluster-1 asia-northeast1-a 1.33.33-gke.24 45.600.23.72 f1-micro 1.13.11-gke.14 3 RUNNING

$ gcloud container clusters get-credentials test-cluster-1 Fetching cluster endpoint and auth data. kubeconfig entry generated for test-cluster-1.

Retrieve the number of nodes/pods inside your cluster

inside a given name space run the command

$ kubectl get nodes NAME STATUS ROLES AGE VERSION gke-test-cluster-1-default-pool-d85b49-2545 NotReady 24m v1.13.11-gke.14 gke-test-cluster-1-default-pool-d85b49-2dr0 NotReady 3h v1.13.11-gke.14 gke-test-cluster-1-default-pool-d85b49-2f31 NotReady 1d v1.13.11-gke.14

$ kubectl get pods NAME READY STATUS RESTARTS AGE busybox 0/1 Pending 0 44s nginx 0/1 Pending 0 1m

-- Theophane Fotso
Source: StackOverflow

1/21/2020

GKE UI under the hood calls Kubernetes API to get information and show in UI. You can use kubectl to query Kubernetes API to get that information.

kubectl get nodes
kubectl get pods

If you turn on the verbose mode in kubectl then it will show what REST API its calling on the kubernetes api server.

kubectl --v=8 get nodes
kubectl --v=8 get pods

The REST API for nodes and pods are

GET https://kubernetes-api-server-endpoint:6443/api/v1/nodes?limit=500
GET https://kubernetes-api-server-endpoint:6443/api/v1/namespaces/default/pods?limit=500

Here is the doc on how to configure Kubectl to connect with GKE.

Here is the doc from kubernetes on different ways to access Kubernetes API.

You can also use kubectl proxy for trying it out.

Remember to call above rest apis you need to authenticate to kubernetes api server either with a certificate or with a bearer token.

-- Arghya Sadhu
Source: StackOverflow