すべてのプロダクト
Search
ドキュメントセンター

Managed Service for Prometheus:Terraform を使用して Prometheus インスタンスのリモート書き込み設定を管理する

最終更新日:Jan 14, 2025

Terraform を使用して、Managed Service for Prometheus の統合センターのエクスポーターとリモート書き込みリソースを管理できます。このトピックでは、Terraform を使用してリモート書き込み設定を管理する方法について説明します。

前提条件

  • Container Service または ECS の Prometheus インスタンスが作成されていること。詳細については、「Terraform を使用して Prometheus インスタンスを管理する」をご参照ください。

  • Terraform がインストールされていること。

    • デフォルトでは、Cloud Shell には Terraform がプリインストールされており、アカウント情報が設定されています。設定を変更する必要はありません。

    • Cloud Shell を使用しない場合は、Terraform を直接インストールできます。詳細については、「Terraform のインストールと設定」をご参照ください。

      説明
      • terraform --version コマンドを実行して、Terraform のバージョンを確認する必要があります。Terraform V0.12.28 以降をインストールする必要があります。

      • Resource Orchestration Service(ROS)は Alibaba Cloud が提供するネイティブの Infrastructure as Code(IaC)サービスですが、Terraform テンプレートの統合もサポートしています。Terraform を ROS と併用することで、Alibaba Cloud、Amazon Web Services(AWS)、または Microsoft Azure のリソースを定義および管理し、リソースパラメーターを指定し、リソースの依存関係を構成できます。詳細については、「Terraform テンプレートの作成」および「Terraform スタックの作成」をご参照ください。

  • Alibaba Cloud アカウント情報が設定されていること。次のいずれかの方法を使用して、Alibaba Cloud アカウント情報を設定できます。

    説明

    権限管理の柔軟性とセキュリティを向上させるために、Terraform という名前の Resource Access Management(RAM)ユーザーを作成することをお勧めします。次に、RAM ユーザーの AccessKey ペアを作成し、RAM ユーザーに権限を付与します。詳細については、「RAM ユーザーの作成」および「RAM ユーザーへの権限の付与」をご参照ください。

    • 方法 1:認証情報を格納する環境変数を追加します。

      export ALICLOUD_ACCESS_KEY="************"
      export ALICLOUD_SECRET_KEY="************"
      export ALICLOUD_REGION="cn-beijing"
      説明

      ビジネス要件に基づいて export ALICLOUD_REGION パラメーターを指定します。

    • 方法 2:設定ファイルの provider セクションで ID 情報を指定します。

      provider "alicloud" {
        access_key = "************"
        secret_key = "************"
        region     = "cn-beijing"
      }
      説明

      ビジネス要件に基づいて export ALICLOUD_REGION パラメーターの値を指定します。

Prometheus インスタンスのリモート書き込み設定を作成する

  1. 作業ディレクトリと、そのディレクトリに main.tf という名前のファイルを作成します。

    provider "alicloud" {
    }
  2. 次のコマンドを実行して、Terraform のランタイム環境を初期化します。

    terraform init

    期待される出力:

    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1...
    ...
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. リモート書き込みリソースをインポートします。

    1. main.tf ファイルにリモート書き込みリソースを追加します。

      # Prometheus インスタンスのリモート書き込み設定。
      resource "alicloud_arms_remote_write" "myRemoteWrite1" {
          cluster_id       = "c77e1106f429e4b46b0ee1720c*****"   // Prometheus インスタンスの ID。
        remote_write_yaml = <<-EOT
                  remote_write:
                  - name: ArmsRemoteWrite1
                    url: http://47.96.XX.XX:8080/prometheus/xxx/yyy/cn-hangzhou/api/v3/write
                    basic_auth: {username: 666, password: '******'}
                    write_relabel_configs:
                    - source_labels: [instance_id]
                      separator: ;
                      regex: si-6e2ca86444db4e5*****
                      replacement: $1
                      action: keep
              EOT
      }
    2. 次のコマンドを実行して、実行プランを作成します。

      terraform plan

      期待される出力:

      Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
        + create
      
      Terraform will perform the following actions:
      
        # alicloud_arms_remote_write.ack-rw1 will be created
        + resource "alicloud_arms_remote_write" "ack-rw1" {
            + cluster_id        = "c77e1106f429e4b46b0ee1720c*****"
            + id                = (known after apply)
            + remote_write_name = (known after apply)
            + remote_write_yaml = <<-EOT
                  remote_write:
                  - name: ArmsRemoteWrite1
                    url: http://xx.xx.xx.xx:8080/prometheus/xxx/yyy/cn-hangzhou/api/v3/write
                    basic_auth: {username: 666, password: '******'}
                    write_relabel_configs:
                    - source_labels: [instance_id]
                      separator: ;
                      regex: si-6e2ca86444db4e5*****
                      replacement: $1
                      action: keep
              EOT
          }
      
      Plan: 1 to add, 0 to change, 0 to destroy.
    3. 次のコマンドを実行して、リモート書き込み設定を作成します。

      terraform apply

      期待される出力:

      Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
        + create
      
      Terraform will perform the following actions:
      
        # alicloud_arms_remote_write.ack-rw1 will be created
        + resource "alicloud_arms_remote_write" "ack-rw1" {
            + cluster_id        = "c77e1106f429e4b46b0ee1720c9*****"
            + id                = (known after apply)
            + remote_write_name = (known after apply)
            + remote_write_yaml = <<-EOT
                  remote_write:
                  - name: ArmsRemoteWrite1
                    url: http://xx.xx.xx.xx:8080/prometheus/xxx/yyy/cn-hangzhou/api/v3/write
                    basic_auth: {username: 666, password: '******'}
                    write_relabel_configs:
                    - source_labels: [instance_id]
                      separator: ;
                      regex: si-6e2ca86444db4e5*****
                      replacement: $1
                      action: keep
              EOT
          }
      
      Plan: 1 to add, 0 to change, 0 to destroy.
      
      Do you want to perform these actions?
        Terraform will perform the actions described above.
        Only 'yes' will be accepted to approve.
      
        Enter a value: yes

      yes が返された場合、現在の Prometheus インスタンスのリモート書き込み設定が作成されます。

結果の確認

Managed Service for Prometheus コンソールにログインし、Prometheus インスタンスの統合センターページでリモート書き込み設定を表示できます。統合センターページでリモート書き込み設定を表示するには、次の手順を実行します。

  1. Managed Service for Prometheus コンソール にログインします。

  2. 左側のナビゲーションペインで、[インスタンス] をクリックします。

  3. 管理する Prometheus インスタンス インスタンスの名前をクリックして、統合センターページに移動します。

  4. カスタム[インストール済み] セクションで リモート書き込み をクリックします。表示されるパネルで、 タブをクリックして、作成されたリモート書き込み設定を表示します。

Prometheus インスタンスからリモート書き込み設定を削除する

手順

次のコマンドを実行して、Terraform を使用して作成された Prometheus インスタンスからリモート書き込み設定を削除できます。

terraform destroy

期待される出力:

...
Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes
...
Destroy complete! Resources: 1 destroyed.

結果の確認

Managed Service for Prometheus コンソールにログインできます。 Prometheus インスタンスの統合センターページで、リモート書き込み設定が削除されているかどうかを確認します。

  1. Managed Service for Prometheus コンソール にログインします。

  2. 左側のナビゲーションペインで、[インスタンス] をクリックします。

  3. 管理する Prometheus インスタンス インスタンスの名前をクリックして、統合センターページに移動します。

  4. [インストール済み] セクションで [カスタム] をクリックします。表示されるパネルで、[リモート書き込み] タブをクリックします。リモート書き込み設定が存在しない場合は、削除されています。