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 搭配使用,请向构建服务帐号授予 Compute Engine 实例管理员 (v1) 角色 (
roles/compute.instanceAdmin.v1) 和服务账号用户角色 (roles/iam.serviceAccountUser) 。如需将构建的映像存储在 Artifact Registry 中,请向构建服务帐号授予 Artifact Registry 写入者 角色 (
roles/artifactregistry.writer) 。
创建 Packer 构建器映像
Cloud Build 提供
Packer 社区构建器映像
,可用于在 Cloud Build 中调用 packer 命令。
在 Cloud Build 配置文件中使用此构建器之前,您必须构建映像并将其推送到 Artifact Registry:
克隆 cloud-builders-community 代码库:
git clone https://github.com/GoogleCloudPlatform/cloud-builders-community.git导航到 Packer 构建器映像:
cd cloud-builders-community/packer将构建器提交到您的项目:
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 构建器
确保您拥有 Packer 模板(例如
packer.pkr.hcl)以及源代码。在项目根目录中,创建一个构建配置文件 名为
cloudbuild.yaml或cloudbuild.json。在构建配置文件中,添加构建步骤以调用
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.hclJSON
{ "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" ] } ] }其中:
使用构建配置文件启动构建:
gcloud builds submit --region=[REGION] --config [CONFIG_FILE_PATH] [SOURCE_DIRECTORY]其中:
[CONFIG_FILE_PATH]是构建配置文件的路径。[SOURCE_DIRECTORY]是源代码的路径或网址。[REGION]是支持的构建区域之一。
如果您未在
gcloud builds submit命令中指定[CONFIG_FILE_PATH]和[SOURCE_DIRECTORY],则 Cloud Build 会假定配置文件和源代码位于当前工作目录中。
构建映像后,您可以在Compute Engine 映像页面 中查看它们 Google Cloud 。