微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

如何获取 GKE 节点池 using the REST (or Node) API 的当前大小?

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

我可以绕过 GKE 并尝试使用 Compute Engine (GCE) API 推断大小,但我还没有研究过这种方法。请注意 it seems difficult to get the node count even from Stack Driver。有没有人找到任何解决方法获取当前节点大小?

解决方法

可以通过获取与节点池关联的实例组从 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() 方法获取客户端,而后者通过环境变量中设置的默认帐户进行身份验证。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。