为什么Terraform创建的负载均衡器无法连接我的后端?

问题描述 投票:0回答:1

我正在尝试使用terraform通过代码进行基础架构管理。但是我遇到了一个非常奇怪的问题,我根本无法解释。

首先,我可以使用terraform为实例组创建负载平衡器,并且所有操作都与在GCP控制台中手动创建的相同。没有错误报告。但是该terraform负载平衡器无法连接到后端。并且GCP记录器报告502错误,这表示负载均衡器出现了问题。但是,当我同时为实例组手动创建负载均衡器时,这意味着我将2个负载均衡器放在实例组的前面。一个是通过terraform创建的,另一个是手工创建的。当设置了手动创建的负载均衡器时,terraform负载均衡器和手动创建的负载均衡器也都可以连接到我的后端,并且运行良好。即使删除了手动创建的负载均衡器,terraform创建的负载均衡器也可以工作。

provider "google-beta" {
  credentials = file("cred.json")
  project = var.project_id
  region = var.region
  zone = var.zone
}

resource "google_compute_instance_template" "instance_template" {
  provider = "google-beta"
  name_prefix = "cadence-graphite-"
  machine_type = var.machine_type
  region = var.region

  disk {
    // Instance Templates reference disks by name, not self link
    source_image = "centos-7" 
    auto_delete = false
    boot        = false
  }


  lifecycle {
    create_before_destroy = true
  }

  network_interface {
    network = "default-network"
  }

  metadata_startup_script = file("startup.sh")
}

resource "google_compute_instance_group_manager" "instance_group_manager" {
  provider = "google-beta"
  name               = "cadence-graphite-manager"
  base_instance_name = "cadence-graphite"
  zone               = var.zone

  version {
     name = "instance_group_manager"
     instance_template = google_compute_instance_template.instance_template.self_link
  }

  target_size       = "1"

  update_policy {
    type                  = "PROACTIVE"
    minimal_action        = "REPLACE"
    max_surge_fixed       = "1"
    max_unavailable_fixed = "1"
  }
}

resource "google_compute_health_check" "autohealing" {
  provider = "google-beta"
  name = "cadence-graphite-health-check"
  check_interval_sec  = 10
  timeout_sec         = 5
  healthy_threshold   = 2

  http_health_check {
    port         = "8080"
  }
}

resource "google_compute_backend_service" "backend" {
  provider = "google-beta"
  name = "cadence-graphite-backend"
  health_checks = [google_compute_health_check.autohealing.self_link]

  port_name = "http"
  protocol = "HTTP"
  timeout_sec = 30

  backend {
     group = google_compute_instance_group_manager.instance_group_manager.instance_group
     balancing_mode = "RATE"
     capacity_scaler = 0.4
     max_rate_per_instance = 50
  }

  security_policy = "cadence-secuirty-policy"
}

resource "google_compute_url_map" "url-map" {
  provider = "google-beta"
  name            = "cadence-graphite-url-map"
  default_service = google_compute_backend_service.backend.self_link
}

resource "google_compute_global_address" "cadence-graphite-external" {
  name = "cadence-gloable-graphite-ip"
}

resource "google_compute_target_http_proxy" "http-proxy" {
  provider = "google-beta"
  name        = "cadence-graphite-proxy"
  description = "http proxy for cadence graphite"
  url_map     = google_compute_url_map.url-map.self_link
}

resource "google_compute_global_forwarding_rule" "gloable-rules" {
  provider = "google-beta"
  name       = "global-cadence-graphite-lb"
  ip_address = google_compute_global_address.cadence-graphite-external.address

  load_balancing_scheme = "EXTERNAL"
  target     = google_compute_target_http_proxy.http-proxy.self_link
  port_range = "8080"
}

google-cloud-platform terraform terraform-provider-gcp
1个回答
0
投票

基本上,此问题来自错误的后端设置。创建后端服务时,必须指定named_port,并且此named_port应该在group_manager中定义。然后,我创建的后端可以找出可以连接的端口。 terraform文档中未提及此部分。

© www.soinside.com 2019 - 2024. All rights reserved.