如何使用Pulumi获取集群节点IP



以下是在DigitalOcean上创建托管Kubernetes集群的示例。

import * as pulumi from "@pulumi/pulumi";
import * as digitalocean from "@pulumi/digitalocean";
const foo = new digitalocean.KubernetesCluster("foo", {
region: "nyc1",
version: "1.20.2-do.0",
nodePool: {
name: "front-end-pool",
size: "s-2vcpu-2gb",
nodeCount: 3,
},
});

示例代码取自Pulumi DigitalOcean软件包文档。

如何检索用于创建DnsRecord资源的液滴节点IPv4地址?

const _default = new digitalocean.Domain("default", {name: "example.com"});
// This code doesn't work because foo.nodePool is just the inputs.
const dnsRecords = foo.nodePool.nodes(node => new digitalocean.DnsRecord("www", {
domain: _default.name,
type: "A",
value: node.ipv4Address,
}));

DigitalOcean不会返回您创建的Kubernetes集群的节点IP地址列表。您可以使用getDroplet函数检索这些值。

然而,您需要在apply()中进行此迭代,如下所示:

const addresses = foo.nodePool.nodes.apply(
nodes => nodes.forEach(
(node) => {
let n = digitalocean.getDropletOutput({
name: node.name
})
new digitalocean.DnsRecord("www", {
domain: domain.name,
type: "A",
value: n.ipv4Address,
})
}
)
)

在这里使用应用程序可以让我们等到API创建foo.nodePool.nodes。然后,我们可以像普通数组一样对其进行迭代,获得液滴,将其分配给变量n,然后为每个节点创建一个新的DNS记录

最新更新