0

如何使用 REST(或节点)API获取 GKE 节点池的当前大小?

我正在使用运行在我的集群上的 Express 应用程序管理我自己的工作池,并且可以设置池的大小并跟踪 setSize 操作的成功,但我看不到用于获取当前节点数的 API。NodePool 资源只包含原始节点数,不包含当前数。我不想在我的一个生产虚拟机上使用 gcloud 或 kubectl。

我可以绕过 GKE 并尝试使用 Compute Engine (GCE) API 来推断大小,但我还没有研究过这种方法。请注意,即使从 Stack Driver 获取节点数似乎也很困难。有没有人找到任何解决方法来获取当前节点大小?

4

1 回答 1

1

通过获取与节点池关联的实例组,可以从 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
}

请注意,这是使用两种不同的 Node API 机制。我们可以使用google.compute而不是@google-cloud/compute. 此外,这两个 API 的身份验证方式不同。前者使用authorize()获取客户端的方法,而后者通过环境变量中设置的默认帐户进行身份验证。

于 2021-03-20T18:39:03.123 回答