本文档说明如何暂停或恢复 Compute Engine 实例。如需详细了解如何暂停、停止或重置实例,请参阅暂停、停止或重置 Compute Engine 实例。
如果您希望保留 Compute Engine 实例,但不想在未使用实例时产生费用,则可以暂停实例。暂停实例会保留实例,并将实例内存中的内容迁移到存储空间。恢复实例后,Compute Engine 会将实例内存从存储空间迁移回实例,然后实例会重新开始运行。
暂停 Compute Engine 实例适用于以下情况:
在关停期间(例如晚上或周末)未充分使用的开发和测试环境,您希望保留这些环境,以节省费用或者实现比创建新实例更快的初始化速度。
在实例启动完成后、准备处理第一个请求之前需要进行长时间初始化的应用,例如虚拟开发者工作站或复杂的 Java 应用。
准备工作
-
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 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
-
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
- Set a default region and zone.
- Install the Google Cloud CLI.
-
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.
- Install the Google Cloud CLI.
-
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.
- Install the Google Cloud CLI.
-
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.
- Install the Google Cloud CLI.
-
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.
- Install the Google Cloud CLI.
-
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.
-
暂停实例:
compute.instances.suspend
-
恢复实例:
compute.instances.resume
只有在客户机操作系统支持的情况下,您才能暂停实例。如需了解详情,请参阅操作系统详细信息。
只有在暂停实例之前配置操作系统的情况下,您才能暂停使用 Debian 8 或 9 作为客户机操作系统的实例。
在 Compute Engine 自动将其状态转换为
TERMINATED
之前,您最多只能将实例暂停 60 天。您可以暂停 Spot 虚拟机或抢占式实例,但如果 Compute Engine 在暂停操作完成之前抢占实例,Compute Engine 会结束暂停操作并抢占实例。
您无法暂停挂接了 GPU 的实例。
您无法暂停裸金属实例。
您无法暂停机密虚拟机。
您无法使用客户机环境中内置的标准过程暂停实例。不支持 Ubuntu 16.04 或更高版本中的
systemctl suspend
等命令。如果调用这些命令,Compute Engine 会忽略客户机内信号。您无法暂停内存超过 208 GB 的实例。
您无法暂停挂接了受 CSEK 保护的磁盘的实例。
如果您尚未连接到 Linux 实例,请先连接。
在
acpi
文件夹中创建events
文件夹:sudo mkdir -p /etc/acpi/events/
将 ACPID 配置为处理睡眠按钮事件:
cat <<EOF | sudo tee /etc/acpi/events/sleepbtn-acpi-support event=button[ /]sleep action=/etc/acpi/sleepbtn-acpi-support.sh EOF
创建休眠事件处理脚本:
cat <<EOF | sudo tee /etc/acpi/sleepbtn-acpi-support.sh #!/bin/sh echo mem > /sys/power/state EOF
设置脚本的权限:
sudo chmod 755 /etc/acpi/sleepbtn-acpi-support.sh
如需使更改生效,请重启 ACPID:
sudo systemctl restart acpid.service
如果您尚未连接到 Linux 实例,请先连接。
安装 D-Bus:
sudo apt-get install dbus
如需使更改生效,请重启
logind
:sudo systemctl restart systemd-logind.service
如果您的实例没有挂接任何本地 SSD 磁盘,请参阅暂停没有本地 SSD 磁盘的实例。
如果您的实例挂接了任何本地 SSD 磁盘,并且您要舍弃或保留(预览版)磁盘的本地 SSD 数据,请参阅暂停具有本地 SSD 磁盘的实例。
在 Google Cloud 控制台中,前往虚拟机实例页面。
选择一个或多个要暂停的实例。
点击
暂停,然后点击暂停进行确认。INSTANCE_NAMES
:以空格分隔的实例名称列表,例如instance-01 instance-02 instance-03
。ZONE
:实例所在的可用区。PROJECT_ID
:实例所在项目的 ID。ZONE
:实例所在的区域。INSTANCE_NAME
:实例的名称。如需保留挂接到实例(不包括 Z3 实例)的本地 SSD 磁盘的数据,请使用 gcloud CLI 或 REST API 暂停实例。
如需同时暂停多个实例,请使用 Google Cloud 控制台或 gcloud CLI(对于位于同一可用区中的实例)。
在 Google Cloud 控制台中,前往虚拟机实例页面。
选择一个或多个要暂停的实例。
点击
暂停,然后点击暂停进行确认。如需舍弃本地 SSD 数据,请使用带有
--discard-local-ssd=true
标志的gcloud compute instances suspend
命令:gcloud compute instances suspend INSTANCE_NAMES \ --discard-local-ssd=true \ --zone=ZONE
如需保留本地 SSD 数据,请使用带有
--discard-local-ssd=false
标志的gcloud beta compute instances suspend
命令:gcloud beta compute instances suspend INSTANCE_NAMES \ --discard-local-ssd=false \ --zone=ZONE
INSTANCE_NAMES
:以空格分隔的实例名称列表,例如instance-01 instance-02 instance-03
。ZONE
:实例所在的可用区。如需舍弃本地 SSD 数据,请向
instances.suspend
方法发出POST
请求。在请求网址中添加discardLocalSsd
查询参数并设置为true
:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/suspend?discardLocalSsd=true
如需保留本地 SSD 数据,请向
beta.instances.suspend
方法发出POST
请求。在请求网址中添加discardLocalSsd
查询参数并设置为false
:POST https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/suspend?discardLocalSsd=false
PROJECT_ID
:实例所在项目的 ID。ZONE
:实例所在的区域。INSTANCE_NAME
:实例的名称。只有在实例所在的可用区中有足够的容量时,您才能恢复实例。这通常不是问题。如果您在恢复实例时遇到问题,请稍后重试。
如果您将本地 SSD 磁盘挂接到实例,并选择在暂停实例时保留本地 SSD 数据,则可能需要在恢复实例后重新装载本地 SSD 磁盘。如需了解详情,请参阅如何在 Linux 实例或 Windows 实例上重新装载非启动磁盘。
在 Google Cloud 控制台中,前往虚拟机实例页面。
选择一个或多个要恢复的已暂停实例。
点击
启动/恢复,然后点击启动。INSTANCE_NAMES
:以空格分隔的实例名称列表,例如instance-01 instance-02 instance-03
。ZONE
:已暂停的实例所在的可用区。INSTANCE_NAME
:要恢复的已暂停实例的名称。PROJECT_ID
:已暂停的实例所在项目的 ID。ZONE
:已暂停的实例所在的可用区。
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 的凭据。
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证。
所需的角色
如需获得暂停或恢复计算实例所需的权限,请让管理员为您授予实例的 Compute Instance Admin (v1) (
roles/compute.instanceAdmin.v1
) IAM 角色。如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。此预定义角色包含暂停或恢复计算实例所需的权限。如需查看所需的确切权限,请展开所需权限部分:
所需权限
您需要具备以下权限才能暂停或恢复计算实例:
限制
暂停计算实例时,需要遵守以下限制:
在 Debian 8 或 9 中启用暂停操作
如果计算实例运行 Debian 8 和 9 作为其客户机操作系统,则在暂停实例之前,您必须通过执行以下操作之一来启用暂停和恢复操作:
配置 ACPID
如需在 Debian 8 或 9 中启用暂停和恢复操作,您可以将高级配置和电源接口事件守护程序 (ACPID) 配置为处理睡眠按钮事件。启用深度睡眠按钮事件后,您可以添加用于处理睡眠事件的 Shell 脚本,如本部分所述。
如需将 ACPID 配置为支持暂停和恢复操作,请执行以下操作:
安装 D-Bus
如需在 Debian 8 或 9 中启用暂停和恢复操作,您可以安装 D-Bus。
如需在计算实例的客户机操作系统(使用 Debian 8 或 9 时)中安装 D-Bus,请执行以下操作:
暂停实例
如果计算实例的客户机操作系统使用的是 Debian 8 或 9,则在暂停实例之前,您必须按照文档中所述配置客户机操作系统,以支持暂停和恢复操作。
如需暂停实例,请根据实例是否挂接了本地 SSD 磁盘,使用以下方法之一:
暂停没有本地 SSD 磁盘的实例
您可以同时暂停多个计算实例或单个实例。对于多个实例,请使用 Google Cloud 控制台;对于位于同一可用区中的实例,请使用 Google Cloud CLI。对于单个实例,请选择以下任一选项:
控制台
gcloud
如需暂停单个可用区中的一个或多个实例,请使用
gcloud compute instances suspend
命令:gcloud compute instances suspend INSTANCE_NAMES \ --zone=ZONE
替换以下内容:
Go
Java
Node.js
PHP
Python
REST
如需暂停实例,请向
instances.suspend
方法发出POST
请求:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/suspend
替换以下内容:
暂停具有本地 SSD 磁盘的实例
根据您要同时暂停的计算实例数量以及是否需要保留其挂接的本地 SSD 磁盘中的数据,执行以下操作:
如需暂停一个或多个挂接了本地 SSD 磁盘的实例,请选择以下选项之一:
控制台
gcloud
在暂停单个可用区中挂接了本地 SSD 磁盘的一个或多个实例时,请指定是舍弃还是保留本地 SSD 数据,如下所示:
替换以下内容:
REST
暂停挂接了本地 SSD 磁盘的实例时,请指定是舍弃还是保留本地 SSD 数据,如下所示:
替换以下内容:
恢复已暂停的实例
在恢复已暂停的计算实例之前,请考虑以下事项:
您可以同时恢复多个实例或单个实例。对于多个实例,请使用 Google Cloud 控制台;对于位于同一可用区中的实例,请使用 gcloud CLI。对于单个实例,请选择以下任一选项:
控制台
gcloud
如需恢复单个可用区中一个或多个已暂停的实例,请使用
gcloud compute instances resume
命令:gcloud compute instances resume INSTANCE_NAMES \ --zone=ZONE
替换以下内容:
Go
Java
Node.js
PHP
Python
REST
如需恢复已暂停的实例,请向
instances.resume
方法发出POST
请求:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/resume
替换以下内容:
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-02-18。
-