本文件說明如何在執行個體資源上設定 deletionProtection
屬性,以免刪除特定的 VM 執行個體。如要進一步瞭解 VM 執行個體,請參閱執行個體說明文件。
在執行工作負載時,某些 VM 執行個體對應用程式或服務的執行可能尤其重要,例如執行 SQL 伺服器的執行個體、用作授權管理員的伺服器等。這些 VM 執行個體可能需要時時刻刻保持運作,因此得採取保護措施,以免 VM 遭到刪除。
設定 deletionProtection
旗標,可保護 VM 執行個體以防不慎刪除。如有使用者嘗試刪除設有 deletionProtection
旗標的 VM 執行個體,該要求會失敗。使用者的角色必須具有 compute.instances.create
權限,才可重設旗標,允許刪除資源。
事前準備
- 參閱執行個體說明文件。
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 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.
Go
To use the Go samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
Java
To use the Java samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
Node.js
To use the Node.js samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
PHP
To use the PHP samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
Python
To use the Python samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
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.
-
規格
防刪除功能無法防止下列動作:
一般 VM 和先佔 VM 均可使用防刪除功能。
防刪除功能無法使用在屬於代管執行個體群組的 VM 上,但可用在屬於非受管執行個體群組的執行個體上。
無法在執行個體範本中指定防刪除功能。
權限
如要執行這項工作,您必須擁有以下權限或者對資源具有以下 IAM 角色。
權限
compute.instances.create
角色
compute.admin
compute.instanceAdmin.v1
於建立執行個體時設定防刪除功能
根據預設,系統會停用執行個體的刪除保護功能。如要啟用防刪除功能,請參閱以下操作說明。
主控台
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
展開「Advanced options」(進階選項) 區段,然後執行下列操作:
- 展開「管理」部分。
- 勾選「Enable deletion protection」(啟用防刪除功能) 核取方塊。
繼續執行 VM 建立程序。
gcloud
建立 VM 執行個體時,請加入 --deletion-protection
或 no-deletion-protection
旗標。根據預設,系統會停用防刪除功能。如要啟用,請使用:
gcloud compute instances create [INSTANCE_NAME] --deletion-protection
其中的 [INSTANCE_NAME]
是需要的執行個體的名稱。
於建立時停用防刪除功能:
gcloud compute instances create [INSTANCE_NAME] --no-deletion-protection
Go
Java
Node.js
Python
REST
在 API 中建立 VM 執行個體時,請在要求主體中加入 deletionProtection
屬性。例如:
POST https://compute.googleapis.com/compute/v1/projects/[PROJECT_ID]/zones/[ZONE]/instances
{
"name": "[INSTANCE_NAME]",
"deletionProtection": "true",
...
}
如要停用防刪除功能,請將 deletionProtection
設為 false
。
判斷執行個體是否已啟用防刪除功能
您可以判斷執行個體是否已在 gcloud tool
或 API 中啟用防刪除功能。
主控台
前往「VM instances」(VM 執行個體) 頁面。
如果出現系統提示,請選取您的專案,然後按一下 [Continue] (繼續)。
在「VM instances」(VM 執行個體) 頁面上,下拉 [Columns] (欄) 選單,然後啟用 [Deletion protection] (防刪除功能)。
此時會顯示具有防刪除功能圖示的新欄。如果 VM 已啟用防刪除功能,執行個體名稱旁會出現圖示。
gcloud
在 gcloud CLI 中,執行 instances describe
指令,並搜尋防刪除功能的欄位。例如:
gcloud compute instances describe example-instance | grep "deletionProtection"
該工具會傳回 deletionProtection
屬性的值,並設為 true
或 false
:
deletionProtection: false
Go
Java
Node.js
Python
REST
在 API 中,發出 GET
要求並尋找 deletionProtection
欄位:
GET https://compute.googleapis.com/compute/v1/projects/[PROJECT_ID]/zones/[ZONE]/instances/[INSTANCE_NAME]
切換現有執行個體的防刪除功能
無論執行個體的目前狀態為何,均可切換現有執行個體的防刪除功能。確切來說,不用停止執行個體,也可以啟用或停用防刪除功能。
主控台
前往「VM instances」(VM 執行個體) 頁面。
如果出現系統提示,請選取您的專案,然後按一下 [Continue] (繼續)。
按一下您要切換防刪除功能的執行個體名稱。執行個體詳細資料頁面隨即會顯示。
請在執行個體詳細資料頁面上,完成以下步驟:
- 按一下頁面頂端的 [Edit] (編輯) 按鈕。
在「Deletion Protection」(防刪除功能) 下方,勾選方塊可啟用防刪除功能,取消勾選則可停用。
儲存變更。
gcloud
使用 gcloud CLI,以 --deletion-protection
或 --no-deletion-protection
旗標執行 update
指令:
gcloud compute instances update [INSTANCE_NAME] \
[--deletion-protection | --no-deletion-protection]
舉例來說,如要啟用名為 example-vm
的執行個體的防刪除功能:
gcloud compute instances update example-vm --deletion-protection
Go
Java
Node.js
Python
REST
在 API 中,可使用 deletionProtection
查詢參數,向 setDeletionProtection
方法發出 POST
要求。例如:
POST https://compute.googleapis.com/compute/v1/projects/[PROJECT_ID]/zones/[ZONE]/instances/[INSTANCE_NAME]/setDeletionProtection?deletionProtection=true
如要停用防刪除功能,請將 deletionProtection
設為 false
。請勿透過此要求提供要求主體。