Terraform和GCS:如何创建多区域LB.

时间:2018-03-09 16:23:00

标签: google-cloud-platform terraform

我想创建一个负载均衡器,在GCP中的同一区域的多个区域上处理两个或多个实例

我是这样开始的: - 创建一个后台服务,用于处理两个实例组:

resource "google_compute_backend_service" "www-service" {
  name     = "${var.environment}-www-service"
  protocol = "HTTP"
  port_name   = "http"

  backend {
    group = "${google_compute_instance_group.instance-group-0.self_link}"
  }

  backend {
    group = "${google_compute_instance_group.instance-group-1.self_link}"
  }

  health_checks = ["${google_compute_health_check.health-check1.self_link}"]
}

然后我有两个实例组,每个实例组都有一个具有该语法的实例:

resource "google_compute_instance_group" "instance-group-0" {
  count        = "${var.web_count}"
  name = "${var.environment}-instance-group-0"

  instances = ["${google_compute_instance.www.self_link}"]

  named_port {
    name = "http"
    port = "80"
  }
  network = "${google_compute_network.platform-network.self_link}"
}

我收到错误:

google_compute_backend_service.www-service: Resource 'google_compute_instance_group.instance-group-0' not found for variable 'google_compute_instance_group.instance-group-0.self_link'

我看到在backend_service中切换后端/组声明会将错误移动到group-1,所以我可以猜测这不是正确的语法,尽管您可以在Google GUI中创建一个包含多个实例组的backend_service。

我有两个问题:

Q1。如何创建具有多个实例组的backend_service? 什么是正确的语法?

Q2。是否可以通过以下语法引用compute_instance_group中的compute_instance:

instances = ["${google_compute_instance.www.[count.index].self_link}"]

(以上语法不起作用)

2 个答案:

答案 0 :(得分:1)

感谢您的回答

最后,我在多个Github票证中找到了语法:

resource "google_compute_instance_group" "instance-group-0" {
  name = "${var.environment}-instance-group-0"
  zone = "${data.google_compute_zones.available.names[0]}" 

  instances = ["${slice(google_compute_instance.www.*.self_link, 0, floor(var.web_count/2)-1)}"]

  named_port {
    name = "http"
    port = "80"
  }
  network = "${google_compute_network.platform-network.self_link}"
} 

例如组1,但不同的切片

然后:

resource "google_compute_instance" "www" {
  count        = "${var.web_count}"
  zone         =  "${data.google_compute_zones.available.names[floor((2*count.index)/var.web_count)]"}

我必须说可以采取不同的设计决策:   - 使用区域托管实例组更简单,但模板是静态的   - 使用Kubernetes

答案 1 :(得分:0)

A1 - 每个后端服务只能使用单个实例组。您需要为每个组创建一个新的后端。然后,您可以将多个后端分配给负载均衡器。

另一方面,错误消息与您正在使用的资源类型有关。以下是unmanaged Instance groupsManaged instance groupsbackend services的资源页面。您可以找到不同compute APIs here的列表。

A2-你应该使用$(ref.name.selfLink)代替,我相信这个语法应该可行,你仍然可以使用你的变量。另外,请务必更新adding instances to the group

的API调用语法
相关问题