Ops エージェント

アーキテクチャ

Ops エージェントが仮想マシンを作成し、Monitoring サービスと Logging サービスと統合するために Ops エージェントをインストールします。以下を使用します。

  • Compute Engine
  • Monitoring
  • ロギング

この例では、シンプルな単一の仮想マシンを設定し、Logging に報告できるように接続します。


使ってみる

Cloud Shell でソースコードのコピーへの次のリンクをクリックします。その後、1 つのコマンドでプロジェクト内のアプリケーションの作業コピーがスピンアップされます。

Cloud Shell で開く

GitHub でソースコードを見る


Ops エージェント コンポーネント

Ops エージェントのアーキテクチャでは、いくつかのプロダクトを使用しています。以下に、関連動画、プロダクト ドキュメント、インタラクティブ チュートリアルへのリンクを含めた、コンポーネントの詳細を示します。
動画 ドキュメント チュートリアル
Compute Engine Compute Engine は Google Cloud の仮想技術です。VM のさまざまな構成を起動して、どのようなコンピューティング ニーズにも対応できます。
Cloud Monitoring Cloud Monitoring では、アプリケーションとインフラストラクチャのパフォーマンス、可用性、健全性を可視化できます。
Cloud Logging Cloud Logging は、エクサバイト規模で行われるストレージ、検索、分析、アラートを備えたフルマネージドでリアルタイムなログ管理機能を提供します。

スクリプト

インストール スクリプトでは、go と Terraform CLI ツールで記述された実行ファイルを使用して、空のプロジェクトを作成し、そこにアプリケーションをインストールします。出力は、機能するアプリケーションとロード バランシング IP アドレスの URL になります。

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

Ops エージェントをインストール

Ops エージェントは、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 にイベントを記録するよう適切に構成された VM が作成されました。さらに、環境に合わせてソリューションを変更または拡張するためのコードもすべて用意されています。