[英]GCP List vm instances and Get External IPs
I am at wits end searching for several days now, I am no terraform expert but I cant seem to find anyway to get all the GCP Vm instance List of external ips inside of a Kubernetes cluster as i then need to use this list to add them to allowed list for Database access.我现在不知不觉地搜索了几天,我不是 terraform 专家,但我似乎无法找到获取所有 GCP Vm 实例的 Kubernetes 集群内部外部 ips 列表,因为我需要使用此列表来添加它们到允许的数据库访问列表。
Can someone point me to an example or what有人可以指出我的例子或什么
tried something like this but cant use * =\\尝试过这样的事情,但不能使用 * =\\
data "google_compute_instance" "instances" {
}
output "instance_ids" {
value = "${data.google_compute_instance.instances.*.network_interface.0.access_config.0.nat_ip }"
}
I came up with one way to do this using Terraform, but it's slightly hacky as it involves a bit of string manipulation on some of the URLs due to the GKE cluster metadata returning a list of Instance Group Managers and not Instance Groups (despite what the attribute is labelled).我想出了一种使用 Terraform 执行此操作的方法,但由于 GKE 集群元数据返回实例组管理器列表而不是实例组(尽管属性被标记)。 However it does work for the cluster I tested it on.
但是它确实适用于我测试过的集群。
If it helps, here's a Terraform example that outputs the external IPs for all the nodes compute instances:如果有帮助,这里有一个 Terraform 示例,它输出所有节点计算实例的外部 IP:
provider "google" {
version = "~> 2"
project = "my-project"
}
// GKE cluster details
data "google_container_cluster" "my_cluster" {
name = "my-cluster-name"
location = "my-location"
}
// GKE node instance group details
data "google_compute_instance_group" "node_instance_groups" {
for_each = toset(data.google_container_cluster.my_cluster.node_pool[0].instance_group_urls)
self_link = replace(each.key, "instanceGroupManagers", "instanceGroups")
}
// GKE node compute instance details
data "google_compute_instance" "nodes" {
for_each = toset(flatten([for x in data.google_compute_instance_group.node_instance_groups : x.instances[*]]))
self_link = each.key
}
// Return the external IPs for all GKE node instances
output "external_ips" {
value = [for x in data.google_compute_instance.nodes : x.network_interface[0].access_config[0].nat_ip]
}
Rather than think about the reality that GKE nodes are implemented as Compute Engines, maybe think about GKE nodes in their Kubernetes concept.与其考虑 GKE 节点作为计算引擎实现的现实,不如考虑在其 Kubernetes 概念中的 GKE 节点。 As a test, I created a cluster with 3 nodes and then ran:
作为测试,我创建了一个包含 3 个节点的集群,然后运行:
kubectl get nodes -o wide
The result was结果是
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
gke-c1-default-pool-5b6b460a-59nb Ready <none> 68s v1.13.11-gke.14 10.128.0.42 35.222.104.41 Container-Optimized OS from Google 4.14.138+ docker://18.9.7
gke-c1-default-pool-5b6b460a-ggh9 Ready <none> 68s v1.13.11-gke.14 10.128.0.41 35.192.152.130 Container-Optimized OS from Google 4.14.138+ docker://18.9.7
gke-c1-default-pool-5b6b460a-j8nn Ready <none> 67s v1.13.11-gke.14 10.128.0.40 104.197.68.223 Container-Optimized OS from Google 4.14.138+ docker://18.9.7
Please pay attention to the column called EXTERNAL-IP.请注意名为 EXTERNAL-IP 的列。 I then compared these against the Compute Engine VM instance exposed public IP and found them to be identical.
然后,我将这些与 Compute Engine VM 实例公开的公共 IP 进行了比较,发现它们是相同的。
Now that we see that examining the nodes using kubectl returns the information we desire, we can now potentially use a Terraform equivalent to this command.现在我们看到使用 kubectl 检查节点会返回我们想要的信息,我们现在可以使用与此命令等效的 Terraform。 For example, the Kubernetes Provider .
例如, Kubernetes 提供程序。
My terraform knowledge is a little haisy but can you not do the following?我的 terraform 知识有点杂乱无章,但您不能执行以下操作吗?
As mentioned on this page: https://www.terraform.io/docs/provisioners/local-exec.html如本页所述: https : //www.terraform.io/docs/provisioners/local-exec.html
resource "gcp_instance" "web" {
# ...
provisioner "local-exec" {
command = "echo gcloud compute instances list --format=\"value(External IP)\" --filter=\"name~'gke-$cluster_name-$pool_name*' --format=\"value(External_IP)\" "} >> public_ips.txt"
}
}
then read it from the file?然后从文件中读取它?
The sortest solution to this problem is by using the gcloud command-line tool in conjunction with awk to manipulate the output.此问题的最佳解决方案是将 gcloud 命令行工具与 awk 结合使用来操作输出。
With 2 simple commands, you get a nice formatted output.使用 2 个简单的命令,您可以获得一个漂亮的格式化输出。
First, calculate the number of nodes:首先,计算节点数:
num_nodes=$(kubectl get nodes | awk 'END{print NR - 1}')
After后
gcloud compute instances list | awk '{print $1,$5}' | tail -n $num_nodes
If you want to create a dynamic Ansible inventory, then the above command becomes:如果要创建动态 Ansible 库存,则上述命令变为:
gcloud compute instances list | awk '{print $1, "ansible_ssh_host="$5}' | tail -n $num_nodes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.