使用Terraform管理环境实例的自定义Job

您可以使用Terraform创建Prometheus监控作业,并配置监控对象、抓取路径和抓取间隔等参数,将监控作业部署到容器服务环境实例或ECS环境实例中,实现Prometheus自动监控,使监控更加高效且易于管理。

前提条件

已创建容器服务类型的环境。具体操作,请参见使用Terraform管理环境实例

使用限制

仅支持容器服务和ECS类型的环境。

添加环境实例的自定义Job

  1. 创建一个工作目录,并在工作目录中创建名为main.tf的配置文件,用于配置自定义Job资源。

    provider "alicloud" {
    # access_key = "************"
    # secret_key = "************"
    # region = "cn-beijing"
    }
    # 环境实例的自定义Job配置。
    resource "alicloud_arms_env_custom_job" "my-custom-job1" {
    	env_custom_job_name = "自定义Job名称,如my-custom-job1"
      environment_id = "容器服务或ECS环境的ID,如env-xxxxx"
      config_yaml    = <<-EOT
          scrape_configs:
          - job_name: job-demo1
            honor_timestamps: false
            honor_labels: false
            scrape_interval: 30s
            scheme: http
            metrics_path: /metric
            static_configs:
            - targets:
              - 127.0.0.1:9090
          - job_name: job-demo2
            honor_timestamps: false
            honor_labels: false
            scrape_interval: 30s
            scheme: http
            metrics_path: /metric
            static_configs:
            - targets:
              - 127.0.0.1:9090
            http_sd_configs:
            - url: 127.0.0.1:9090
              refresh_interval: 30s
    	EOT
    }
  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. 执行以下命令,生成资源规划。

    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_env_custom_job.myCustomJob1 will be created
     + resource "alicloud_arms_env_custom_job" "myCustomJob1" {
    				env_custom_job_name = "myCustomJob1"
    				environment_id = "xxx"
    				config_yaml = ....
     }
    
    Plan: 1 to add, 0 to change, 0 to destroy.
  4. 执行以下命令,创建自定义Job。

    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_env_custom_job.myCustomJob1 will be created
     + resource "alicloud_arms_env_custom_job" "myCustomJob1" {
    				env_custom_job_name = "myCustomJob1"
    				environment_id = "xxx"
    				config_yaml = ....
     }
    
    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

    提示Enter a value时,输入yes,当前环境实例的自定义Job创建成功。

  5. 登录ARMS控制台

  6. 在左侧导航栏,单击接入管理。在接入管理页面的环境列表中,单击目标环境名称进入详情页面。

  7. 在环境详情页面,单击指标采集页签,然后单击自定义采集,查看是否已成功创建自定义Job。

删除容器服务环境实例的自定义Job

  1. 执行以下命令,删除通过Terraform创建的集群。

    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.

    提示Enter a value时,输入yes

  2. 登录ARMS控制台

  3. 在左侧导航栏,单击接入管理。在接入管理页面的环境列表中,单击目标环境名称进入详情页面。

  4. 在环境详情页面,单击指标采集页签,然后单击自定义采集,查看是否已成功删除自定义Job。