作業套件代理程式

+

架構

作業套件代理程式會建立虛擬機器,並安裝作業套件代理程式,以便整合監控和記錄服務。使用方式如下:

  • Compute Engine
  • 監控
  • 記錄

這個範例會設定單一簡單的虛擬機器,並將其連線至記錄功能。


開始使用

按一下以下連結,前往 Cloud Shell 中的原始碼副本。完成後,您只需執行單一指令,即可在專案中啟動應用程式的可用副本。

在 Cloud Shell 開啟

在 GitHub 上查看原始碼


作業套件代理程式元件

作業套件代理程式架構會使用多項產品。以下列出這些元件,並提供相關資訊,包括相關影片、產品說明文件和互動式操作說明的連結。
影片 文件 逐步操作說明
Compute Engine Compute Engine 是 Google Cloud 的虛擬技術。您可以使用這項功能啟動多種不同的 VM 設定,以符合各種運算需求。
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
}

建立虛擬機器

建立 VM。

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"]
  }
}

安裝作業套件代理程式

作業套件代理程式是從 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"
        },
    ]
}

結論

執行完畢後,您應該會擁有已正確設定的 VM,可將事件記錄到 Google Cloud Logging。此外,您應該擁有所有程式碼,以便修改或擴充這個解決方案,以便符合您的環境。