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

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

如需使用映像檔系列的最佳做法,請參閱映像檔系列最佳做法

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

事前準備

  • 參閱映像檔文件。
  • 如果尚未設定驗證,請先完成設定。 「驗證」是指驗證身分的程序,確認您有權存取 Trusted Cloud by S3NS 服務和 API。如要從本機開發環境執行程式碼或範例,請選取下列任一選項,向 Compute Engine 進行驗證:

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

    Console

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

    gcloud

    1. 安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI。 登入後,執行下列指令初始化 Google Cloud CLI:

      gcloud init
    2. Set a default region and zone.

    REST

    如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。

      安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI。 登入後,執行下列指令初始化 Google Cloud CLI:

      gcloud init

    詳情請參閱 Trusted Cloud 驗證說明文件中的「Authenticate for using REST」。

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

主控台

  1. 前往 Trusted Cloud 控制台的「Create an image」(建立映像檔) 頁面。

    前往「建立圖像」

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

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

  4. 如要從連結到執行中 VM 的磁碟建立映像檔,請勾選「讓執行個體繼續執行」,確認要在 VM 執行時建立映像檔。您可以先準備 VM,再建立映像檔。

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

  6. 為新映像檔指定映像檔系列。例如,新增 my-image-family,將映像檔整理為映像檔系列的成員。

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

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