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

如何用Terraform管理GCP Ops Agent?部署问题排查

问题分析与解决方案

核心问题1:OS版本配置不匹配

你提供的Terraform脚本中,os_types指定的是Ubuntu 20.04,但目标是Ubuntu 22实例,这直接导致策略无法匹配目标实例,自然不会触发代理安装。这是实例未成功安装Ops Agent的首要原因。

Beta版本策略的影响

策略处于Beta版本本身不会导致安装失败,Beta版本仅代表功能处于预览阶段,GA版本为正式稳定版。但建议优先使用GA版本的命令和资源,避免潜在兼容性问题。你可以用GA版本的gcloud命令查看策略:

gcloud compute instances ops-agents policies describe ops-agents-ububtu-22-cc-os-policy --project XXXXXXXXX

其他可能遗漏的配置

  1. OS Config API未启用:Ops Agent策略依赖GCP的OS Config服务,必须确保该服务已在项目中启用,否则策略无法下发到实例。
  2. Terraform模块版本过旧:如果使用的terraform-google-modules/cloud-operations模块版本较老,可能不支持Ubuntu 22.04的识别,需确保使用最新稳定版模块。

修正后的Terraform脚本

# 启用OS Config服务(若未启用)
resource "google_project_service" "osconfig" {
  project = var.gcp_project
  service = "osconfig.googleapis.com"

  disable_on_destroy = false
}

module "agent_policy_detailed" {
  source  = "terraform-google-modules/cloud-operations/google//modules/agent-policy"
  # 指定最新稳定模块版本,避免兼容性问题
  version = "~> 3.0"
  project_id = var.gcp_project
  policy_id = "ops-agents-ubuntu-22-${var.env}-os-policy"
  agent_rules = [
    {
      type               = "ops-agent"
      version            = "current-major"
      package_state      = "installed"
      enable_autoupgrade = true
    }
  ]
  os_types = [
    {
      short_name = "ubuntu"
      version    = "22.04"
    },
  ]

  # 依赖OS Config服务启用完成后再创建策略
  depends_on = [google_project_service.osconfig]
}

验证步骤

  1. 应用修正后的Terraform配置:
terraform init -upgrade
terraform plan
terraform apply
  1. 查看GA版本策略详情,确认OS类型已更新:
gcloud compute instances ops-agents policies describe ops-agents-ubuntu-22-${var.env}-os-policy --project XXXXXXXXX
  1. 等待10-15分钟(策略下发需要时间),在目标Ubuntu22实例上检查Ops Agent状态:
sudo systemctl status google-cloud-ops-agent

内容的提问来源于stack exchange,提问作者devops on the road

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 23:48:21