terraform GCP http(s)负载均衡器

时间:2020-01-22 17:37:08

标签: google-cloud-platform terraform-provider-gcp

我正在尝试在GCP上使用Terraform创建一个HTTP(S)负载平衡器。我希望它同时为HTTP和HTTPS客户端提供服务器。我正在使用以下方法创建LB的前端部分(google_compute_global_forwarding_rule)。

// SSL
resource "google_compute_global_forwarding_rule" "default-ssl" {
  name       = "frontend-https"
  target     = google_compute_target_https_proxy.default-ssl.self_link
  port_range = "443"
}


resource "google_compute_target_https_proxy" "default-ssl" {
  provider         = google-beta
  name             = "target-proxy-ssl"
  description      = "a description"
  ssl_certificates = ["mysslcert"]
  url_map          = google_compute_url_map.default.self_link
}

// non SSL
resource "google_compute_global_forwarding_rule" "default" {
  name       = "frontend-http"
  target     = google_compute_target_http_proxy.default.self_link
  port_range = "80"
}

resource "google_compute_target_http_proxy" "default" {
  project     = var.project_id
  provider    = google-beta
  name        = "target-proxy"
  description = "a description"
  url_map     = google_compute_url_map.default.self_link
}

问题是,它分配了两个IP地址。一种用于HTTP,另一种用于HTTPS。 但是,当我在GCP上手动创建负载均衡器(没有terraform)时,我可以创建IP地址并选择协议。通过这样做,我可以在创建下一个前端规则时使用相同的IP地址。 adding-an-image

已创建地形;

enter image description here

手动创建;

enter image description here

在创建仅具有一个IP地址的负载均衡器时获得帮助。

2 个答案:

答案 0 :(得分:0)

您还可以通过以下方式动态分配外部IP:

resource "google_compute_global_address" "L7LB_IP_ADDRESS" {
  name                  = "l7lb-external-ip-address"
}

然后在转发规则(前端)中,设置IP地址:

resource "google_compute_global_forwarding_rule" "EXTERNAL_FWD_RULE_HTTP" {
  name                  = "frontend-80"
  ip_address            = google_compute_global_address.L7LB_IP_ADDRESS.address
  port_range            = "80"
}
resource "google_compute_global_forwarding_rule" "EXTERNAL_FWD_RULE_HTTPS" {
  name                  = "frontend-443"
  ip_address            = google_compute_global_address.L7LB_IP_ADDRESS.address
  port_range            = "443"
}

答案 1 :(得分:0)

提供的 IP 地址资源需要在 Terraform 中具有 SHARED_LOADBALANCER_VIP 用途

SHARED_LOADBALANCER_VIP 表示可供多个内部负载平衡器使用的地址。 https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_address