1

我使用在 GCP 控制台上创建了一个全局(多区域)TCP(代理)LB

  1. 前端配置
  2. 后端类型的四种后端配置: 4 个不同区域的实例组
  3. 以及一个完整的后端配置的健康检查

现在同样无法使用 Terraform 创建下面是我的完整代码:

问题:负载均衡器未使用名称创建:google_compute_target_tcp_proxy中使用名称创建仅在google_compute_backend_service如果只传递一个后端,如果我传递多个通过计数传递的后端,则创建多个负载均衡器,而不是将所有后端附加到一个负载均衡器。谁能建议如何将多个后端附加到单个google_compute_target_tcp_proxy?我是 terraform 新手,我在 Terraform 文档中没有找到任何详细信息。

 provider "google" {
  credentials = file(var.credentials_file)
  project     = var.project_id
}

provider "google-beta" {
  credentials = file(var.credentials_file)
  project     = var.project_id
}


resource "google_compute_global_forwarding_rule" "default" {
  #count      = length(var.zones)
  name       = "frontend-service-mig-test" #We can have single FE IP
  #target     = google_compute_target_tcp_proxy.default[count.index].id
  target     = google_compute_target_tcp_proxy.default.id
  port_range = "443"
  load_balancing_scheme = "EXTERNAL"
}

resource "google_compute_target_tcp_proxy" "default" {
  #count       = length(var.zones)
  name            = "test-proxy" # This name wont be visible on gui.
  #backend_service = google_compute_backend_service.default[count.index].id
  backend_service = google_compute_backend_service.default.id
}

resource "google_compute_backend_service" "default" {
  count       = length(var.zones)
  name        = "mig-test-${count.index}-backend-service"
  load_balancing_scheme = "EXTERNAL"
  protocol    = "TCP"
  timeout_sec = 10
  port_name   = "https"
  health_checks = [google_compute_health_check.default.id]
  backend {
  #group       = "https://www.googleapis.com/compute/v1/projects/terraform-playground-301207/zones/northamerica-northeast1-a/instanceGroups/mig-test-0"
  group       = "https://www.googleapis.com/compute/v1/projects/terraform-playground-301207/zones/${var.zones[count.index]}/instanceGroups/mig-test-${count.index}"
  balancing_mode  = "UTILIZATION"
  capacity_scaler = 1
  max_utilization = 0.8
  }
}

resource "google_compute_health_check" "default" {
  count               = length(var.zones)
  provider            = google-beta
  name                = "health-check-mig-test-${count.index}"
  timeout_sec         = 5
  check_interval_sec  = 5
  healthy_threshold   = 2
  unhealthy_threshold = 2
  
  log_config  {
    enable = false
  }            
  tcp_health_check {
    port = "443"
  }

}
4

1 回答 1

1

Solution is adding multiple backends to backend resource itself , i used dynamic feature for same.

resource "google_compute_global_forwarding_rule" "default" {
  name       = var.fe_name #We can have single FE IP
  target     = google_compute_target_tcp_proxy.default.id
  port_range = var.be_protocol_range
  load_balancing_scheme = var.lb_scheme
}

resource "google_compute_target_tcp_proxy" "default" {
  name            = var.loadbalancername # This name wont be visible on gui.
  backend_service = google_compute_backend_service.default.id
}

resource "google_compute_backend_service" "default" {
  name        = var.loadbalancername
  load_balancing_scheme = var.lb_scheme
  protocol    = var.be_protocol
  timeout_sec = var.be_timeout_sec
  port_name   = var.be_protocol_name
  health_checks = [google_compute_health_check.default.id]

  dynamic backend {
        for_each = var.zones
    content {
         group       = "https://www.googleapis.com/compute/v1/projects/terraform-playground-301207/zones/${backend.value}/instanceGroups/${var.appname}-${var.regions[backend.key]}"
         balancing_mode  = var.be_balancing_mode
         capacity_scaler = var.be_capacity_scaler
         max_utilization = var.be_max_utilization
    }
  }
}

resource "google_compute_health_check" "default" {
  provider            = google-beta
  name                = var.be_healthcheck_name
  timeout_sec         = var.be_healthcheck_timeout_sec
  check_interval_sec  = var.be_healthcheck_interval_sec
  healthy_threshold   = var.be_healthcheck_threshold
  unhealthy_threshold = var.be_unhealthycheck_threshold
  
  log_config  {
    enable = var.be_healthycheck_logconfig
  }            
  tcp_health_check {
    port = var.be_healthycheck_port
  }

}
于 2021-01-19T08:28:49.753 回答