在映像檔系列中設定映像檔版本


以映像檔系列簡化映像檔版本的設定工作。新增映像檔到映像檔系列中,將它設定為系列中最新的映像檔版本。如果您決定必須將映像檔系列復原為前一個映像檔版本,請淘汰系列中最新的映像檔。

如要瞭解使用映像檔系列時的最佳做法,請參閱「映像檔系列最佳做法」。

您可以視需要選擇使用 Google Cloud 主控台、具有 --storage-location 旗標的 gcloud compute images create 指令,或 images().insert 方法來指定映像檔的儲存位置

事前準備

  • 參閱「圖片」說明文件。
  • 如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要在本機開發環境中執行程式碼或範例,您可以選取下列任一選項,向 Compute Engine 進行驗證:

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    gcloud

    1. After installing the Google Cloud CLI, initialize it by running the following command:

      gcloud init

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    2. Set a default region and zone.

    REST

    To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.

      After installing the Google Cloud CLI, initialize it by running the following command:

      gcloud init

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    For more information, see Authenticate for using REST in the Google Cloud authentication documentation.

在映像檔系列中設定映像檔版本

主控台

  1. 在 Google Cloud 控制台中,前往「Create an image」(建立映像檔) 頁面。

    前往「Create an image」(建立映像檔)

  2. 指定映像檔的「Name」(名稱)。例如:image-v1

  3. 指定您要建立映像檔的「Source」(來源)。這可以是 Cloud Storage 中的永久磁碟、快照、其他映像檔或 disk.raw 檔案。

  4. 如要從連結到執行中 VM 的磁碟建立映像檔,請勾選「Keep instance running」,確認您要在 VM 執行時建立映像檔。您可以在建立映像檔前準備 VM

  5. 在「Based on source disk location (default)」(根據來源磁碟位置 (預設值)) 下拉式選單中,指定儲存圖片的位置。例如,指定 us 會將圖片儲存在 us 多地區;指定 us-central1 則會儲存在 us-central1 地區。如果未選擇位置,Compute Engine 會將映像檔儲存在最靠近映像檔來源位置的多地區。

  6. 為新圖片指定系列。例如,新增 my-image-family 來將圖片整理為映像檔系列的一部分。

  7. 選用:指定其他圖片屬性:

    • 說明:自訂圖片的說明。
    • 標籤標籤可用來將資源分組。
  8. 指定加密金鑰。您可以選擇 Google-owned and Google-managed encryption key、Cloud Key Management Service (Cloud KMS) 金鑰或客戶提供的加密金鑰 (CSEK)。如果未指定加密金鑰,系統會使用 Google-owned and Google-managed encryption key為圖片加密。

  9. 按一下 [建立] 以建立映像檔。

gcloud

gcloud compute images create image-v1 \
    --source-disk disk-1 \
    --source-disk-zone us-central1-f \
    --family my-image-family

映像檔系列指向 image-v1。新增第二個映像檔到系列:

gcloud compute images create image-v2 \
    --source-disk disk-2 \
    --source-disk-zone us-central1-f \
    --family my-image-family

REST

images().insert 方法提出 POST 要求。在要求主體中指定映像檔系列。

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/images

{
  "name": "image-v2",
  "sourceDisk": "/zones/us-central1-f/disks/disk-2",
  "family":"my-image-family",

}

映像檔系列指向 image-v2,因為它是您新增至映像檔系列的最新映像檔。

gcloud

您可以執行 gcloud compute images describe-from-family 指令,查看系列指向哪個映像檔。

例如:

gcloud compute images describe-from-family my-image-family

family: my-image-family
id: '8904691942610171306'
kind: compute#image
name: image-v2
selfLink: https://compute.googleapis.com/compute/v1/projects/my-project/global/images/image-v2
sourceDisk: https://compute.googleapis.com/compute/v1/projects/my-project/zones/us-central1-f/disks/disk-v2
sourceDiskId: '1677449456001963379'
sourceType: RAW
status: READY

REST

images().getFromFamily 方法提出 GET 要求。在要求主體中指定映像檔系列。

GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/images/family

{
  "resourceId":"my-image-family",

}

如果您決定復原映像檔系列,使它不再指向 image-v2,請淘汰 image-v2,系列就會再次指向 image-v1

gcloud compute images deprecate image-v2 \
    --state DEPRECATED \
    --replacement image-v1

檢查並確認映像檔系列指向了 image-v1

gcloud compute images describe-from-family my-image-family

family: my-image-family
id: '2741732787056801255'
kind: compute#image
name: image-v1
selfLink: https://compute.googleapis.com/compute/v1/projects/my-project/global/images/image-v1
sourceDisk: https://compute.googleapis.com/compute/v1/projects/my-project/zones/us-central1-f/disks/disk-v1
sourceDiskId: '1677449456001963379'
sourceType: RAW
status: READY