使用 Packer 建立 VM 映像檔

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 權限

建立 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 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 建構工具

  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] 是您要建構的 VM 映像檔名稱。
    • [IMAGE_FAMILY] 是 VM 映像檔的映像檔系列
    • [IMAGE_ZONE]映像檔區域
  4. 使用建構設定檔展開建構作業:

    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 映像檔頁面中查看。

後續步驟