本文件說明如何暫停或重新啟用 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
-
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.
-
必要的角色
如要取得暫停或恢復運作運算執行個體所需的權限,請要求管理員為您授予執行個體的 Compute 執行個體管理員 (v1) (roles/compute.instanceAdmin.v1
) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
這個預先定義的角色具備暫停或重新啟動運算執行個體所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:
所需權限
如要暫停或恢復運算執行個體,您必須具備下列權限:
-
如要暫停執行個體,請按照下列步驟操作:
compute.instances.suspend
-
如要繼續執行例項,請按照下列步驟操作:
compute.instances.resume
限制
暫停運算執行個體時,會受到下列限制:
只有在客體作業系統支援的情況下,您才能暫停執行個體。詳情請參閱「作業系統詳細資訊」。
您必須先設定作業系統,才能暫停使用 Debian 8 或 9 做為訪客作業系統的執行個體。
您最多只能暫停執行個體 60 天,之後 Compute Engine 會自動將其狀態轉換為
TERMINATED
。您可以暫停 Spot VM 或先占執行個體,但如果 Compute Engine 在暫停作業完成前先占執行個體,則會結束暫停作業並先占執行個體。
您無法暫停搭載 GPU 的執行個體。
您無法暫停 Bare Metal 執行個體。
您無法暫停機密 VM。
您無法使用訪客環境內建的標準程序暫停執行個體。不支援 Ubuntu 16.04 以上版本的
systemctl suspend
等指令。如果呼叫,Compute Engine 就會忽略在客體內的信號。您無法暫停記憶體超過 208 GB 的執行個體。
您無法暫停已連接 CSEK 保護磁碟的執行個體。
在 Debian 8 或 9 中啟用暫停作業
如果運算執行個體以 Debian 8 和 9 做為其訪客作業系統,則在暫停執行個體前,您必須透過下列任一操作啟用暫停和恢復作業:
設定 ACPID
如要在 Debian 8 或 9 中啟用暫停和繼續運作作業,您可以設定進階設定和電源介面事件 Daemon (ACPID) 來處理休眠按鈕事件。啟用深度休眠按鈕事件後,您可以新增殼層指令碼,處理本節所述的休眠事件。
如要設定 ACPID 以支援暫停和繼續作業,請執行下列操作:
如果您尚未連線,請連線至 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
安裝 D-Bus
如要在 Debian 8 或 9 中啟用暫停和恢復作業,您可以安裝 D-Bus。
如要在運算執行個體的訪客 OS 中安裝 D-Bus,請在 OS 使用 Debian 8 或 9 時執行下列操作:
如果您尚未連線,請連線至 Linux 執行個體。
安裝 D-Bus:
sudo apt-get install dbus
如要讓變更生效,請重新啟動
logind
:sudo systemctl restart systemd-logind.service
暫停執行個體
如果運算執行個體的來賓作業系統使用 Debian 8 或 9,則在暫停執行個體前,您必須設定來賓作業系統,以便支援暫停和繼續作業,如本文所述。
如要暫停執行個體,請根據執行個體是否已連結本機 SSD 磁碟,使用下列方法:
如果執行個體未連結任何本機 SSD 磁碟,請參閱暫停沒有本機 SSD 磁碟的執行個體。
如果執行個體已連結任何本機 SSD 磁碟,且您想要捨棄或保留 (預覽) 磁碟的本機 SSD 資料,請參閱「暫停具有本機 SSD 磁碟的執行個體」。
暫停沒有本機 SSD 磁碟的執行個體
您可以同時或個別暫停多個運算執行個體。如果是多個執行個體,請使用 Google Cloud 控制台;如果是位於同一區域的執行個體,請使用 Google Cloud CLI。針對個別執行個體,請選取下列任一選項:
主控台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
選取要暫停的一或多個執行個體。
依序按一下
「停權」和「停權」,確認將使用者停權。
gcloud
如要在單一區域中將一或多個執行個體停用,請使用 gcloud compute instances suspend
指令:
gcloud compute instances suspend INSTANCE_NAMES \
--zone=ZONE
更改下列內容:
INSTANCE_NAMES
:以空格分隔的例項名稱清單,例如instance-01 instance-02 instance-03
。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
更改下列內容:
PROJECT_ID
:執行個體所在專案的 ID。ZONE
:執行個體所在的區域。INSTANCE_NAME
:執行個體的名稱。
暫停含有本機 SSD 磁碟的執行個體
根據您要同時暫停的運算執行個體數量,以及是否需要保留已連結本機 SSD 磁碟的資料,請執行下列操作:
如要保留已連結至執行個體 (不含 Z3 執行個體) 的本機 SSD 磁碟資料,請使用 gcloud CLI 或 REST API 暫停執行個體。
如要同時暫停多個執行個體,請使用 Google Cloud 控制台,或是位於相同區域的執行個體的 gcloud CLI。
如要暫停已連接本機 SSD 磁碟的一或多個執行個體,請選取下列任一選項:
主控台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
選取要暫停的一或多個執行個體。
依序按一下
「停權」和「停權」,確認將使用者停權。
gcloud
在已連接本機 SSD 磁碟的單一區域中暫停一或多個執行個體時,請指定是否要捨棄或保留本機 SSD 資料,如下所示:
如要捨棄本機 SSD 資料,請使用
gcloud compute instances suspend
指令,並加上--discard-local-ssd=true
標記:gcloud compute instances suspend INSTANCE_NAMES \ --discard-local-ssd=true \ --zone=ZONE
如要保留本機 SSD 資料,請使用
gcloud beta compute instances suspend
指令並加上--discard-local-ssd=false
標記:gcloud beta compute instances suspend INSTANCE_NAMES \ --discard-local-ssd=false \ --zone=ZONE
更改下列內容:
INSTANCE_NAMES
:以空格分隔的例項名稱清單,例如instance-01 instance-02 instance-03
。ZONE
:執行個體所在的區域。
REST
暫停已連結本機 SSD 磁碟的執行個體時,請指定是否要捨棄或保留本機 SSD 資料,如下所示:
如要捨棄本機 SSD 資料,請對
instances.suspend
方法發出POST
要求。在要求網址中加入設為true
的discardLocalSsd
查詢參數:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/suspend?discardLocalSsd=true
如要保留本機 SSD 資料,請對
beta.instances.suspend
方法發出POST
要求。在要求網址中加入設為false
的discardLocalSsd
查詢參數: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 控制台;如果是位於同一區域的執行個體,請使用 gcloud CLI。針對個別執行個體,請選取下列任一選項:
主控台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
選取一或多個要恢復的已暫停執行個體。
依序點選
「啟動 / 繼續」和「啟動」。
gcloud
如要在單一區域中重新啟用一或多個已暫停的執行個體,請使用 gcloud compute instances resume
指令:
gcloud compute instances resume INSTANCE_NAMES \
--zone=ZONE
更改下列內容:
INSTANCE_NAMES
:以空格分隔的例項名稱清單,例如instance-01 instance-02 instance-03
。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
更改下列內容:
INSTANCE_NAME
:要復原的暫停執行個體名稱。PROJECT_ID
:已暫停執行的執行個體所在專案 ID。ZONE
:已暫停的執行個體所在的區域。