使用 Terraform 和 Cloud Scheduler 创建和运行批量作业


本教程介绍如何使用 Terraform 通过 Cloud Scheduler Cron 作业创建和运行批量作业。

Terraform 是一种开源工具,可让您通过在配置文件中指定所需的状态来预配和管理基础架构。这些文件可以被视为代码并存储在版本控制系统(如 GitHub)中。

虽然 Terraform 没有可用于 Batch 的资源,但本教程介绍了如何使用 Terraform 创建 Batch 作业。具体而言,您可以使用 Terraform 安排并运行 Cloud Scheduler Cron 作业,以定位 Batch API 来创建和运行 Batch 作业。Cloud Scheduler 是一项 Google Cloud 服务,可让您自动安排 Cron 作业并支持 Terraform。

本教程适用于已经在使用 Terraform 管理基础架构并希望将 Batch 作业整合到 Terraform 中的 Batch 用户。

目标

  • 创建 Terraform 目录和配置文件,用于定义创建批量作业的 Cloud Scheduler Cron 作业。
  • 部署 Terraform 配置以运行 Cron 作业。
  • 验证 Cron 作业是否会创建批量作业。
  • 更新 Terraform 配置,以暂停 Cron 作业,使其停止创建批量作业。

费用

在本文档中,您将使用 Google Cloud 的以下收费组件:

您可使用价格计算器根据您的预计使用情况来估算费用。 Google Cloud 新用户可能有资格申请免费试用

完成本文档中描述的任务后,您可以通过删除所创建的资源来避免继续计费。如需了解详情,请参阅清理

准备工作

  1. 准备开发环境(Cloud Shell 或本地 shell):

    Cloud Shell

    如需在已设置 gcloud CLI 和 Terraform 的环境中使用在线终端,请激活 Cloud Shell。

    在此页面底部,Cloud Shell 会话将启动并显示命令行提示符。会话初始化可能需要几秒钟时间。

    本地 shell

    如需使用本地开发环境,请按照以下步骤操作:

    1. 安装 Google Cloud CLI。
    2. 如需初始化 gcloud CLI,请运行以下命令:

      gcloud init
    3. 安装 Terraform
  2. 创建或选择 Google Cloud 项目。

    • 创建 Google Cloud 项目:

      gcloud projects create PROJECT_ID
    • 选择您创建的 Google Cloud 项目:

      gcloud config set project PROJECT_ID
  3. 确保您的 Google Cloud 项目已启用结算功能

  4. Enable the Batch, Compute Engine, Cloud Logging, Cloud Scheduler, and Resource Manager APIs:

    gcloud services enable batch.googleapis.com compute.googleapis.com logging.googleapis.com  cloudscheduler.googleapis.com cloudresourcemanager.googleapis.com
  5. 确保您的项目至少有一个服务帐号具有本教程所需的权限。

    具体而言,您可以使用同一服务帐号或两个单独的服务帐号授予以下权限:

    • 允许 Cron 作业创建批量作业并为该批量作业关联服务帐号。
    • 允许批量作业创建和访问运行所需的资源。

    为确保本教程的服务帐号具有使用 Terraform 通过 Cloud Scheduler Cron 作业创建批量作业的必要权限,请让管理员授予本教程以下 IAM 角色的服务帐号:

    • Cloud Scheduler Cron 作业的服务帐号:
    • 批量作业的服务帐号:

    如需详细了解如何授予角色,请参阅管理访问权限

    您的管理员还可以通过自定义角色或其他预定义角色为本教程授予服务帐号所需的权限。

  6. 确保您拥有本教程所需的权限。

    具体而言,您需要有相应权限才能执行以下操作:

    • 创建一个 Cron 作业,并为 Cron 作业附加服务帐号。
    • 查看和删除 Cron 作业和 Batch 作业。

    要获得使用 Terraform 通过 Cloud Scheduler Cron 作业创建批量作业所需的权限,请让管理员授予您以下 IAM 角色:

创建 Terraform 目录和配置文件

为 Terraform 创建一个目录,并创建一个配置文件,用于定义您要使用 Terraform 创建或更新的资源。本教程的示例配置文件定义了一个名为 batch-job-invoker 的 Cloud Scheduler Cron 作业。启用后,batch-job-invoker cron 作业每 5 分钟运行一次,以创建所定义的 Batch 作业的新实例。

  1. 如需创建目录和该目录中的新 Terraform 配置 (.tf) 文件,请输入以下命令,然后按 Enter

    mkdir terraform && cd terraform && cat > main.tf
    

    此命令会创建 terraform 目录,导航到该目录,然后在下一行开始定义新的 main.tf 配置文件。

  2. 复制并粘贴以下 Terraform 配置:

    # define variables
    variable "project_id" {
      type        = string
      description = "The project name to use."
      default = "PROJECT_ID"
    }
    
    variable "project_number" {
      type        = string
      description = "The project number to use."
      default = "PROJECT_NUMBER"
    }
    
    variable "region" {
      type        = string
      description = "The region where resources are created."
      default = "us-central1"
    }
    
    variable "cloud_scheduler_service_account_email" {
      type        = string
      description = "The service account email."
      default = "CLOUD_SCHEDULER_SERVICE_ACCOUNT_EMAIL"
    }
    
    variable "batch_service_account_email" {
      type        = string
      description = "The service account email."
      default = "BATCH_SERVICE_ACCOUNT_EMAIL"
    }
    
    # define a Cloud Scheduler cron job which triggers Batch jobs
    resource "google_cloud_scheduler_job" "batch-job-invoker" {
      paused           = false # this cron job is enabled
      name             = "batch-job-invoker"
      project          = var.project_id
      region           = var.region
      schedule         = "*/5 * * * *" # when enabled, run every 5 minutes
      time_zone        = "America/Los_Angeles"
      attempt_deadline = "180s"
    
      retry_config {
        max_doublings        = 5
        max_retry_duration   = "0s"
        max_backoff_duration = "3600s"
        min_backoff_duration = "5s"
      }
    
      # when this cron job runs, create and run a Batch job
      http_target {
        http_method = "POST"
        uri = "https://batch.googleapis.com/v1/projects/${var.project_number}/locations/${var.region}/jobs"
        headers = {
          "Content-Type" = "application/json"
          "User-Agent"   = "Google-Cloud-Scheduler"
        }
        # Batch job definition
        body = base64encode(<<EOT
        {
          "taskGroups":[
            {
              "taskSpec": {
                "runnables":{
                  "script": {
                    "text": "echo Hello world! This job was created using Terraform and Cloud Scheduler."
                  }
                }
              }
            }
          ],
          "allocationPolicy": {
            "serviceAccount": {
              "email": "${var.batch_service_account_email}"
            }
          },
          "labels": {
            "source": "terraform_and_cloud_scheduler_tutorial"
          },
          "logsPolicy": {
            "destination": "CLOUD_LOGGING"
          }
        }
        EOT
        )
        oauth_token {
          scope                 = "https://www.googleapis.com/auth/cloud-platform"
          service_account_email = var.cloud_scheduler_service_account_email
        }
      }
    }
    
    

    请替换以下内容:

    • PROJECT_ID:项目的项目 ID
    • PROJECT_NUMBER:项目的项目编号
    • CLOUD_SCHEDULER_SERVICE_ACCOUNT_EMAIL:您为 Cloud Scheduler Cron 作业准备的服务帐号的电子邮件地址。

      例如,要使用 Compute Engine 默认服务帐号,请指定以下内容:

      PROJECT_NUMBER-compute@developer.gserviceaccount.com
      
    • BATCH_SERVICE_ACCOUNT_EMAIL:您为批量作业准备的服务帐号的电子邮件地址。

      例如,要使用 Compute Engine 默认服务帐号,请指定以下内容:

      PROJECT_NUMBER-compute@developer.gserviceaccount.com
      

    此 Terraform 配置定义了一些输入变量,以及可与创建批量作业的 API 方法通信的 Cron 作业。

  3. 如需保存和关闭文件,请按 Ctrl+D(在 macOS 上,按 Command+D)。

部署 Terraform 配置以创建 Cron 作业

通过初始化 Terraform、生成计划更改并应用更改来部署 Terraform 配置。部署 Terraform 配置后,您可以描述项目中的资源,以验证 Terraform 是否成功创建了 batch-job-invoker Cron 作业。

  1. 在目录中初始化 Terraform:

    terraform init
    

    输出类似于以下内容:

    ...
    Terraform has been successfully initialized!
    
    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.
    
  2. 根据项目的当前状态和配置文件生成 Terraform 执行计划:

    terraform plan
    

    输出内容类似于以下内容,显示计划创建 batch-job-invoker Cron 作业:

    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:
    
      # google_cloud_scheduler_job.batch-job-invoker will be created
      + resource "google_cloud_scheduler_job" "batch-job-invoker" {
          + id        = (known after apply)
          + name      = "batch-job-invoker"
          + paused    = false
          + project   = "PROJECT_ID"
          + region    = "us-central1"
          + schedule  = "*/5 * * * *"
          + state     = (known after apply)
          + time_zone = "America/Los_Angeles"
    
          + http_target {
              + body        = "..."
              + headers     = {
                  + "Content-Type" = "application/json"
                  + "User-Agent"   = "Google-Cloud-Scheduler"
                }
              + http_method = "POST"
              + uri         = "https://batch.googleapis.com/v1/projects/PROJECT_NUMBER/locations/us-central1/jobs"
    
              + oauth_token {
                  + scope                 = "https://www.googleapis.com/auth/cloud-platform"
                  + service_account_email = "CLOUD_SCHEDULER_SERVICE_ACCOUNT_EMAIL"
                }
            }
    
          + retry_config {
              + max_backoff_duration = "3600s"
              + max_doublings        = 5
              + max_retry_duration   = "0s"
              + min_backoff_duration = "5s"
              + retry_count          = (known after apply)
            }
        }
    
    Plan: 1 to add, 0 to change, 0 to destroy.
    
    ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
    
    Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
    
  3. 如需应用计划以创建 batch-job-invoker Cron 作业,请按以下步骤操作:

    1. 输入以下命令:

      terraform apply
      

      输出内容类似于上一个 terraform plan 命令,只不过它以确认提示结尾。

    2. 若要确认并应用该方案,请输入 yes

      输出类似于以下内容:

      google_cloud_scheduler_job.batch-job-invoker: Creating...
      google_cloud_scheduler_job.batch-job-invoker: Creation complete after 0s [id=projects/PROJECT_ID/locations/us-central1/jobs/batch-job-invoker]
      
      Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
      
  4. 如需验证 batch-job-invoker cron 作业是否存在以及是否已启用,请对其进行描述:

    gcloud scheduler jobs describe batch-job-invoker --location us-central1
    

    输出类似于以下内容:

    attemptDeadline: 180s
    httpTarget:
      body: ...
      headers:
        Content-Type: application/json
        User-Agent: Google-Cloud-Scheduler
      httpMethod: POST
      oauthToken:
        scope: https://www.googleapis.com/auth/cloud-platform
        serviceAccountEmail: CLOUD_SCHEDULER_SERVICE_ACCOUNT_EMAIL
      uri: https://batch.googleapis.com/v1/projects/PROJECT_NUMBER/locations/us-central1/jobs
    lastAttemptTime: '...'
    name: projects/PROJECT_ID/locations/us-central1/jobs/batch-job-invoker
    retryConfig:
      maxBackoffDuration: 3600s
      maxDoublings: 5
      maxRetryDuration: 0s
      minBackoffDuration: 5s
    schedule: '*/5 * * * *'
    scheduleTime: '...'
    state: ENABLED
    status: {}
    timeZone: America/Los_Angeles
    userUpdateTime: '...'
    

    在输出中,验证 state 字段是否设置为 ENABLED

验证 Cron 作业是否会创建批量作业

验证 batch-job-invoker Cron 作业是否正确创建批量作业。

  1. 请等待 5 分钟,以便 Cron 作业自动运行,或触发 Cron 作业立即运行:

    gcloud scheduler jobs run batch-job-invoker --location us-central1
    
  2. 列出 batch-job-invoker Cron 作业创建的批量作业:

    gcloud batch jobs list \
    --filter labels.source=\"terraform_and_cloud_scheduler_tutorial\" \
    --sort-by ~createTime
    
    • --filter labels.source=\"terraform_and_cloud_scheduler_tutorial\" 标志会过滤列表,以仅包含具有标签为 source 且值为 terraform_and_cloud_scheduler_tutorial 的批处理作业。
    • --sort-by ~createTime 标志用于按从新到旧的顺序对列表进行排序。

更新 Terraform 配置,以暂停 Cron 作业

获得所需的批量作业数量后,请更新并部署 Terraform 配置以暂停 batch-job-invoker Cron 作业。如果您想要更新 Cron 作业或未来的批量作业的其他属性,需要遵循同样的流程。

  1. 更新 Terraform 配置文件,以通过将 paused 字段设置为 true 来暂停 Cron 作业:

    sed -i 's/paused           = false # this cron job is enabled/paused           = true # this cron job is paused/g' main.tf
    
  2. 根据项目的当前状态和配置文件生成 Terraform 执行计划:

    terraform plan
    

    输出内容类似于以下内容,显示计划将 paused 字段的值从 false 更新为 true

    google_cloud_scheduler_job.batch-job-invoker: Refreshing state... [id=projects/PROJECT_ID/locations/us-central1/jobs/batch-job-invoker]
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      ~ update in-place
    
    Terraform will perform the following actions:
    
      # google_cloud_scheduler_job.batch-job-invoker will be updated in-place
      ~ resource "google_cloud_scheduler_job" "batch-job-invoker" {
            id               = "projects/PROJECT_ID/locations/us-central1/jobs/batch-job-invoker"
            name             = "batch-job-invoker"
          ~ paused           = false -> true
            # (6 unchanged attributes hidden)
    
          ~ http_target {
              ~ headers     = {
                  + "User-Agent"   = "Google-Cloud-Scheduler"
                    # (1 unchanged element hidden)
                }
                # (3 unchanged attributes hidden)
    
                # (1 unchanged block hidden)
            }
    
            # (1 unchanged block hidden)
        }
    
    Plan: 0 to add, 1 to change, 0 to destroy.
    
    ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
    
    Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
    
  3. 如需应用计划以更新 batch-job-invoker Cron 作业,请按以下步骤操作:

    1. 输入以下命令:

      terraform apply
      

      输出内容类似于上一个 terraform plan 命令,只不过它以确认提示结尾。

    2. 若要确认并应用该方案,请输入 yes

      输出类似于以下内容:

      google_cloud_scheduler_job.batch-job-invoker: Modifying... [id=projects/PROJECT_ID/locations/us-central1/jobs/batch-job-invoker]
      google_cloud_scheduler_job.batch-job-invoker: Modifications complete after 1s [id=projects/PROJECT_ID/locations/us-central1/jobs/batch-job-invoker]
      
      Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
      
  4. 如需验证 batch-job-invoker Cron 作业是否已暂停,请进行描述:

    gcloud scheduler jobs describe batch-job-invoker --location us-central1
    

    输出类似于以下内容:

    attemptDeadline: 180s
    httpTarget:
      body: ...
      headers:
        Content-Type: application/json
        User-Agent: Google-Cloud-Scheduler
      httpMethod: POST
      oauthToken:
        scope: https://www.googleapis.com/auth/cloud-platform
        serviceAccountEmail: CLOUD_SCHEDULER_SERVICE_ACCOUNT_EMAIL
      uri: https://batch.googleapis.com/v1/projects/PROJECT_NUMBER/locations/us-central1/jobs
    lastAttemptTime: '...'
    name: projects/PROJECT_ID/locations/us-central1/jobs/batch-job-invoker
    retryConfig:
      maxBackoffDuration: 3600s
      maxDoublings: 5
      maxRetryDuration: 0s
      minBackoffDuration: 5s
    schedule: '*/5 * * * *'
    scheduleTime: '...'
    state: PAUSED
    status: {}
    timeZone: America/Los_Angeles
    userUpdateTime: '...'
    

    在输出中,验证 state 字段是否设置为 PAUSED

清理

为避免因本教程中使用的资源导致您的 Google Cloud 帐号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。

删除项目

  1. 删除 Google Cloud 项目:

    gcloud projects delete PROJECT_ID

  2. 转到父目录,然后删除 Terraform 目录及其所有文件。

    cd .. && rm -r terraform
    

删除各个资源

  1. 删除 batch-job-invoker Cron 作业。

    terraform destroy
    
  2. 如需删除本教程中的所有批量作业,请按以下步骤操作:

    1. 列出由 batch-job-invoker Cron 作业创建的所有批处理作业:

      gcloud batch jobs list \
      --filter labels.source=\"terraform_and_cloud_scheduler_tutorial\" \
      --sort-by ~createTime
      

      记录您要删除的每个作业的名称。

    2. 从本教程中删除批量作业:

      gcloud batch jobs delete JOB_NAME --location us-central1
      

      JOB_NAME 替换为批量作业的名称。

      对所有批处理作业重复此步骤。

  3. 如果您为本教程创建了一个服务帐号,请删除该服务帐号:

    gcloud iam service-accounts delete SERVICE_ACCOUNT_EMAIL
    

    SERVICE_ACCOUNT_EMAIL 替换为您为本教程创建的服务帐号的电子邮件地址。也就是说,您使用了以下服务帐号:

    • CLOUD_SCHEDULER_SERVICE_ACCOUNT_EMAIL:Cloud Scheduler 的服务帐号。
    • BATCH_SERVICE_ACCOUNT_EMAIL:Batch 的服务帐号。

    如果您创建了两个单独的服务帐号,请重复此步骤。

  4. 转到父目录,然后删除 Terraform 目录及其所有文件。

    cd .. && rm -r terraform
    

后续步骤