问题描述
我使用
在 GCP 控制台上创建了一个全局(多区域)TCP(代理)LB- 单一前端配置
- 后端类型的四种后端配置:4个不同区域的实例组。
- 还有一次运行状况检查,用于完整的后端配置
现在同样无法使用 terraform 创建,下面是我的完整代码:
问题:负载均衡器没有在 google_compute_target_tcp_proxy 中使用 name: 创建,而是使用 创建名称: 仅在 google_compute_backend_service 中,如果只传递一个后端,如果我传递多个以 count 传递的后端,则创建多个负载均衡器而不是将所有后端附加到一个负载均衡器。任何人都可以建议如何将多个后端附加到单个 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"
}
}
解决方法
解决方案是向后端资源本身添加多个后端,我为此使用了动态功能。
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
}
}