本文件說明如何停止或重新啟動 Compute Engine 執行個體。如要進一步瞭解如何暫停、停止或重設執行個體,請參閱「暫停、停止或重設 Compute Engine 執行個體」。
當您不再使用執行個體,或要修改其屬性 (例如變更機器類型,或移除任何已連結和掛載的磁碟) 時,停止執行個體就很實用。停止執行個體後,您可以執行下列操作:
重新啟動即可繼續工作負載。
如果不再需要,請刪除該帳戶。
如要自動停止或重新啟動執行個體,請改為參閱以下文章:
事前準備
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 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 Instance Admin (v1) (roles/compute.instanceAdmin.v1
) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
這個預先定義的角色具備停止或重新啟動運算執行個體所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:
所需權限
如要停止或重新啟動運算執行個體,您必須具備下列權限:
-
如要停止執行個體,請執行下列操作:
compute.instances.stop
在執行個體上 -
如要透過來賓作業系統停止執行個體:
compute.instances.setMetadata
執行個體 (如果使用執行個體層級公開安全殼層金鑰)。 -
如要重新啟動執行個體,請按照下列步驟操作:
compute.instances.start
在執行個體上 -
如要重新啟動使用加密金鑰的執行個體,請按照下列步驟操作:
在執行個體上執行
compute.instances.startWithEncryptionKey
停止執行個體
當您停止運算執行個體,或 Compute Engine 為預定動作執行停止作業時,執行個體會保留其附加的資源、設定、內部 IP 位址、MAC 位址和中繼資料。不過,執行個體會遺失記憶體內資料和應用程式狀態。如果您需要保留這些狀態,請改為暫停執行個體。
您可以使用下列方法停止執行個體,具體取決於執行個體是否已連接本機 SSD 磁碟,以及您要如何處理任何關機指令碼:
-
您可以同時停止一或多個未連接任何本機 SSD 磁碟的執行個體。執行個體中的任何關機指令碼都必須在預設關機期限內完成執行。
-
當您同時停止連接本機 SSD 磁碟的一或多個執行個體時,可以選擇捨棄或保留 (預覽) 這些磁碟上的資料。執行個體中的任何關機指令碼都必須在預設關機期限內完成執行。
-
這種做法可讓您在關閉指令碼執行完畢後,停止單一執行個體,或是在您啟用正常關機功能時,在不正常關機的情況下停止執行個體,或結束正在進行的正常關機程序。除非您手動將任何已連結本機 SSD 磁碟的資料備份至持久儲存體,否則在執行個體的來賓作業系統中停止執行個體,就會捨棄這些磁碟上的所有資料。
停止沒有本機 SSD 硬碟的執行個體
視您在停止執行個體時要執行的操作而定,請使用下列選項:
如果您已在執行個體中啟用平穩關機功能,則可以使用 Google Cloud 控制台、gcloud CLI 或 REST API 停止執行個體,而不會中斷平穩關機程序,或結束正在進行的平穩關機程序。
如要同時停止多個執行個體,請使用 Google Cloud 控制台,如果是位於同一區域的執行個體,則請使用 gcloud CLI。
如要停止一或多個執行個體,請選取下列任一選項:
主控台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
選取要停止的執行中執行個體。
按一下
「停止」。在對話方塊中執行下列操作:
選用:如要停止執行個體而不進行優雅關機,或結束正在進行的優雅關機,請選取「Skip graceful shutdown (if applicable)」(略過優雅關機 (如適用) )核取方塊。
按一下「Stop」確認。
gcloud
如要停止同一個區域中一或多個執行個體,請使用 gcloud compute instances stop
指令:
gcloud compute instances stop INSTANCE_NAMES \
--zone=ZONE
更改下列內容:
INSTANCE_NAMES
:以空格分隔的執行個體名稱清單,例如instance-01 instance-02 instance-03
。ZONE
:執行個體所在的區域。
您也可以選擇在一個或多個執行個體中啟用正常關機功能,這樣一來,您就能停止執行個體,而不需要正常關機,也可以手動結束正在進行的正常關機程序。如要執行這項操作,請使用 gcloud beta compute instances stop
指令,並加上 --no-graceful-shutdown
旗標:
gcloud beta compute instances stop INSTANCE_NAMES \
--no-graceful-shutdown \
--zone=ZONE
Go
Java
Node.js
PHP
Python
REST
如要停止執行個體,請對 instances.stop
方法發出 POST
要求:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/stop
更改下列內容:
INSTANCE_NAME
:執行個體的名稱。PROJECT_ID
:執行個體所在專案的 ID。ZONE
:執行個體所在的區域。
您也可以選擇在執行個體中啟用安全關機功能,這樣一來,您就可以停止執行個體,而不會安全關機,也可以手動結束正在進行的安全關機程序。如要這樣做,請對 instances.stop
方法發出 POST
要求。在要求網址中加入 noGracefulShutdown=true
查詢參數:
POST https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/stop?noGracefulShutdown=true
停止具有本機 SSD 磁碟的執行個體
視您在停止運算執行個體時要執行的動作而定,請使用下列選項:
如果您已在執行個體中啟用平穩關機功能,則可以使用 Google Cloud 控制台、gcloud CLI 或 REST API 停止執行個體,而不會造成平穩關機或結束持續進行的平穩關機。
如要保留已連結至執行個體的本機 SSD 磁碟資料 (不含 Z3 執行個體),請使用 gcloud CLI 或 REST API 停止執行個體。
如要同時停止多個執行個體,請使用 Google Cloud 控制台,如果是位於同一區域的執行個體,則請使用 gcloud CLI。
如要停止連接本機 SSD 磁碟的一或多個執行個體,請選取下列任一選項:
主控台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
選取要停止的執行中執行個體。
按一下
「停止」。在對話方塊中執行下列操作:
選用:如要停止執行個體而不進行優雅關機,或結束正在進行的優雅關機,請選取「Skip graceful shutdown (if applicable)」(略過優雅關機 (如適用) )核取方塊。
按一下「Stop」確認。
gcloud
當您在同一個區域中停止一或多個已連接本機 SSD 磁碟的執行個體時,請指定是否要捨棄或保留本機 SSD 資料,如下所示:
如要捨棄本機 SSD 資料,請使用
gcloud compute instances stop
指令,並加上--discard-local-ssd=true
標記:gcloud compute instances stop INSTANCE_NAMES \ --discard-local-ssd=true \ --zone=ZONE
如要保留本機 SSD 資料,請使用
gcloud beta compute instances stop
指令並加上--discard-local-ssd=false
標記:gcloud beta compute instances stop INSTANCE_NAMES \ --discard-local-ssd=false \ --zone=ZONE
更改下列內容:
INSTANCE_NAMES
:以空格分隔的執行個體名稱清單,例如instance-01 instance-02 instance-03
。ZONE
:執行個體所在的區域。
您也可以選擇在一個或多個執行個體中啟用正常關機功能,這樣一來,您就能停止執行個體,而不需要正常關機,或結束正在進行的正常關機程序。如要執行這項操作,請使用 gcloud beta compute instances stop
指令,並加上 --no-graceful-shutdown
旗標:
gcloud beta compute instances stop INSTANCE_NAMES \
--discard-local-ssd=DISCARD_LOCAL_SSD \
--no-graceful-shutdown \
--zone=ZONE
將 DISCARD_LOCAL_SSD
替換為 true
可捨棄本機 SSD 磁碟中的資料,將 DISCARD_LOCAL_SSD
替換為 false
則可保留資料。
REST
停止已連接本機 SSD 磁碟的執行個體時,請指定是否要捨棄或保留本機 SSD 資料,如下所示:
如要捨棄本機 SSD 資料,請對
instances.stop
方法發出POST
要求。在要求網址中加入設為true
的discardLocalSsd
查詢參數:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/stop?discardLocalSsd=true
如要保留本機 SSD 資料,請對
beta.instances.stop
方法發出POST
要求。在要求網址中加入設為false
的discardLocalSsd
查詢參數:POST https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/stop?discardLocalSsd=false
更改下列內容:
PROJECT_ID
:執行個體所在專案的 ID。ZONE
:執行個體所在的區域。INSTANCE_NAME
:執行個體的名稱。
您也可以選擇在執行個體中啟用安全關機功能,這樣一來,您就可以停止執行個體,而不會安全關機,也可以手動結束正在進行的安全關機程序。如要這樣做,請對 instances.stop
方法發出 POST
要求。在要求網址中加入 noGracefulShutdown=true
查詢參數:
POST https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/stop?discardLocalSsd=DISCARD_LOCAL_SSD&noGracefulShutdown=true
將 DISCARD_LOCAL_SSD
替換為 true
可捨棄本機 SSD 磁碟中的資料,將 DISCARD_LOCAL_SSD
替換為 false
則可保留資料。
透過訪客 OS 停止執行個體
如果運算執行個體已連接本機 SSD 磁碟,關閉訪客作業系統會自動捨棄本機 SSD 資料。如要保留這類資料,請在停止執行個體前,手動將資料複製到永久性儲存空間選項。
如要透過訪客作業系統停止執行個體,請選取下列任一選項:
Linux
如果您尚未連線,請連線至執行個體。
如要停止執行個體,請選擇下列其中一種方法:
如要執行乾淨關機作業,讓執行個體在關閉訪客作業系統前執行關機指令碼,請執行下列指令:
sudo shutdown -h now
如要強制關機,請執行下列指令:
sudo poweroff
Windows
如果您尚未連線,請使用下列任一方法連線至執行個體:
如要停止執行個體,請選擇下列其中一種方法:
如要乾淨關閉執行個體,並讓執行個體在關閉訪客 OS 前執行關機指令碼,請執行下列指令:
shutdown /s
如要強制關機,請執行下列指令:
shutdown /f
重新啟動執行個體
您可以重新啟動已完全停止的運算執行個體,也就是執行個體狀態為 TERMINATED
時。
如果您在停止執行個體時選擇保留本機 SSD 磁碟的資料,則可能需要在重新啟動後重新掛載本機 SSD 磁碟。如要進一步瞭解如何掛接本機 SSD 磁碟,請參閱「格式化並掛接本機 SSD 裝置」。
如要重新啟動執行個體,請根據執行個體是否已連接加密磁碟,使用下列其中一種方法:
如果執行個體未附加加密磁碟,請參閱重新啟動未附加加密磁碟的執行個體。
否則,請參閱重新啟動具有加密磁碟的執行個體。
重新啟動沒有加密磁碟的執行個體
您可以同時或個別重新啟動多個運算單元。如果是多個執行個體,請使用 Google Cloud 控制台;如果是位於同一區域的執行個體,請使用 gcloud CLI。針對個別例項,請選取下列任一選項:
主控台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
選取一或多個執行個體。
按一下
「開始 / 繼續」。
gcloud
如要在同一個區域中重新啟動一或多個執行個體,請使用 gcloud compute instances start
指令:
gcloud compute instances start INSTANCE_NAMES \
--zone=ZONE
更改下列內容:
INSTANCE_NAMES
:以空格分隔的執行個體名稱清單,例如instance-01 instance-02 instance-03
。ZONE
:執行個體所在的區域。
Go
Java
Node.js
PHP
Python
REST
如要重新啟動執行個體,請對 instances.start
方法發出 POST
要求:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/start
更改下列內容:
INSTANCE_NAME
:要重新啟動的執行個體名稱。PROJECT_ID
:執行個體所在專案的 ID。ZONE
:執行個體所在的區域。
重新啟動具有加密磁碟的執行個體
重新啟動已停止的運算執行個體時,如果該執行個體具有已使用客戶提供的加密金鑰加密的已連接磁碟,則必須提供加密金鑰資訊。
您可以同時重新啟動多個執行個體,也可以個別重新啟動。如果是多個執行個體,請使用 Google Cloud 控制台;如果是位於同一區域的執行個體,請使用 gcloud CLI。針對個別執行個體,請選取下列任一選項:
主控台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
選取要重新啟動的執行個體。
按一下
「開始 / 繼續」。為連接到執行個體的每個加密磁碟指定加密金鑰,然後按一下「Start」。
gcloud
如要在同一個區域內重新啟動一或多個使用加密磁碟的執行個體,請使用 gcloud compute instances start
指令,並加上 --csek-key-file
標記。如果您使用的是 RSA 包裝金鑰,請改用 gcloud beta compute instances start
指令,並加上 --csek-key-file
標記:
gcloud compute instances start INSTANCE_NAMES \
--csek-key-file=ENCRYPTION_KEY_FILE \
--zone=ZONE
更改下列內容:
INSTANCE_NAMES
:以空格分隔的執行個體名稱清單,例如instance-01 instance-02 instance-03
。ENCRYPTION_KEY_FILE
:包含客戶提供的加密金鑰 JSON 檔案的相對路徑。只有在執行個體使用相同的客戶提供加密金鑰時,才能同時重新啟動多個執行個體。ZONE
:執行個體所在的區域。
Go
Java
Node.js
PHP
Python
REST
如要重新啟動使用加密磁碟的執行個體,請對 instances.startWithEncryptionKey
方法發出 POST
要求:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/startWithEncryptionKey
{
"disks": [
{
"source": "DISK_URL",
"diskEncryptionKey": {
"ENCRYPTION_TYPE": "ENCRYPTION_KEY"
}
}
]
}
更改下列內容:
PROJECT_ID
:執行個體所在專案的 ID。ZONE
:執行個體所在的區域。INSTANCE_NAME
:執行個體的名稱。DISK_URL
:資源網址:與已連接磁碟的完整資源名稱相對應,該磁碟是使用客戶提供的加密金鑰加密過。ENCRYPTION_TYPE
:您使用的磁碟加密類型,可為下列任一類型:rawKey
、kmsKeyName
或rsaEncryptedKey
。如果您使用rsaEncryptedKey
類型,請對beta.instances.startWithEncryptionKey
方法發出POST
要求。ENCRYPTION_KEY
:用來加密連結至執行個體的永久磁碟的加密金鑰。rawKey
或rsaEncryptedKey
金鑰必須採用 Base64 編碼。此外,如要準備rsaEncryptedKey
金鑰,請參閱「RSA 金鑰包裝」。