本頁面說明如何使用執行個體範本建立 VM 執行個體。執行個體範本是一種 API 資源,可定義 VM 執行個體的屬性。您可以在執行個體範本中定義機器類型、OS 映像檔、永久磁碟設定、中繼資料、開機指令碼等屬性,然後使用執行個體範本建立個別 VM 執行個體或代管執行個體群組。
利用執行個體範本建立 VM 執行個體時,預設行為會建立 VM 執行個體,其屬性與範本中指定的屬性相同,但 VM 執行個體名稱和執行個體運作時所屬的區域例外。此外,如果您要變更執行個體範本的某些屬性以做特定用途,也可以選擇在建立執行個體期間覆寫特定欄位。
本文假定執行個體範本已準備就緒。如果您還沒有執行個體範本,請按照操作說明建立新執行個體範本。
事前準備
- 參閱執行個體範本說明文件。
- 建立執行個體範本。
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 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.
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.
-
利用執行個體範本建立 VM 執行個體
您可以使用區域或全域執行個體範本建立 VM 執行個體。如要完全依照執行個體範本說明所述建立執行個體,請依照下列操作說明操作。
主控台
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
在
「Create VM from...」選單中,選取「Instance templates」。在隨即顯示的「Create VM from template」視窗中,執行下列操作:
選取範本。
如要建立並啟動 VM,請按一下 [Create] (建立)。
gcloud
如要從區域或全球執行個體範本建立 VM,請使用與建立一般執行個體相同的 gcloud compute instances create
指令,但要新增 --source-instance-template
標記:
gcloud compute instances create VM_NAME \ --source-instance-template INSTANCE_TEMPLATE_NAME
更改下列內容:
VM_NAME
:執行個體的名稱。INSTANCE_TEMPLATE_NAME
:要使用的執行個體範本名稱。如果是地區執行個體範本,您必須指定範本的完整或部分網址。完整網址的範例為https://www.googleapis.com/compute/v1/projects/example-project/regions/us-central1/instanceTemplates/example-regional-instance-template
,部分網址的範例為projects/example-project/regions/us-central1/instanceTemplates/example-regional-instance-template
。
例如:
gcloud compute instances create example-instance \ --source-instance-template my-instance-template
Go
Java
Node.js
Python
REST
如要利用區域或全球執行個體範本建立 VM,請建構用以建立執行個體的一般要求,但要包含 sourceInstanceTemplate
查詢參數,且後面要加上執行個體範本的正確路徑。
POST https://compute.googleapis.com/compute/v1/projects/ PROJECT_ID/zones/ZONE/ instances?sourceInstanceTemplate=INSTANCE_TEMPLATE_NAME
在要求主體中提供 VM 執行個體的 name
:
{ "name": "example-instance" }
例如,下列程式碼片段包含了範本的完整路徑:https://compute.googleapis.com/compute/v1/projects/myproject/global/instanceTemplates/example-instance-template
。
POST https://compute.googleapis.com/ compute/v1/projects/myproject/zones/us-central1-a/instances?sourceInstanceTemplate= https://compute.googleapis.com/compute/v1/projects/myproject/global/ instanceTemplates/example-instance-template
{ "name": "example-instance" }
利用執行個體範本與覆寫功能建立 VM 執行個體
使用執行個體範本啟動 VM 執行個體時,預設行為會完全依照執行個體範本說明所述建立 VM 執行個體,但執行個體名稱和區域例外。
如果您想主要根據執行個體範本建立執行個體,但稍做一些變更,可使用覆寫行為。如要使用覆寫行為,您可在建立執行個體時傳入屬性,以覆寫現有執行個體範本。
主控台
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
在
「Create VM from...」選單中,選取「Instance templates」。在隨即顯示的「Create VM from template」視窗中選取範本,然後按一下
「Customize」。選用:指定其他設定選項。詳情請參閱「建立執行個體時的設定選項」。
如要建立並啟動執行個體,請按一下「建立」。
gcloud
使用 gcloud CLI 提出要求,以 --source-instance-template
旗標建立執行個體,然後以適當的 gcloud
旗標覆寫所需的任何屬性。如要查看適用標記清單,請參閱 gcloud
參考資料。
例如,提供下列標記,即可覆寫執行個體範本的機器類型、中繼資料、作業系統、永久磁碟開機磁碟和次要磁碟:
gcloud compute instances create example-instance \ --source-instance-template example-instance --machine-type e2-standard-2 \ --image-family debian-9 --image-project debian-cloud \ --metadata bread=butter --disk=boot=no,name=my-override-disk
Go
Java
Node.js
Python
REST
在 API 中,在建構用以建立執行個體的一般要求中,使用 sourceInstanceTemplate
查詢參數,並提供在要求主體中要覆寫的任何欄位。
API 中的覆寫行為遵守 JSON 合併修補規則,如 RFC 7396 所述。
具體情況如下:
- 若覆寫基本欄位,則執行個體範本中對應的基本欄位,會替換成要求中指定的基本欄位值。基本欄位包括
machineType
、sourceImage
、name
等。 - 若覆寫重複欄位,該屬性的所有重複值都將替換成要求中提供的對應值。重複欄位通常是類型為
list
的屬性。例如,disks
和networkInterfaces
是重複欄位。 - 若要覆寫
nested object
,則執行個體範本中的物件會與要求中的對應物件規格合併。請注意,如果巢狀物件存在於重複欄位之中,系統會依據重複欄位的規則處理欄位。此規則唯一的例外是標籤,即使標籤屬於object
類型,也會將其視為重複欄位。
例如,假設您的執行個體範本有兩個非開機磁碟,而您想覆寫其中一個磁碟。您必須在要求中提供完整的 disks
規格,包括要保留的所有磁碟。
此要求的網址為:
POST https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances? sourceInstanceTemplate=https://compute.googleapis.com/compute/v1/projects/myproject/global/instanceTemplates/example-instance-template
要求主體:
{ "disks": [ { # Since you are overriding the repeated disk property, you must # specify a boot disk in the request, even if it is already # specified in the instance template "autoDelete": true, "boot": true, "initializeParams": { "sourceImage": "projects/debian-cloud/global/images/family/debian-8" }, "mode": "READ_WRITE", "type": "PERSISTENT" }, { # New disk you want to use "autoDelete": false, "boot": false, "mode": "READ_WRITE", "source": "zones/us-central1-f/disks/my-override-disk", "type": "PERSISTENT" }, { # Assume this disk is already specified in instance template, but # you must specify it again since you are overriding the disks # property "autoDelete": false, "boot": false, "mode": "READ_WRITE", "source": "zones/us-central1-f/disks/my-other-disk-to-keep", "type": "PERSISTENT" } ], "machineType": "zones/us-central1-f/machineTypes/e2-standard-2", "name": "example-instance" }
後續步驟
- 參閱先佔 VM 執行個體說明文件。
- 瞭解關閉指令碼。
- 參閱先占執行個體定價。
- 連線至執行個體。