本文档介绍如何通过在 Instance 资源上设置 deletionProtection
属性来保护特定虚拟机实例免遭删除。要详细了解虚拟机实例,请参阅实例文档。
作为工作负载的一部分,可能存在某些对于运行应用或服务至关重要的虚拟机实例,例如运行 SQL Server 的实例、用作许可证管理器的服务器等。这些虚拟机实例可能需要无限期地保持运行,因此需要一种方法来保护这些虚拟机免遭删除。
通过设置 deletionProtection
标志,可以防止意外删除虚拟机实例。如果用户尝试删除您已为其设置 deletionProtection
标志的虚拟机实例,则请求会失败。只有已被授予具有 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.
-
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.
-
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.
-
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.
-
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.
-
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.
删除保护不会阻止以下操作:
删除保护可以应用于常规虚拟机和抢占式虚拟机。
删除保护不能应用于属于托管实例组的虚拟机,但可以应用于属于非托管实例组的实例。
不能在实例模板中指定删除保护。
compute.instances.create
compute.admin
compute.instanceAdmin.v1
在 Google Cloud 控制台中,前往创建实例页面。
展开高级选项部分,然后执行以下操作:
- 展开管理部分。
- 选中启用删除防护复选框。
继续虚拟机创建过程。
转到“虚拟机实例”页面。
如果出现提示,请选择您的项目并点击继续。
在虚拟机实例页面上,下拉列菜单并启用删除防护。
随即出现一个带有删除防护图标的新列。如果虚拟机启用了删除保护,则此图标将显示在实例名称旁边。
转到“虚拟机实例”页面。
如果出现提示,请选择您的项目并点击继续。
点击要切换删除保护的实例的名称。 此时将显示实例详情页面。
在实例详细信息页面中,完成以下步骤:
- 点击页面顶部的修改按钮。
在删除防护下,勾选复选框以启用删除防护,或取消选中复选框以停用删除防护。
保存更改。
Go
如需在本地开发环境中使用本页面上的 Go 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
Java
如需在本地开发环境中使用本页面上的 Java 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
Node.js
如需在本地开发环境中使用本页面上的 Node.js 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
PHP
如需在本地开发环境中使用本页面上的 PHP 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。
如需了解详情,请参阅 Set up authentication for a local development environment。
Python
如需在本地开发环境中使用本页面上的 Python 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
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 身份验证文档中的使用 REST 时进行身份验证。
规格
权限
要执行此任务,您必须已被授予资源的以下权限或以下某个 IAM 角色。
权限
角色
在实例创建期间设置删除保护
默认情况下,实例会停用删除保护。可使用以下说明启用删除保护。
控制台
gcloud
创建虚拟机实例时,请添加
--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 创建虚拟机实例时,在请求正文中添加
deletionProtection
属性。例如:POST https://compute.googleapis.com/compute/v1/projects/[PROJECT_ID]/zones/[ZONE]/instances { "name": "[INSTANCE_NAME]", "deletionProtection": "true", ... }
要停用删除保护,请将
deletionProtection
设置为false
。确定实例是否启用了删除保护
您可以在
gcloud tool
或 API 中确定实例是否启用了删除保护。控制台
gcloud
在 gcloud CLI 中,运行
instances describe
命令并搜索删除保护字段。例如:gcloud compute instances describe example-instance | grep "deletionProtection"
该工具会返回设置为
true
或false
的deletionProtection
属性的值: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]
为现有实例切换删除保护
无论实例的当前状态如何,您都可以切换现有实例的删除保护。具体而言,您不必在启用或停用删除保护之前停止实例。
控制台
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
。请勿提供带有请求的请求正文。后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-06-23。
-