Packer 是一項開放原始碼工具,可從單一來源設定,為多個平台建立相同的虛擬機器 (VM) 映像檔。本頁說明如何使用 Packer 和 Cloud Build 建立 VM 映像檔,以便在 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 權限
如要搭配 Cloud Build 使用 Packer,請將Compute Engine 執行個體管理員 (v1) 角色 (
roles/compute.instanceAdmin.v1) 和服務帳戶使用者角色 (roles/iam.serviceAccountUser) 授予建構服務帳戶。如要在 Artifact Registry 中儲存建構的映像檔,請將 Artifact Registry Writer 角色 (
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 VM 映像檔的 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 會假設設定檔和原始碼位於目前的工作目錄中。
映像檔建構完成後,您可以在 Google Cloud 控制台的 Compute Engine 映像檔頁面中查看。