【问题标题】:How to load balance google compute instance using terraform?如何使用 terraform 对谷歌计算实例进行负载平衡?
【发布时间】:2018-01-14 20:29:12
【问题描述】:

在我的 terraform 配置文件中,我这样定义我的资源:

resource "google_compute_instance" "test" {
    ...
    count = 2
}

我现在想要的是创建负载均衡器,它将在我的谷歌计算实例的两个实例之间进行平衡。不幸的是,我在文档中找不到与此任务相关的任何内容。 google_compute_target_poolgoogle_compute_lb_ip_ranges 似乎与我的问题无关。

【问题讨论】:

    标签: google-cloud-platform terraform


    【解决方案1】:

    您必须使用此 terraform document 上指示的“转发规则”。要使用负载平衡和协议转发,您必须创建将流量定向到特定目标实例的转发规则。转发规则在云平台上的使用可以找here

    【讨论】:

    • 谢谢您,先生!我会检查的!
    【解决方案2】:

    在常见的情况下,您可以使用以下内容:

    resource "google_compute_instance" "test" {
        name         = "nlb-node${count.index}"
        zone         = "europe-west3-b"
        machine_type = "f1-micro"
        count        = 2
    
        boot_disk {
            auto_delete = true
            initialize_params {
                image       = "ubuntu-os-cloud/ubuntu-1604-lts"
                size        = 10
                type        = "pd-ssd"
            }
        }
    
        network_interface {
            subnetwork = "default"
    
            access_config {
                nat_ip = ""
            }
        }
    
        service_account {
            scopes = ["userinfo-email", "compute-ro", "storage-ro"]
        }
    }
    
    resource "google_compute_http_health_check" "nlb-hc" {
        name               = "nlb-health-checks"
        request_path       = "/"
        port               = 80
        check_interval_sec = 10
        timeout_sec        = 3
    }
    
    resource "google_compute_target_pool" "nlb-target-pool" {
        name             = "nlb-target-pool"
        session_affinity = "NONE"
        region           = "europe-west3"
    
        instances = [
            "${google_compute_instance.test.*.self_link}"
        ]
    
        health_checks = [
            "${google_compute_http_health_check.nlb-hc.name}"
        ]
    }
    
    resource "google_compute_forwarding_rule" "network-load-balancer" {
        name                  = "nlb-test"
        region                = "europe-west3"
        target                = "${google_compute_target_pool.nlb-target-pool.self_link}"
        port_range            = "80"
        ip_protocol           = "TCP"
        load_balancing_scheme = "EXTERNAL"
    }
    

    您可以通过${google_compute_forwarding_rule.network-load-balancer.ip_address}获取负载均衡器外部ip

    // output.tf
    output "network_load_balancer_ip" {
        value = "${google_compute_forwarding_rule.network-load-balancer.ip_address}"
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-02
      • 2017-01-20
      • 2015-05-02
      • 2015-12-25
      • 2015-10-22
      • 1970-01-01
      • 2015-02-20
      • 2012-09-29
      • 2020-06-20
      相关资源
      最近更新 更多