简体   繁体   English

GKE REST/Node API 调用以获取池中的节点数?

[英]GKE REST/Node API call to get number of nodes in a pool?

How can I get the current size of a GKE node pool using the REST (or Node) API ?如何 使用 REST (或节点) API获取 GKE 节点池的当前大小?

I'm managing my own worker pool using my Express app running on my cluster, and can set the size of the pool and track the success of the setSize operation, but I see no API for getting the current node count.我正在使用在集群上运行的 Express 应用程序管理我自己的工作池,并且可以设置池的大小并跟踪 setSize 操作的成功,但我没有看到 API 用于获取当前节点数。 The NodePool resource only includes the original node count, not the current count. NodePool 资源仅包含原始节点数,不包含当前数。 I don't want to use gcloud or kubectl on one of my production VMs.我不想在我的一个生产虚拟机上使用 gcloud 或 kubectl。

I could go around GKE and try to infer the size using the Compute Engine (GCE) API, but I haven't looked into that approach yet.我可以在 GKE 周围使用 go 并尝试使用计算引擎 (GCE) API 来推断大小,但我还没有研究过这种方法。 Note that it seems difficult to get the node count even from Stack Driver .请注意, 即使从 Stack Driver 获取节点数似乎也很困难 Has anyone found any workarounds to get the current node size?有没有人找到任何解决方法来获取当前节点大小?

The worker pool size can be retrieved from the Compute Engine API by getting the instance group associated with the node pool.通过获取与节点池关联的实例组,可以从 Compute Engine API 检索工作器池大小。

const { google } = require('googleapis')
const Compute = require('@google-cloud/compute')

const container = google.container('v1')
const compute = new Compute()

const projectId = 'project-12345'
const zone = 'us-central1-a'
const nodePoolId = 'worker-pool'
const clusterId = 'cluster-name'

async function authorize() {
  const auth = new google.auth.GoogleAuth({
    scopes: [ 'https://www.googleapis.com/auth/cloud-platform' ],
  })
  return auth.getClient()
}

const getNodePoolSize = async () => {
  const auth = await authorize()
  const clusterName = `projects/${projectId}/zones/${zone}/clusters/${clusterId}`
  const request = { name: clusterName, auth }
  const response = await container.projects.locations.clusters.get(request)
  const nodePool = response.data.nodePools.find(({ name }) => name === nodePoolId)
  const igName = nodePool.instanceGroupUrls[0].match(/.*\/instanceGroupManagers\/([a-z0-9-]*)$/)[1]
  const instanceGroup = await compute.zone(zone).instanceGroup(igName).get()
  return instanceGroup[1 /* 0 is config, 1 is instance */].size
}

Note that this is using two different Node API mechanisms.请注意,这是使用两种不同的 Node API 机制。 We could use google.compute instead of @google-cloud/compute .我们可以使用google.compute代替@google-cloud/compute Also, the two APIs are authenticated differently.此外,这两个 API 的身份验证方式不同。 The former uses the authorize() method to get a client, while the latter is authenticated via the default account set in environment variables.前者使用authorize()方法获取客户端,而后者通过环境变量中设置的默认帐户进行身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM