[英]Google cloud: Get GKE stats using python API
In order eg to get the number of nodes in a GKE cluster, you need to: 为了例如获取GKE集群中的节点数,您需要:
glcoud
command to switch to the appropriate project 使用glcoud
命令切换到适当的项目 gcloud
command to get the credentials for the cluster in the likes of 使用gcloud
命令来获取集群的凭据,例如 gcloud container clusters get-credentials "${CLUSTER_NAME}" --zone "${ZONE}" --project "${PROJECT_ID}"
(but after explicitly passing the zone) (但在明确通过区域之后)
Assuming I am powerful enough, is there a way to get eg the number of nodes for a GKE cluster just by iterating over the projects and then the clusters, eg 假设我足够强大,是否有办法通过迭代项目然后再遍历集群来获取例如GKE集群的节点数。
for project in gcp_projects:
clusters = get_clusters(project)
for cluster in clusters:
nodes = get_number_of_nodes(cluster)
The following Python 3 script will print all clusters with the current node count in multiple projects (specified in the variable gcp_projects
), in all zones ( zone = '-'
in the query means all zones) : 以下Python 3脚本将在所有区域(查询中的zone = '-'
表示所有区域)的多个项目(在变量gcp_projects
指定)中打印具有当前节点数的所有集群:
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
service = discovery.build('container', 'v1', credentials=credentials)
gcp_projects = ['project-1', 'project-2', 'project-3']
for project in gcp_projects:
request = service.projects().zones().clusters().list(projectId=project, zone='-')
response = request.execute()
if 'clusters' in response:
for cluster in response['clusters']:
print("%s,%s,%d" % (project, cluster['name'], cluster['currentNodeCount']))
Sample result ( project,clusterName,currentNodeCount
) : 样本结果( project,clusterName,currentNodeCount
):
project1,cluster1-in-project1,3
project1,cluster2-in-project1,2
project2,cluster1-in-project2,6
This example is based on the example provided in the documentation of projects.locations.clusters.list
API . 本示例基于projects.locations.clusters.list
API文档中提供的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.