本文件說明如何避免 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.
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.reservations.create
在專案中 -
如要建立執行個體,請按照下列步驟操作:
- 專案的
compute.instances.create
權限 - 如何使用自訂映像檔在映像檔上建立 VM:
compute.images.useReadOnly
- 如何使用快照建立 VM: 在快照上執行
compute.snapshots.useReadOnly
- 如何使用執行個體範本在執行個體範本上建立 VM:
compute.instanceTemplates.useReadOnly
- 如要將舊版網路指派給專案中的 VM:
compute.networks.use
- 如要為專案中的 VM 指定靜態 IP 位址:
compute.addresses.use
- 如要在使用舊版網路時將外部 IP 位址指派給 VM,請在專案中使用
compute.networks.useExternalIp
- 如要為 VM 指定子網路,請在專案或所選子網路中使用
compute.subnetworks.use
- 如要在使用虛擬私有雲網路時將外部 IP 位址指派給 VM,請在專案或所選子網路上設定
compute.subnetworks.useExternalIp
- 如何為專案中的 VM:
compute.instances.setMetadata
設定 VM 執行個體中繼資料 - 如何為 VM 設定標記:
compute.instances.setTags
- 如何為 VM 設定標籤:
compute.instances.setLabels
- 如何設定 VM 要使用的服務帳戶: 在 VM 上使用
compute.instances.setServiceAccount
- 如要為專案中的 VM
compute.disks.create
建立新磁碟 - 如要以唯讀或讀寫模式連接現有磁碟:磁碟的
compute.disks.use
- 如要以唯讀模式連接現有磁碟:磁碟上的
compute.disks.useReadOnly
- 專案的
-
如要建立執行個體範本:
compute.instanceTemplates.create
在專案上
避免預留項目消耗
如要避免運算執行個體使用保留項目,請將其保留項目相依性 (reservationAffinity
) 屬性設為不使用保留項目。這個屬性可控制執行個體是否可以使用相符的保留項目、特定保留項目或不使用任何保留項目。
如要避免一或多個執行個體使用保留項目,請使用下列其中一種方法:
防止現有執行個體中的使用量
您可以更新執行中的執行個體,讓該執行個體不再自動使用預留空間。如本節所述,您必須重新啟動執行個體,變更才會生效。
如要避免現有執行個體使用保留項目,請選取下列其中一個選項:
gcloud
建立空的 YAML 檔案。
如要將執行個體的屬性匯出至您剛建立的 YAML 檔案,請使用
gcloud compute instances export
指令:gcloud compute instances export INSTANCE_NAME \ --destination=YAML_FILE \ --zone=ZONE
更改下列內容:
INSTANCE_NAME
:執行個體的名稱。YAML_FILE
:您在先前步驟中建立的空白 YAML 檔案路徑。ZONE
:執行個體所在的區域。
在 YAML 設定檔中,將
consumeReservationType
設為NO_RESERVATION
:reservationAffinity: consumeReservationType: NO_RESERVATION
如要更新執行個體並重新啟動,請使用
gcloud compute instances update-from-file
指令,並將--most-disruptive-allowed-action
旗標設為RESTART
:gcloud compute instances update-from-file INSTANCE_NAME \ --most-disruptive-allowed-action=RESTART \ --source=YAML_FILE \ --zone=ZONE
更改下列內容:
INSTANCE_NAME
:執行個體的名稱。YAML_FILE
:YAML 檔案的路徑,其中包含您在上一個步驟中修改的設定資料。ZONE
:執行個體所在的區域。
REST
如要查看現有例項的屬性,請對
instances.get
方法發出GET
要求:GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME
更改下列內容:
PROJECT_ID
:您建立執行個體的專案 ID。ZONE
:執行個體所在的區域。INSTANCE_NAME
:執行個體的名稱。
將
GET
要求的輸出內容儲存在檔案或文字編輯器中。修改複製的輸出內容,將consumeReservationType
欄位變更為NO_RESERVATION
:{ ... "reservationAffinity": { "consumeReservationType": "NO_RESERVATION" }, ... }
如要更新執行個體並重新啟動,請對
instances.update
方法發出PUT
要求。在要求中執行以下操作:在要求網址中,將
mostDisruptiveAllowedAction
查詢參數設為RESTART
。針對要求主體,請使用先前步驟中編輯的
GET
要求輸出內容。
要求會類似以下示例:
PUT https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-central-1/instances/instance-01?mostDisruptiveAllowedAction=RESTART { ... "reservationAffinity": { "consumeReservationType": "NO_RESERVATION" }, ... }
如要進一步瞭解如何更新執行個體,請參閱「更新執行個體屬性」。
建立執行個體時避免使用
如要建立無法使用保留項目的運算執行個體,請選取下列任一選項:
主控台
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
在「名稱」欄位中輸入執行個體的名稱。
在「Region」(區域) 和「Zone」(可用區) 清單中,選取要建立執行個體的區域和可用區。
指定用於執行個體的機器類型。
在導覽選單中,按一下「進階」。
在「Reservations」部分,選取「Don't use a reservation」。
按一下 [建立]。
gcloud
如要建立無法使用保留空間的執行個體,請使用 gcloud compute instances create
指令,並將 --reservation-affinity
旗標設為 none
:
gcloud compute instances create INSTANCE_NAME \
--machine-type=MACHINE_TYPE \
--reservation-affinity=none \
--zone=ZONE
更改下列內容:
INSTANCE_NAME
:執行個體的名稱。MACHINE_TYPE
:要用於執行個體的機器類型。ZONE
:建立執行個體的區域。
Go
如要建立無法使用保留項目的執行個體,請使用下列程式碼範例:
Java
如要建立無法使用保留項目的執行個體,請使用下列程式碼範例:
Node.js
如要建立無法使用保留項目的執行個體,請使用下列程式碼範例:
Python
如要建立無法使用保留項目的執行個體,請使用下列程式碼範例:
REST
如要建立無法使用保留項目的執行個體,請對 instances.insert
方法發出 POST
要求。在要求主體中加入 consumeReservationType
欄位,並將其設為 NO_RESERVATION
:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances
{
"name": "INSTANCE_NAME",
"machineType": "zones/ZONE/machineTypes/MACHINE_TYPE",
"disks": [
{
"boot": true,
"initializeParams": {
"sourceImage": "projects/IMAGE_PROJECT/global/images/IMAGE"
}
}
],
"networkInterfaces": [
{
"network": "global/networks/default"
}
],
"reservationAffinity": {
"consumeReservationType": "NO_RESERVATION"
}
}
更改下列內容:
PROJECT_ID
:您要建立執行個體的專案 ID。ZONE
:建立執行個體的區域。INSTANCE_NAME
:執行個體的名稱。MACHINE_TYPE
:要用於執行個體的機器類型。IMAGE_PROJECT
:包含 OS 映像檔的映像檔專案,例如debian-cloud
。如要進一步瞭解支援的圖像專案,請參閱「公開圖像」。IMAGE
:指定下列其中一個值:
如要進一步瞭解如何建立執行個體,請參閱「建立及啟動 Compute Engine 執行個體」。
在大量建立執行個體時避免使用
如要大量建立無法使用保留項目的運算資源執行個體,請選取下列任一選項:
gcloud
如要建立無法使用保留項目的大量執行個體,請使用 gcloud compute instances bulk create
指令,並將 --reservation-affinity
旗標設為 none
。
舉例來說,如要在單一可用區中大量建立執行個體並指定名稱模式,請執行下列指令:
gcloud compute instances bulk create \
--count=COUNT \
--machine-type=MACHINE_TYPE \
--name-pattern="NAME_PATTERN" \
--reservation-affinity=none \
--zone=ZONE
更改下列內容:
COUNT
:要建立的執行個體數量。MACHINE_TYPE
:執行個體使用的機器類型。NAME_PATTERN
:執行個體的名稱模式。如要取代執行個體名稱中的數字序列,請使用一連串井字號 (#
) 字元。舉例來說,如果將instance-#
用於名稱模式,系統會產生名稱開頭為instance-1
、instance-2
的執行個體,並持續產生COUNT
指定的執行個體數量。ZONE
:要用於大量建立執行個體的區域。
REST
如要大量建立不會使用保留項目的執行個體,請對 instances.bulkInsert
方法發出 POST
要求。在要求主體中加入 consumeReservationType
欄位,並將其設為 NO_RESERVATION
。
舉例來說,如要在單一區域中大量建立執行個體並指定名稱模式,請提出以下要求:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/bulkInsert
{
"count": COUNT,
"namePattern": "NAME_PATTERN",
"instanceProperties": {
"machineType": "MACHINE_TYPE",
"disks": [
{
"boot": true,
"initializeParams": {
"sourceImage": "projects/IMAGE_PROJECT/global/images/IMAGE"
}
}
],
"networkInterfaces": [
{
"network": "global/networks/default"
}
],
"reservationAffinity": {
"consumeReservationType": "NO_RESERVATION"
}
}
}
更改下列內容:
PROJECT_ID
:要用來大量建立執行個體的專案 ID。ZONE
:要用於大量建立執行個體的區域。COUNT
:要建立的執行個體數量。NAME_PATTERN
:執行個體的名稱模式。如要取代執行個體名稱中的數字序列,請使用一連串井字號 (#
) 字元。舉例來說,如果將instance-#
用於名稱模式,系統會產生名稱開頭為instance-1
、instance-2
的執行個體,並持續產生COUNT
指定的執行個體數量。MACHINE_TYPE
:執行個體使用的機器類型。IMAGE_PROJECT
:包含 OS 映像檔的映像檔專案,例如debian-cloud
。如要進一步瞭解支援的圖像專案,請參閱「公開圖像」。IMAGE
:指定下列其中一個值:
如要進一步瞭解如何大量建立執行個體,請參閱「大量建立 VM」。
建立執行個體範本時避免消耗
建立執行個體範本,並將執行個體設為不使用預留項目後,您可以使用該範本執行下列操作:
避免代管執行個體群組 (MIG) 中的運算執行個體在執行下列操作時使用預留容量:
如要建立執行個體範本,並設定執行個體不使用預留項目,請選取下列任一選項:
主控台
在 Google Cloud 控制台中,前往「Create an instance template」(建立執行個體範本) 頁面。
在「Name」欄位中,輸入執行個體範本的名稱。
在「Location」部分中,指定要建立區域 (預設) 或全球執行個體範本。
在「Machine configuration」(機器設定) 區段中,指定要用於使用範本建立的執行個體的機器類型。
展開「進階選項」部分,然後執行下列操作:
展開「管理」部分。
在「Reservations」部分,選取「Don't use a reservation」。
按一下 [建立]。
gcloud
如要建立執行個體範本,並設定執行個體不使用保留項目資源,請使用 gcloud compute instances-templates create
指令,並將 --reservation-affinity
旗標設為 none
。
如要建立地區執行個體範本,並設定執行個體不使用預留容量,請執行下列指令。如果您要建立全域執行個體範本,請使用相同的指令,但不含 --instance-template-region
標記。
gcloud compute instance-templates create INSTANCE_TEMPLATE_NAME \
--instance-template-region=REGION \
--machine-type=MACHINE_TYPE \
--reservation-affinity=none
更改下列內容:
INSTANCE_TEMPLATE_NAME
:執行個體範本的名稱。REGION
:要建立執行個體範本的區域。MACHINE_TYPE
:使用執行個體範本建立執行個體時使用的機器類型。
Go
如要建立執行個體範本,並將執行個體設為不使用保留值,請使用下列程式碼範例:
Java
如要建立執行個體範本,並將執行個體設為不使用保留值,請使用下列程式碼範例:
Node.js
如要建立執行個體範本,並將執行個體設為不使用保留值,請使用下列程式碼範例:
Python
如要建立執行個體範本,並將執行個體設為不使用保留值,請使用下列程式碼範例:
REST
如要建立執行個體範本,將執行個體設為不使用保留項目,請向下列任一方法提出 POST
要求:
如要建立全域執行個體範本,請使用
instanceTemplates.insert
方法。如要建立區域執行個體範本,請使用
regionInstanceTemplates.insert
方法。
在要求主體中加入 consumeReservationType
欄位,並將其設為 NO_RESERVATION
。
舉例來說,如要建立區域執行個體範本,並指定不使用保留空間,請提出以下要求:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/InstanceTemplates
{
"name": "INSTANCE_TEMPLATE_NAME",
"properties": {
"machineType": "MACHINE_TYPE",
"disks": [
{
"boot": true,
"initializeParams": {
"sourceImage": "projects/IMAGE_PROJECT/global/images/IMAGE"
}
}
],
"networkInterfaces": [
{
"network": "global/networks/default"
}
],
"reservationAffinity": {
"consumeReservationType": "NO_RESERVATION"
}
}
}
更改下列內容:
PROJECT_ID
:建立執行個體範本的專案 ID。INSTANCE_TEMPLATE_NAME
:執行個體範本的名稱。MACHINE_TYPE
:使用執行個體範本建立執行個體時使用的機器類型。IMAGE_PROJECT
:包含 OS 映像檔的映像檔專案,例如debian-cloud
。如要進一步瞭解支援的圖像專案,請參閱「公開圖像」。IMAGE
:指定下列其中一個值:
如要進一步瞭解如何建立執行個體範本,請參閱「建立執行個體範本」。