本文件說明在 Compute Engine 上使用映像檔系列時的最佳做法。
事前準備
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要在本機開發環境中執行程式碼或範例,您可以選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
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.
-
公開映像檔系列
Compute Engine 提供映像檔系列,確保您的自動化處理系統可以參照最新的映像檔。管理員可以將多個映像檔歸類為一個映像檔系列。如此一來,映像檔的使用者僅需追蹤映像檔系列名稱,而不用記住確切的映像檔名稱。因為映像檔名稱不能重複,映像檔建構管道通常會使用一些資訊來建立映像檔名稱,例如應用程式名稱、日期和版本等,譬如說 my-application-v3-20210101
。在自動化工具中,您可以參照映像檔系列名稱,而不需要定期更新映像檔名稱。使用映像檔系列可確保您隨時都能存取系列中的最新映像檔,例如 my-application
。
公開映像檔會分組為映像檔系列。公開映像檔系列始終會指向各個區域可用的最新版本映像檔。當新映像檔在全球發布時,其在映像檔系列中的初始可用性會依區域而異,這可在 Google 映像檔更新期間,提升工作流程的區域容錯能力。
在映像檔推出期間,不同區域的映像檔系列中,最新的映像檔版本可能會有所不同。舉例來說,debian-cloud
專案中的 debian-12
映像檔系列永遠都會指向最新的 Debian 12 映像檔,但區域 us-central1-a
和 southamerica-east1-b
中的最新 Debian 12 映像檔可能不同。
使用 Google Cloud CLI 從映像檔系列建立 VM 時,Compute Engine 會根據您的要求,使用 VM 所在區域可用的最新映像檔。使用 Google Cloud 控制台建立 VM 時,Compute Engine 只會顯示您所選區域中可用的公開映像檔。如要使用最新的全域可用映像檔建立 VM,請使用 gcloud CLI instances create
指令並指定 --image-family-scope=global
。
查看最新可用的映像檔版本
您可以查看映像檔系列中全球適用的最新映像檔,或是查看特定區域適用的最新映像檔。
全球
如要查看映像檔系列中全球可用的最新映像檔,請使用下列其中一種方法:
gcloud
使用 gcloud compute images describe-from-family
指令:
gcloud compute images describe-from-family IMAGE_FAMILY_NAME \ --project=IMAGE_PROJECT
更改下列內容:
REST
對 images.getFromFamily
方法發出 GET
要求:
GET https://compute.googleapis.com/compute/v1/projects/IMAGE_PROJECT/global/images/family/IMAGE_FAMILY_NAME
更改下列內容:
每個可用區
如要查看特定區域的映像檔系列中最新可用的映像檔,請使用下列其中一種方法:
gcloud
使用加上 --zone
旗標的 gcloud compute images describe-from-family
指令:
gcloud compute images describe-from-family IMAGE_FAMILY_NAME \ --project=IMAGE_PROJECT \ --zone=ZONE
更改下列內容:
REST
對 imageFamilyViews
方法發出 GET
要求:
GET https://compute.googleapis.com/compute/v1/projects/IMAGE_PROJECT/zones/ZONE/imageFamilyViews/IMAGE_FAMILY_NAME
更改下列內容:
自訂映像檔系列
您可以為自訂映像檔建立自訂映像檔系列。映像檔系列會指向您用來建立映像檔系列的最新映像檔。如要將映像檔系列復原為先前的映像檔版本,您可以淘汰該系列中最新的映像檔 (前提是先前的映像檔未淘汰)。詳情請參閱在映像檔系列中設定映像檔版本。
如要使用映像檔系列建立映像檔,或是建立映像檔系列,您必須在映像檔建立步驟中新增額外的 --family
標記,例如:
gcloud compute images create my-application-v3-20210101 \ --source-disk my-application-disk-1 \ --source-disk-zone us-central1-f \ --family my-application
執行此指令後,任何呼叫若要執行以 my-application
映像檔為基礎的執行個體,都會指向新建立的映像檔 my-application-v3-20210101
。
選取映像檔系列名稱時,請參閱命名慣例。
如何使用映像檔系列
雖然映像檔系列可讓您參照最新的映像檔,但最新的映像檔可能會與應用程式不相容,如果未經過驗證,可能會在實際環境中導致問題。如要同時享有映像檔系列的優點和降低風險,建議您先測試映像檔系列中最新的參照映像檔,再將其用於正式環境。
總結來說,您可以考慮採用下列做法:
- 設定與實際工作環境分開的測試環境。
- 在測試環境中完成下列步驟:
- 使用來源映像檔系列建立自訂映像檔系列。
- 驗證自訂映像檔系列中新映像檔與工作負載的穩定性。
- 完成驗證後,請將這個自訂映像檔系列移至正式環境。
例如,這項程序可能會類似以下程序。
在測試專案中,使用來源映像檔系列建立映像檔。這個新的圖片來源系列也必須有自己的自訂圖片系列,才能在測試環境中參照。如要使用自訂映像檔系列建立映像檔,請執行下列指令:
gcloud compute images create test-image-name \ --source-image-project source-project \ --source-image-family source-image-family \ --project test-project \ --family test-image-family
更改下列內容:
test-image-name
:測試圖片的名稱。source-project
:來源圖片系列所屬的專案。source-image-family
:來源圖片系列的名稱。test-project
:您要新增圖片系列的測試專案名稱。test-image-family
:測試圖片群組的名稱。
使用自訂映像檔系列
test-image-family
建立 VM,以便測試工作負載。如要建立 VM,請執行下列指令:gcloud compute instances create test-instance-name \ --image-family your-test-image-family \ --project test-project
更改下列內容:
test-instance-name
:測試執行個體的名稱。test-image-family
:測試圖片群組的名稱。test-project
:測試專案名稱。
驗證這張圖片是否適用於工作負載後,請將圖片複製到實際工作環境。
gcloud compute images create prod-image-name \ --source-image-family test-image-family \ --source-image-project test-project \ --project prod-project \ --family prod-image-family
更改下列內容:
prod-image-name
:正式版映像檔的名稱。test-image-family
:測試圖片群組的名稱。test-project
:測試映像檔系列所屬的專案。prod-project
:實際環境中的專案名稱。prod-image-family
:您要在實際環境中使用的圖片系列名稱。