Packer を使用した VM イメージのビルド

Packer は、単一のソース構成から複数のプラットフォーム向けに同じ仮想マシン(VM)イメージを作成するオープンソース ツールです。このページでは、Packer と Cloud Build によって Compute Engine で使用する VM イメージを作成する方法について説明します。

準備

このページの説明は、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 に push する必要があります。

  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.yaml または cloudbuild.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]
    

    ここで

    gcloud builds submit コマンドに [CONFIG_FILE_PATH][SOURCE_DIRECTORY] を指定しないと、Cloud Build は、構成ファイルとソースコードが現在の作業ディレクトリにあることを前提とします。

ビルドされたイメージは、コンソールの Compute Engine イメージページ で確認できます。 Google Cloud

次のステップ