简体   繁体   English

Google云端:使用python API获取GKE统计信息

[英]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集群中的节点数,您需要:

  • use glcoud command to switch to the appropriate project 使用glcoud命令切换到适当的项目
  • use 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.

相关问题 使用 Python 的 Google Cloud Billing API - Google Cloud Billing API using Python 在 Python 中使用 API 创建 Google Cloud Function - Create Google Cloud Function using API in Python 如何使用云函数(python)从 GKE(GCP)获取图像和环境变量? - How can I get images and environment variables from GKE(GCP) using cloud function(python)? Elasticsearch Python API GET索引统计信息 - Elasticsearch python api GET index stats 无法从 IEX Cloud 获取高级统计信息 API - Cannot get an advanced stats from IEX Cloud API 使用api python客户端获取Google云容器集群置备的状态 - Get the status of google cloud container cluster provisioning using api python client 如何列出所有应用程序以使用 python API 在谷歌云平台的应用程序引擎上获取 appID - How to list all apps to get appID on app engine in google cloud platform using python API 将 Google Calendar API 与 Python 3.7 Google Cloud Function 结合使用 - Using Google Calendar API with a Python 3.7 Google Cloud Function Python:如何使用 pyaudio 为 Google Cloud Speech API 获取原始音频文件 - Python: How to get raw audio file using pyaudio for Google Cloud Speech API 在Google云端点项目(Python)中使用其他Google API - Using other Google API's in a Google cloud endpoints project (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM