本文將說明如何替換、移除或刪除刊登位置政策。
在下列情況中,取代、移除或刪除刊登位置政策很有用:
取代虛擬機器 (VM) 執行個體中的配置政策,將 VM 移至相對於其他 VM 的不同位置。
如果您不再需要 VM 相對於其他 VM 的位置,可以從 VM 移除放置位置政策。
不再需要配置政策時,可以將其刪除。
事前準備
- 查看刊登位置政策的 限制。
-
如果尚未設定驗證,請先完成設定。
「驗證」是指驗證身分的程序,確認您有權存取 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」。
必要的角色
如要取得取代、移除或刪除放置群組政策所需的權限,請要求管理員授予您專案的 Compute 執行個體管理員 (v1) (
roles/compute.instanceAdmin.v1
) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。這個預先定義的角色具備取代、移除或刪除配置政策所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:
所需權限
如要取代、移除或刪除刊登位置政策,您必須具備下列權限:
-
如要取代或移除 VM 的配置政策,請按照下列步驟操作:
compute.instances.update
on the project -
如要刪除配置政策:
compute.resourcePolicies.delete
on the project
替換 VM 中的配置政策
在 VM 中以密集配置政策取代分散式配置政策前,請確認 VM 使用支援的機型和主機維護政策。如要在更換 VM 的放置政策前更新 VM,請執行下列一或多項操作:
在 VM 中取代放置位置政策後,您必須重新啟動 VM,新套用的放置位置政策才會生效。
如要取代 VM 中的放置位置政策,請選取下列任一選項:
gcloud
建立空白 YAML 檔案。
如要將 VM 的屬性匯出至您剛建立的 YAML 檔案,請使用
gcloud compute instances export
指令。gcloud compute instances export VM_NAME \ --destination=FILE_PATH \ --zone=ZONE
更改下列內容:
VM_NAME
:現有 VM 的名稱,用於指定放置位置政策。FILE_PATH
:您在上一步建立的 YAML 檔案路徑。ZONE
:VM 所在的區域。
在 YAML 設定檔中,編輯
resourcePolicies
欄位的值,指定其他刊登位置政策。resourcePolicies: - https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/resourcePolicies/POLICY_NAME
更改下列內容:
PROJECT_ID
:刊登位置政策所在的專案 ID。您只能指定與 VM 位於相同專案的配置方式政策。REGION
:放置群組政策所在的區域。POLICY_NAME
:刊登位置政策的名稱。
如要更新及重新啟動 VM,請使用
gcloud compute instances update-from-file
指令,並將--most-disruptive-allowed-action
旗標設為RESTART
。gcloud compute instances update-from-file VM_NAME \ --most-disruptive-allowed-action=RESTART \ --source=YAML_FILE \ --zone=ZONE
更改下列內容:
VM_NAME
:VM 名稱。YAML_FILE
:YAML 檔案的路徑,內含您在上一步修改的設定資料。ZONE
:VM 所在的區域。
REST
建立空白的 JSON 檔案。
如要查看現有 VM 的屬性,請對
instances.get
方法發出GET
要求。GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME
更改下列內容:
PROJECT_ID
:VM 所在的專案 ID。ZONE
:VM 所在的區域。VM_NAME
:現有 VM 的名稱,用於指定放置位置政策。
在您於上一個步驟中建立的空白 JSON 檔案中,執行下列操作:
輸入
GET
要求輸出內容中的 VM 屬性。找出
resourcePolicies
欄位,然後編輯其值,指定其他刊登位置政策。"resourcePolicies": [ "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/resourcePolicies/POLICY_NAME" ]
更改下列內容:
PROJECT_ID
:刊登位置政策所在的專案 ID。您只能指定與 VM 位於相同專案的配置方式政策。REGION
:刊登位置政策所在的區域。POLICY_NAME
:放置策略的名稱。
如要更新 VM 並重新啟動,請對
instances.update
方法發出PUT
要求。在要求中執行下列操作:在要求網址中,加入設為
RESTART
的most_disruptive_allowed_action
查詢參數。在要求主體中,使用您在上一個步驟中建立及更新的 JSON 檔案中的 VM 設定詳細資料。
PUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME?most_disruptive_allowed_action=RESTART { ... "resourcePolicies": [ "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/resourcePolicies/POLICY_NAME" ], ... }
如要進一步瞭解如何更新 VM 屬性,請參閱「更新 VM 屬性」。
從 VM 移除配置政策
如要從 VM 移除具有兩個以上可用性網域的散布位置政策,不必停止 VM 即可完成這項操作。如要移除密集配置政策或只有一個可用性網域的散佈配置政策,請先停止 VM,再移除附加的配置政策。
從 VM 移除配置政策不會影響其實體位置。 不過,如果 VM 正在即時遷移,Compute Engine 可能會將 VM 移至其他實體位置。
如要從 VM 移除配置政策,請選取下列任一選項:
gcloud
如要從 VM 移除放置政策,請使用
gcloud compute instances remove-resource-policies
指令。gcloud compute instances remove-resource-policies VM_NAME \ --resource-policies=POLICY_NAME \ --zone=ZONE
更改下列內容:
VM_NAME
:現有 VM 的名稱,用於指定放置位置政策。POLICY_NAME
:套用至 VM 的刊登位置政策名稱。如要驗證放置位置政策的名稱,請查看 VM 的詳細資料,並檢查resourcePolicies
欄位的值。ZONE
:VM 所在的區域。
REST
如要從 VM 移除配置政策,請對
instances.removeResourcePolicies
方法發出POST
要求。POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/removeResourcePolicies { "resourcePolicies": [ "projects/PROJECT_ID/regions/REGION/resourcePolicies/POLICY_NAME" ] }
更改下列內容:
PROJECT_ID
:VM 所在的專案 ID。ZONE
:VM 所在的區域。VM_NAME
:現有 VM 的名稱,用於指定放置位置政策。REGION
:放置群組政策所在的區域。POLICY_NAME
:套用至 VM 的刊登位置政策名稱。如要驗證放置位置政策的名稱,請查看 VM 的詳細資料,並檢查resourcePolicies
欄位的值。
刪除配置政策
只有在任何 Compute Engine 資源都未套用放置群組政策時,您才能刪除該政策。否則無法刪除配置政策。 如要刪除套用至一或多項 Compute Engine 資源的放置位置政策,請採取下列其中一種做法:
如果政策只套用至 VM,且您想保留 VM,請執行下列操作:
從 VM 移除配置政策,如本文所述。
按照本節說明刪除配置政策。
如果沒有,請按照下列步驟操作:
選用: 從要保留的任何 VM 中移除配置政策,如本文所述。
依下列順序刪除套用放置群組政策的所有其他 Compute Engine 資源:
VM
保留項目
執行個體範本
按照本節說明刪除配置政策。
如要刪除配置政策,請選取下列任一選項:
gcloud
如要刪除配置政策,請使用
gcloud compute resource-policies delete
指令。gcloud compute resource-policies delete POLICY_NAME \ --region=REGION
更改下列內容:
POLICY_NAME
:現有刊登位置政策的名稱。REGION
:放置群組政策所在的區域。
REST
如要刪除配置政策,請對
resourcePolicies.delete
方法發出DELETE
要求。DELETE https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/resourcePolicies/POLICY_NAME
更改下列內容:
PROJECT_ID
:放置位置政策所在的專案 ID。REGION
:放置群組政策所在的區域。POLICY_NAME
:現有刊登位置政策的名稱。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-08 (世界標準時間)。
[[["容易理解","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-08 (世界標準時間)。"],[[["This guide details the procedures for replacing, removing, and deleting placement policies for virtual machines (VMs) in Google Cloud."],["Replacing a placement policy involves updating the VM's configuration file and then restarting it to implement the new policy."],["Removing a placement policy from a VM can be done without stopping the VM for certain spread policies, but other policies require the VM to be stopped first."],["Deleting a placement policy requires ensuring that it is not applied to any Compute Engine resources, which may involve first removing it from associated VMs, reservations, or instance templates."],["The document also describes the necessary authentication steps, required roles, and permissions to make these changes, along with gcloud and REST examples for each operation."]]],[]] -