如要測試地區代管執行個體群組 (MIG) 是否已過度佈建,且能應對區域服務中斷的情形,可以使用下列範例模擬區域故障。
事前準備
- 如要使用本指南提供的指令列範例,請安裝 Google Cloud CLI。
-
如果尚未設定驗證,請先完成設定。
「驗證」是指驗證身分的程序,確認您有權存取 Trusted Cloud by S3NS 服務和 API。如要從本機開發環境執行程式碼或範例,請選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
gcloud
-
安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI。 登入後,執行下列指令初始化 Google Cloud CLI:
gcloud init
- 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」。
使用指令碼模擬區域服務中斷
這個指令碼會停止和啟動 Apache 來做為預設案例。如果這個指令碼不適用於您的應用程式,請以您自己的失敗和復原案例取代停止和啟動 Apache 的指令。
在群組的每個 VM 中持續部署並執行這個指令碼。您可以透過將這個指令碼新增至執行個體範本,或者將這個指令碼包含在自訂映像檔中並在執行個體範本中使用該映像檔,來進行這項作業。
透過設定以下兩個專案中繼資料欄位來模擬區域失敗的情形:
failed_zone
:設定您要模擬服務中斷的區域 (將故障範圍限制在單一區域)。failed_instance_names
:依名稱選擇要使其離線的 VM (將故障範圍限制於只包含此字串的 VM 名稱)。
您可以使用 gcloud CLI 設定此中繼資料。舉例來說,下列指令會將區域服務中斷設為
europe-west1-b
區域,並影響名稱以base-instance-name
開頭的 VM:gcloud compute project-info add-metadata --metadata failed_zone='europe-west1-b',failed_instance_names='base-instance-name-'
完成模擬服務中斷後,請透過移除中繼資料鍵從故障中復原:
gcloud compute project-info remove-metadata --keys failed_zone,failed_instance_names
以下提供一些您可以使用這個指令碼執行之失敗案例的建議:
- 完全停止應用程式,查看 MIG 的回應方式。
- 讓 VM 在進行負載平衡健康狀態檢查時傳回「健康狀態不良」。
- 修改 iptables,封鎖進出 VM 的部分流量。
- 關閉 VM。根據預設,地區 MIG 將在不久之後重新建立那些執行個體,但只要設定了中繼資料值,新的執行個體將在該指令碼執行時立即自行關閉。如此將會導致重複當機。
後續步驟
- 瞭解如何建立可擴充且有彈性的網路應用程式。
- 瞭解 Google Cloud Platform 上的災難復原。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-19 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-19 (世界標準時間)。"],[[["\u003cp\u003eThis guide explains how to simulate a zonal failure to test if a regional managed instance group (MIG) is overprovisioned enough to handle outages.\u003c/p\u003e\n"],["\u003cp\u003eThe simulation involves using a script that can be deployed in each VM within the group to stop and start a service, such as Apache, to mimic failure and recovery scenarios.\u003c/p\u003e\n"],["\u003cp\u003eTo simulate a zone outage, two project metadata fields need to be set: \u003ccode\u003efailed_zone\u003c/code\u003e, specifying the zone to target, and \u003ccode\u003efailed_instance_names\u003c/code\u003e, which identifies the VMs to affect.\u003c/p\u003e\n"],["\u003cp\u003eYou can use the \u003ccode\u003egcloud\u003c/code\u003e CLI to set the metadata values for the simulated outage and then remove them to recover from the simulated failure.\u003c/p\u003e\n"],["\u003cp\u003eThe provided failure script can be used to test various failure scenarios, including stopping the application, causing VMs to return as "unhealthy," modifying iptables, and shutting down VMs.\u003c/p\u003e\n"]]],[],null,[]] -