使用 Packer 构建虚拟机映像

Packer 是一种开源工具,可以通过单一源配置为多个平台创建相同的虚拟机 (VM) 映像。本页面介绍了如何使用 Packer 和 Cloud Build 创建用于 Compute Engine 的虚拟机映像。

准备工作

本页面的说明假定您熟悉 Packer。此外:

  • 准备好包含 Packer 模板 的源代码。
  • 在 Artifact Registry 中拥有 Docker 代码库,或创建一个新代码库
  • 如果您要使用本页面中的 gcloud 命令,请安装 Google Cloud CLI
  • 启用以下 API:

    gcloud services enable compute.googleapis.com
    gcloud services enable servicemanagement.googleapis.com
    gcloud services enable storage-api.googleapis.com
    gcloud services enable artifactregistry.googleapis.com
    

必需的 IAM 权限

创建 Packer 构建器映像

Cloud Build 提供 Packer 社区构建器映像 ,可用于在 Cloud Build 中调用 packer 命令。 在 Cloud Build 配置文件中使用此构建器之前,您必须构建映像并将其推送到 Artifact Registry:

  1. 克隆 cloud-builders-community 代码库:

    git clone https://github.com/GoogleCloudPlatform/cloud-builders-community.git
    
  2. 导航到 Packer 构建器映像:

    cd cloud-builders-community/packer
    
  3. 将构建器提交到您的项目:

    gcloud builds submit . --config cloudbuild.yaml --substitutions=_AR_HOST=[LOCATION]-docker.pkg.dev,_AR_REPO=[REPOSITORY]
    

    其中:

    • [LOCATION] 是您的 Artifact Registry 代码库的 区域
    • [REPOSITORY] 是您的 Artifact Registry 代码库的名称。

创建 Packer 模板

创建一个名为 packer.pkr.hcl 的新文件,并添加模板配置。

以下示例展示了配置为构建 Compute Engine 虚拟机映像的 Packer 模板 (packer.pkr.hcl):

packer {
  required_plugins {
    googlecompute = {
      version = ">= 1.1.1"
      source  = "github.com/hashicorp/googlecompute"
    }
  }
}

variable "image_name" {
  type    = string
  description = "The name of the output VM image"
  default = "my-packer-image"
}
variable "project_id" {
  type    = string
  description = "The GCP project ID"
}
variable "image_family" {
  type    = string
  description = "The family of the output VM image"
  default = "my-image-family"
}
variable "image_zone" {
  type    = string
  description = "The zone to build the image in"
  default = "us-central1-a"
}

source "googlecompute" "gce" {
  project_id          = var.project_id
  source_image_family = "ubuntu-2004-lts"
  source_image_project_ids = ["ubuntu-os-cloud"]
  zone                = var.image_zone
  ssh_username        = "packer"
  machine_type        = "e2-small"
  image_name          = "${var.image_name}-"
  image_family        = var.image_family
}

build {
  name    = "gce-vm-image"
  sources = ["sources.googlecompute.gce"]

  provisioner "shell" {
    inline = [
      "echo 'Provisioning image...'",
      "sudo apt-get update",
      "sudo apt-get install -y nginx"
    ]
  }
}

使用 Packer 构建器

  1. 确保您拥有 Packer 模板(例如 packer.pkr.hcl)以及源代码。

  2. 在项目根目录中,创建一个构建配置文件 名为 cloudbuild.yamlcloudbuild.json

  3. 在构建配置文件中,添加构建步骤以调用 packer build 命令:

    YAML

    steps:
    - name: '[LOCATION]-docker.pkg.dev/[PROJECT_ID]/[REPOSITORY]/packer'
      args:
      - build
      - -var
      - image_name=[IMAGE_NAME]
      - -var
      - project_id=[PROJECT_ID]
      - -var
      - image_family=[IMAGE_FAMILY]
      - -var
      - image_zone=[IMAGE_ZONE]
      - packer.pkr.hcl
    

    JSON

    {
      "steps": [
       {
          "name": "[LOCATION]-docker.pkg.dev/[PROJECT_ID]/[REPOSITORY]/packer",
          "args": [
            "build",
            "-var",
            "image_name=[IMAGE_NAME]",
            "-var",
            "project_id=[PROJECT_ID]",
            "-var",
            "image_family=[IMAGE_FAMILY]",
            "-var",
            "image_zone=[IMAGE_ZONE]",
            "packer.pkr.hcl"
           ]
        }
       ]
    }
    

    其中:

    • [LOCATION] 是您的 Artifact Registry 代码库的 区域
    • [PROJECT_ID] 是 Google Cloud 项目 ID。
    • [REPOSITORY] 是您的 Artifact Registry 代码库的名称。
    • [IMAGE_NAME] 是您要构建的虚拟机映像的名称。
    • [IMAGE_FAMILY] 是虚拟机映像的映像系列
    • [IMAGE_ZONE]映像地区
  4. 使用构建配置文件启动构建:

    gcloud builds submit --region=[REGION] --config [CONFIG_FILE_PATH] [SOURCE_DIRECTORY]
    

    其中:

    如果您未在 gcloud builds submit 命令中指定 [CONFIG_FILE_PATH][SOURCE_DIRECTORY],则 Cloud Build 会假定配置文件和源代码位于当前工作目录中。

构建映像后,您可以在Compute Engine 映像页面 中查看它们 Google Cloud 。

后续步骤