You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Terraform Managed Instance Group模块以更新实例配置

GCP托管实例组(MIG)Terraform配置切换至“更新实例配置”方案

问题背景

你使用Terraform的terraform-google-modules/vm/google//modules/mig模块(版本8.0.0)创建托管实例组,部署后控制台默认选中“保持相同实例配置”选项。尝试配置update_policy但未生效,现有配置如下:

module "mig" {
  source  = "terraform-google-modules/vm/google//modules/mig"
  version = "8.0.0"

  project_id        = var.project_id
  target_size       = var.target_number_of_instance
  hostname          = "mig-sample"
  instance_template = module.instance_template.self_link
  region            = var.region_name
  named_ports = [
    {
      name = "http",
      port = 8000
    }
  ]
  # update_policy = [{
  #   type                           = "PROACTIVE"
  #   instance_redistribution_type   = "PROACTIVE"
  #   minimal_action                 = "REPLACE"
  #   most_disruptive_allowed_action = "REPLACE"
  #   max_surge_fixed                = 0
  #   max_surge_percent              = null
  #   max_unavailable_percent        = null
  #   max_unavailable_fixed          = 4
  #   min_ready_sec                  = 50
  #   replacement_method             = "RECREATE"
  # }]
}

解决步骤

1. 定位核心问题

该模块的update_policy参数接受单个对象,而非数组。你之前的配置用[]将其包裹为数组,导致Terraform无法正确解析,因此配置未生效。

2. 正确配置示例

取消注释并修改update_policy为单个对象格式,其中type = "PROACTIVE"对应控制台的“更新实例配置”选项(默认OPPORTUNISTIC对应“保持相同实例配置”):

module "mig" {
  source  = "terraform-google-modules/vm/google//modules/mig"
  version = "8.0.0"

  project_id        = var.project_id
  target_size       = var.target_number_of_instance
  hostname          = "mig-sample"
  instance_template = module.instance_template.self_link
  region            = var.region_name
  named_ports = [
    {
      name = "http",
      port = 8000
    }
  ]
  update_policy = {
    type                           = "PROACTIVE"
    instance_redistribution_type   = "PROACTIVE"
    minimal_action                 = "REPLACE"
    most_disruptive_allowed_action = "REPLACE"
    max_surge_fixed                = 0
    max_unavailable_fixed          = 4
    min_ready_sec                  = 50
    replacement_method             = "RECREATE"
  }
}

3. 关键参数说明

  • type = "PROACTIVE":开启主动更新模式,当实例模板变更时,MIG会主动更新所有实例,对应控制台“更新实例配置”选项。
  • minimal_action = "REPLACE":指定实例更新时的最小操作是替换实例(也可设为REFRESH仅刷新配置)。
  • max_unavailable_fixed = 4:更新期间允许同时不可用的实例数量,可根据业务容灾需求调整。

内容的提问来源于stack exchange,提问作者Asdfg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 01:00:59