使用機器映像檔,為在 Compute Engine 中運作的 VM 執行個體儲存多個磁碟中的所有設定、中繼資料、權限和資料。用來建立機器映像檔的 VM 執行個體稱為「來源 VM 執行個體」。
如要瞭解使用機器映像檔的時機和方式,請參閱「機器映像檔」一文。
本文說明如何根據來源 VM 執行個體建立機器映像檔。
事前準備
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 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
-
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.
- 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.
-
限制和規定
- 每 60 分鐘最多只能建立特定執行個體的機器映像檔 6 次。
- 您可以使用 Cloud Key Management Service (Cloud KMS) 或客戶提供的加密金鑰 (CSEK) 來保護機器映像檔。加密金鑰可保護磁碟的內容。不過,中繼資料並未受到保護。
- 如果您使用 CSEK 保護機器映像檔,系統會在產生機器映像檔時儲存所有磁碟資料的完整副本。在所有其他情況下,磁碟資料都會儲存在差異複本中。
您無法使用下列來源 VM 建立機器映像檔:
- 已連接的磁碟超過 200 TB。
- 沒有任何已連結的磁碟。
- 是否有任何已連結的 Hyperdisk 磁碟區。
- 使用 C3D、H3、A4、A3 或 Z3 機型
機器映像檔不支援的執行個體和磁碟屬性
從 VM 執行個體建立機器映像檔,或從機器映像檔建立 VM 執行個體時,系統不會保留或還原下列執行個體和磁碟屬性:
disks.architecture
disks.description
disks.labels
disks.locked
disks.multiWriter
disks.onUpdateAction
disks.provisionedIops
disks.shieldedInstanceInitialState
disks.sourceImage
disks.sourceImageEncryptionKey
disks.sourceInstantSnapshot
disks.sourceSnapshot
disks.sourceSnapshotEncryptionKey
privateIpv6GoogleAccess
resourceManagerTags
resourcePolicies
secure_tag
shieldedInstanceConfig
透過 VM 建立機器映像檔
您可以使用 Google Cloud 控制台、Google Cloud CLI 或 REST 建立機器映像檔。
如要建立機器映像檔,您需要下列資訊:
- 您要建立的機器映像檔名稱。
- 來源 VM 執行個體的名稱。
- 來源 VM 執行個體所在的區域。
- 選填說明。
- 選用的
storageLocation
。如果您未指定位置,預設儲存位置會是來源 VM 執行個體的多地區 Cloud Storage 位置。 - 選用的加密金鑰。您可以選擇 Google-owned and Google-managed encryption key、Cloud Key Management Service (Cloud KMS) 金鑰或客戶提供的加密金鑰 (CSEK)。如果未指定加密金鑰,機器映像檔會使用 Google-owned and Google-managed encryption key加密。
如果您想使用機器映像檔來複製執行個體和建立快照,請先移除執行個體特有的 OS 和應用程式資訊,再從執行個體產生機器映像檔。舉例來說,如果是 Windows VM 執行個體,請使用
GCESysprep
為系統準備複製作業。
主控台
gcloud
使用 gcloud compute machine-images create
指令,從執行個體建立機器映像檔。
gcloud compute machine-images create MACHINE_IMAGE_NAME \ --source-instance=SOURCE_VM_NAME
更改下列內容:
MACHINE_IMAGE_NAME
:您要建立的機器映像檔名稱。SOURCE_VM_NAME
:您要用來建立映像檔的來源 VM 執行個體名稱。
示例
舉例來說,您可以使用下列 gcloud
指令,從名為 my-instance
的來源執行個體建立名為 my-machine-image
的機器映像檔:
gcloud compute machine-images create my-machine-image \ --source-instance=my-instance
這項程序需要幾分鐘的時間。建立機器映像檔後,您會看到類似以下的輸出內容:
Created [https://www.googleapis.com/compute/v1/projects/project-12345/global/machineImages/my-machine-image]. NAME STATUS my-machine-image READY
REST
在 API 中,建構對 machineimages.insert
方法的 POST
要求。在要求主體中加入下列 POST
要求:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/machineImages { "name": "MACHINE_IMAGE_NAME", "sourceInstance": "SOURCE_INSTANCE_URL" }
更改下列內容:
PROJECT_ID
:專案 ID。MACHINE_IMAGE_NAME
:您要建立的機器映像檔名稱。SOURCE_INSTANCE_URL
:您要用來建立機器映像檔的來源 VM 執行個體的完整或部分網址。舉例來說,如果您在名為myProject
的專案中,有一個名為my-instance
的來源例項,以下網址有效:https://www.googleapis.com/compute/v1/projects/myProject/global/instances/my-instance
projects/myProject/global/instances/my-instance
global/instances/my-instance
後續步驟
- 進一步瞭解機器映像檔
- 使用機器映像檔建立 VM 執行個體