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.
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
You need to:
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.
(only for option A)
$ gcloud auth login Your browser has been opened to visit:
$ 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
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.
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
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.