本文說明如何在區域或地區之間移動虛擬機器 (VM) 執行個體。
事前準備
- 詳閱區域說明文件。
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要在本機開發環境中執行程式碼或範例,您可以選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
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.
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.
-
需求條件
本節列出在區域和區域之間移動 VM 的需求條件:
專案配額:您的專案必須有足夠的配額,才能執行下列操作:
- 建立新的快照。
- 推送所有臨時外部 IP 位址。
在目標地區域中建立新的 VM 和磁碟。
舉例來說,如果您有三個磁碟連結到要移動的 VM,則需要有足夠的配額才能建立三個暫時的永久磁碟快照和三個新磁碟。建立新的磁碟後,您可以刪除暫時的快照。
永久磁碟:您要移動的 VM 所連結的永久磁碟並未連結至其他 VM。
本機 SSD本機 SSD 屬於暫存空間,手動終止 VM 會刪除本機 SSD 中的資料,如果您需要保留本機 SSD 資料,請使用永久磁碟等具有耐用性的儲存選項來複製資料。
GPU。如果您的 VM 含有 GPU,請確認您要使用的 GPU 適用於 VM 的目標區域。如需 GPU 及其可用區域的清單,請參閱 Compute Engine 中的 GPU 一文。
子網路:如果您想在地區之間移動 VM (例如在
us-west1-a
和asia-south1-b
之間),且 VM 屬於子網路,則必須為 VM 選取新的子網路。如要瞭解如何建立子網路,請參閱「新增子網路」一節。
限制
如果您將 VM 移至其他區域,就無法保留 VM 的暫時性內部或外部 IP 位址。重新建立 VM 時,您必須選擇新的 IP 位址。
資源屬性
如要移動 VM,您必須先關閉 VM,將其移至目標可用區或區域,然後重新啟動。移動 VM 後,請更新原始資源的所有參照項目,例如指向舊 VM 的任何目標 VM 或目標集區。
在移動期間,某些伺服器產生的 VM 和磁碟屬性會變更。
VM 的屬性變更
下表說明 VM 的屬性變更:
屬性名稱 | 變更 |
---|---|
內部 IP 位址 | 系統通常會指派新的內部 IP 位址,但 VM 可能會保留原始內部 IP 位址。 |
外部 IP 位址 | 如果 VM 是在同一個地區中的不同區域之間移動,外部 IP 位址會維持不變。如果不是,則請為 VM 執行個體選用不同的外部 IP 位址。 |
CPU 平台 | 視目標區域中的可用 CPU 平台而定,虛擬機器在移動後可能會採用不同的 CPU 平台。如需各個區域中的 CPU 平台完整清單,請參閱可用地區和區域一文。 |
網路/子網路 | 如果您的 VM 屬於子網路,且您要跨地區移動 VM,則必須為 VM 挑選新的子網路。在同一個地區中不同區域之間移動的 VM 會保留相同的子網路。 |
磁碟會變更的屬性
下表說明磁碟的屬性變更:
屬性名稱 | 變更 |
---|---|
來源快照 | 系統會將新磁碟的來源快照設定為在移動期間建立的暫時快照。 |
來源快照 ID | 系統會將來源快照 ID 設定為暫時快照的 ID。 |
來源映像檔 | 來源映像檔欄位為空白。 |
映像檔 ID | 映像檔 ID 為空白。 |
上次取消連結的時間戳記 | 上次取消連結的時間戳記為空白。 |
上次連結的時間戳記 | 上次連接的時間戳記會變更為將新磁碟附加至新執行個體時的時間戳記。 |
VM 和磁碟的屬性變更
下表說明 VM 和磁碟的屬性變更:
屬性名稱 | 變更 |
---|---|
ID | 產生新的資源 ID。 |
建立時間戳記 | 產生新的建立時間戳記。 |
區域資源網址 | 所有區域資源網址都會依據目標區域變更。以下列出會發生變更的資源網址:
|
跨可用區或區域移動 VM
如要跨區域或地區移動 VM,您可以執行下列操作:
- 建立來源 VM 的機器映像檔。
- 在其他可用區或區域中,使用機器映像檔建立 VM。
以下範例說明如何在不同區域之間移動 VM。
gcloud
在這個範例中,您會將名為 myinstance
的 VM 從 europe-west1-c
移至 us-west1-b
,該 VM 包含兩個名為 mybootdisk
和 mydatadisk
的永久磁碟。
找出與您要移動的 VM 相關聯的磁碟:
gcloud compute instances describe myinstance --format="list(name,status,disks)"
在這個範例中,您會找到
myinstance
VM 的下列兩個關聯磁碟:- 名為
mybootdisk
的開機磁碟 - 名為
mydatadisk
的資料磁碟
- 名為
將
mybootdisk
和mydatadisk
的自動刪除狀態設為false
,確保系統在刪除 VM 時不會自動刪除這兩個磁碟。gcloud compute instances set-disk-auto-delete myinstance --zone europe-west1-c \ --disk mybootdisk --no-auto-delete
狀態更新之後,
gcloud compute
會傳回Updated [...]
的回應。如果自動刪除狀態已設為「false」,則gcloud compute
會傳回:No change requested; skipping update for [myinstance].
(選用) 儲存 VM 中繼資料。
刪除 VM 時,系統也會一併移除 VM 中繼資料。您可以將該資訊儲存在不同的檔案中,然後將 VM 中繼資料重新套用至新的 VM。
以下列方式說明 VM 中繼資料:
gcloud compute instances describe myinstance --zone europe-west1-c
將內容儲存到不同的檔案中。
使用永久磁碟快照建立資料備份。
為以防萬一,在永久磁碟仍附加至 VM 時,請使用永久磁碟快照建立資料備份。請在建立快照前,遵循快照最佳做法,確保快照與永久磁碟的狀態一致。
清除磁碟緩衝區後,請建立快照:
gcloud compute disks snapshot mybootdisk mydatadisk \ --snapshot-names backup-mybootsnapshot,backup-mydatasnapshot \ --zone europe-west1-c
如要確認快照是否建立完成,請執行
gcloud compute snapshots list
。(選用) 如果您要將 VM 從同一個區域的不同區域移動,且想保留其臨時內部或外部 IP 位址,請將內部或外部 IP 位址提升為靜態 IP 位址,以便日後重複使用。
刪除 VM。
刪除 VM 會將其完全關閉並卸離所有永久磁碟。
gcloud compute instances delete myinstance --zone europe-west1-c
gcloud
會提示您確認刪除:The following VMs are deleted. Any attached disks configured to be auto-deleted are deleted unless they are attached to any other VMs or the `--keep-disks` flag is given and specifies them for keeping. Deleting a disk is irreversible and any data on the disk is lost. — [myinstance] in [europe-west1-c]
Do you want to continue (Y/n)?
由於您先前已在這項程序中關閉磁碟的自動刪除狀態,請輸入 Y 來繼續操作並忽略警告訊息。
接下來,請為開機磁碟和資料磁碟建立另一個快照。
gcloud compute disks snapshot mybootdisk mydatadisk \ --snapshot-names mybootsnapshot,mydatasnapshot \ --zone europe-west1-c
Created [.../mydatasnapshot]. Created [.../mybootsnapshot].
(選用) 刪除永久磁碟。
如果您打算讓新磁碟重複使用永久磁碟的名稱,則必須刪除現有的磁碟以釋出那些名稱。刪除磁碟也可以節省永久磁碟的儲存空間費用。
如果您不打算重複使用相同的磁碟名稱,則不必刪除。
gcloud compute disks delete mybootdisk mydatadisk --zone europe-west1-c
透過您建立的快照,在
us-west1-b
中建立新的永久磁碟。請先建立開機磁碟。gcloud compute disks create mybootdiskb --source-snapshot mybootsnapshot \ --zone us-west1-b
Created [.../mybootdiskb]. NAME ZONE SIZE_GB TYPE STATUS mybootdiskb us-west1-b 100 pd-standard READY
接著,請建立資料磁碟。
gcloud compute disks create mydatadiskb --source-snapshot mydatasnapshot \ --zone us-west1-b
Created [.../mydatadiskb]. NAME ZONE SIZE_GB TYPE STATUS mydatadiskb us-west1-b 4000 pd-standard READY
在
us-west1-b
中重新建立 VM。如果您選擇將 VM 中繼資料儲存在
myinstance.describe
等檔案中,您可以使用該檔案在 VM 中設定相同的中繼資料。如果您的 VM 具有靜態外部 IP 位址,您可以指定
--address [ADDRESS]
選項,將該位址重新指派給新的 VM。如果要跨地區移動 VM,則必須為新的 VM 執行個體挑選不同的外部 IP 位址。如果您的 VM 具有靜態內部 IP 位址,您可以指定
--private-network-ip ADDRESS
選項,將該位址重新指派給新的 VM。如果要跨地區移動 VM,則必須為新的 VM 執行個體挑選不同的內部 IP 位址。如果您的 VM 含有 GPU,請使用
--accelerator
選項將 GPU 新增至 VM。如果 VM 使用特定子網路,請新增
--subnet [SUBNET_NAME]
標記。
如需其他旗標的完整清單,請參閱 gcloud compute instances create。
gcloud compute instances create myinstanceb --machine-type n1-standard-4 \ --zone us-west1-b \ --disk name=mybootdiskb,boot=yes,mode=rw \ --disk name=mydatadiskb,mode=rw
Created [.../myinstanceb]. NAME ZONE MACHINE_TYPE INTERNAL_IP EXTERNAL_IP STATUS myinstanceb us-west1-b n1-standard-4 10.240.173.229 146.148.112.106 RUNNING
(選用) 刪除永久磁碟快照。
確認已移動您的虛擬機器後,即可透過刪除您建立的暫時快照來節省儲存空間費用。
gcloud compute snapshots delete mybootsnapshot mydatasnapshot
如果您不再需要備份快照,請一併將其刪除:
gcloud compute snapshots delete backup-mybootsnapshot backup-mydatasnapshot
Go
取得 VM 的詳細資料,並找出連接至 VM 的磁碟。
將開機磁碟和資料磁碟的自動刪除狀態設為
false
,確保系統在刪除 VM 時不會自動刪除這兩個磁碟。使用永久磁碟快照建立資料備份。
為以防萬一,在永久磁碟仍附加至 VM 時,請使用永久磁碟快照建立資料備份。請在建立快照前,遵循快照最佳做法,確保快照與永久磁碟的狀態一致。
清除磁碟緩衝區後,請建立快照:
從來源區域刪除 VM。
接下來,請為開機磁碟和資料磁碟建立另一個快照。
(選用) 刪除永久磁碟。
如果您打算讓新磁碟重複使用永久磁碟的名稱,則必須刪除現有的磁碟以釋出那些名稱。刪除磁碟也可以節省永久磁碟的儲存空間費用。
如果您不打算重複使用相同的磁碟名稱,則不必刪除。
使用您建立的快照,在目標可用區中建立新的永久磁碟。請先建立開機磁碟,再建立資料磁碟。
在目標可用區中,使用新的磁碟重新建立 VM。
(選用) 刪除臨時磁碟快照。確認虛擬機器已移動後,請刪除您建立的暫時快照,以節省儲存空間費用。
Java
取得 VM 的詳細資料,並找出連接至 VM 的磁碟。
將開機磁碟和資料磁碟的自動刪除狀態設為
false
,確保系統在刪除 VM 時不會自動刪除這兩個磁碟。使用永久磁碟快照建立資料備份。
為以防萬一,在永久磁碟仍附加至 VM 時,請使用永久磁碟快照建立資料備份。請在建立快照前,遵循快照最佳做法,確保快照與永久磁碟的狀態一致。
清除磁碟緩衝區後,請建立快照:
從來源區域刪除 VM。
接下來,請為開機磁碟和資料磁碟建立另一個快照。
(選用) 刪除永久磁碟。
如果您打算讓新磁碟重複使用永久磁碟的名稱,則必須刪除現有的磁碟以釋出那些名稱。刪除磁碟也可以節省永久磁碟的儲存空間費用。
如果您不打算重複使用相同的磁碟名稱,則不必刪除。
使用您建立的快照,在目標可用區中建立新的永久磁碟。請先建立開機磁碟,再建立資料磁碟。
在目標可用區中,使用新的磁碟重新建立 VM。
(選用) 刪除臨時磁碟快照。確認虛擬機器已移動後,請刪除您建立的暫時快照,以節省儲存空間費用。
Node.js
取得 VM 的詳細資料,並找出連接至 VM 的磁碟。
將開機磁碟和資料磁碟的自動刪除狀態設為
false
,確保系統在刪除 VM 時不會自動刪除這兩個磁碟。使用永久磁碟快照建立資料備份。
為以防萬一,在永久磁碟仍附加至 VM 時,請使用永久磁碟快照建立資料備份。請在建立快照前,遵循快照最佳做法,確保快照與永久磁碟的狀態一致。
清除磁碟緩衝區後,請建立快照:
從來源區域刪除 VM。
接下來,請為開機磁碟和資料磁碟建立另一個快照。
(選用) 刪除永久磁碟。
如果您打算讓新磁碟重複使用永久磁碟的名稱,則必須刪除現有的磁碟以釋出那些名稱。刪除磁碟也可以節省永久磁碟的儲存空間費用。
如果您不打算重複使用相同的磁碟名稱,則不必刪除。
使用您建立的快照,在目標可用區中建立新的永久磁碟。請先建立開機磁碟,再建立資料磁碟。
在目標可用區中,使用新的磁碟重新建立 VM。
(選用) 刪除臨時磁碟快照。確認虛擬機器已移動後,請刪除您建立的暫時快照,以節省儲存空間費用。
Python
取得 VM 的詳細資料,並找出連接至 VM 的磁碟。
將開機磁碟和資料磁碟的自動刪除狀態設為
false
,確保系統在刪除 VM 時不會自動刪除這兩個磁碟。使用永久磁碟快照建立資料備份。
為以防萬一,在永久磁碟仍附加至 VM 時,請使用永久磁碟快照建立資料備份。請在建立快照前,遵循快照最佳做法,確保快照與永久磁碟的狀態一致。
清除磁碟緩衝區後,請建立快照:
從來源區域刪除 VM。
接下來,請為開機磁碟和資料磁碟建立另一個快照。
(選用) 刪除永久磁碟。
如果您打算讓新磁碟重複使用永久磁碟的名稱,則必須刪除現有的磁碟以釋出那些名稱。刪除磁碟也可以節省永久磁碟的儲存空間費用。
如果您不打算重複使用相同的磁碟名稱,則不必刪除。
使用您建立的快照,在目標可用區中建立新的永久磁碟。請先建立開機磁碟,再建立資料磁碟。
在目標可用區中,使用新的磁碟重新建立 VM。
(選用) 刪除臨時磁碟快照。確認虛擬機器已移動後,請刪除您建立的暫時快照,以節省儲存空間費用。
後續步驟
- 瞭解如何針對專案變更預設區域/地區。
- 瞭解即時遷移。
- 查看 VM 的狀態。
- 瞭解如何將 VM 遷移至 Compute Engine。