Ops Agent

使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。

架构

Ops Agent 会创建一个虚拟机并安装 Ops Agent 以与 Monitoring 和 Logging 服务集成。它使用:

  • Compute Engine
  • 监控
  • 日志记录

Example 会设置一个简单的虚拟机,并将其连接到虚拟机以向 Logging 报告。


开始

点击以下链接,转到 Cloud Shell 中源代码的副本。完成后,一条命令将启动项目中应用的工作副本。

在 Cloud Shell 中打开

在 GitHub 上查看源代码


Ops Agent 组件

Ops Agent 架构使用多种产品。下面列出了这些组件,并列出了关于这些组件的更多信息,包括相关视频的链接、产品文档和互动式演示。
视频 文档 演示
Compute Engine Compute Engine 是 Google Cloud 的虚拟技术。借助它,您可以启动许多不同的虚拟机配置,以满足您的任何计算需求。
Cloud Monitoring Cloud Monitoring 可帮助您了解应用和基础架构的性能、可用性和健康状况
Cloud Logging Cloud Logging 提供全代管式实时日志管理服务,可提供 EB 级的存储空间、搜索、分析和提醒功能。

脚本

安装脚本使用用 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 中。此外,您应该能够根据自己的环境修改或扩展此解决方案。