本文說明在 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.
- 設定與實際工作環境不同的測試環境。
- 在測試環境中,完成下列步驟:
- 從來源映像檔系列建立自訂映像檔系列。
- 針對工作負載,驗證自訂映像檔系列中新映像檔的穩定性。
- 驗證完成後,請將這個自訂映像檔系列移至正式環境。
在測試專案中,從來源映像檔系列建立映像檔。這個新的圖片來源系列也必須有自己的自訂圖片系列,才能在測試環境中參照。如要使用自訂映像檔系列建立映像檔,請執行下列指令:
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
:要在正式環境中使用的映像檔系列名稱。
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 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.
詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。
公開映像檔系列
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
使用
gcloud compute images describe-from-family
指令並加上--zone
旗標: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
。為映像檔系列選取名稱時,請參閱命名慣例。
如何使用映像檔系列
雖然映像檔系列可讓您參照最新映像檔,但最新映像檔可能與應用程式不相容,如果未經過驗證,可能會導致生產環境發生問題。如要充分運用映像檔系列的優點,同時降低風險,建議您先測試映像檔系列中參照的最新映像檔,再將其用於正式環境。
總而言之,您可以考慮採取下列做法:
舉例來說,這個程序可能類似於下列程序。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-11 (世界標準時間)。
-