Ops Agent 会创建一个虚拟机并安装 Ops Agent,以便与 Monitoring 和 Logging 服务集成。它使用:
- Compute Engine
 - 监控
 - 日志记录
 
该示例设置了一个简单的单个虚拟机,并将其连接到 Logging 以进行报告。
开始使用
点击以下链接,在 Cloud Shell 中查看源代码的副本。进入该环境后,只需一个命令即可在项目中启动应用的工作副本。
Ops Agent 组件
Ops Agent 架构会使用多种产品。 以下列出了这些组件,以及有关这些组件的更多信息,包括指向相关视频、产品文档和互动式演示文稿的链接。脚本
安装脚本使用使用 go 和 Terraform CLI 工具编写的可执行文件,获取一个空项目并在其中安装应用。输出应为一个正常运行的应用和负载均衡 IP 地址的网址。
./main.tf
启用服务
默认情况下,Google Cloud 服务在项目中处于停用状态。如需使用本文中的任何解决方案,我们必须启用以下功能:
- Compute Engine - 虚拟机和网络
 
variable "gcp_service_list" {
    description = "The list of apis necessary for the project"
    type        = list(string)
    default = [
        "compute.googleapis.com",
    ]
}
resource "google_project_service" "all" {
  for_each                   = toset(var.gcp_service_list)
  project                    = var.project_number
  service                    = each.key
  disable_dependent_services = false
  disable_on_destroy         = false
}
创建虚拟机
创建虚拟机。
resource "google_compute_instance" "default" {
  project      = var.project_id
  name         = "${var.basename}-instance"
  machine_type = "f1-micro"
  zone         = var.zone
  boot_disk {
    initialize_params {
      image = "centos-cloud/centos-8-v20210817"
    }
  }
  labels = {
    env        = "prod"
    app        = "myproduct"
    created_by = "terraform"
  }
  network_interface {
    network = "default"
    access_config {
      // Include this section to give the VM an external ip address
    }
  }
  service_account {
    // Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
    // This non production example uses the default compute service account.
    email  = local.sacompute
    scopes = ["cloud-platform"]
  }
}
安装 Ops Agent
Ops Agent 是用于从 Compute Engine 实例收集遥测数据的主要代理。该视频将向您展示如何使用 Terraform 进行安装。
module "agent_policy" {
    source  = "terraform-google-modules/cloud-operations/google//modules/agent-policy"
    version = "~> 0.1.0"
    
    project_id = var.project_id
    policy_id  = "ops-agents-example-policy"
    agent_rules = [
        {
        type               = "ops-agent"
        version            = "current-major"
        package_state      = "installed"
        enable_autoupgrade = true
        },
    ]
    group_labels = [
        {
        env        = "prod"
        app        = "myproduct"
        created_by = "terraform"
        }
    ]
    
    os_types = [
        {
        short_name = "centos"
        version    = "8"
        },
    ]
}
总结
运行后,您现在应该有一个已正确配置的虚拟机,可将事件记录到 Google Cloud Logging。此外,您应该拥有修改或扩展此解决方案以适应您环境的所有代码。