本文說明如何防止 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.
-
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.
-
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.
-
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.
-
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.
-
如要建立預留項目:
專案的
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 設定 VM 執行個體中繼資料,請執行下列指令:
compute.instances.setMetadata
- 如要為 VM 設定標記,請在 VM 上按一下
compute.instances.setTags
- 如要為 VM 設定標籤,請在 VM 上執行下列步驟:
compute.instances.setLabels
- 如要設定 VM 使用的服務帳戶,請在 VM 上執行
compute.instances.setServiceAccount
- 如要為 VM 建立新磁碟,請取得專案的
compute.disks.create
權限 - 如要以唯讀或讀寫模式連接現有磁碟,請對磁碟擁有
compute.disks.use
權限 - 如要以唯讀模式連接現有磁碟,請對磁碟擁有
compute.disks.useReadOnly
權限
- 專案的
-
如要建立執行個體範本:
按一下專案上的
compute.instanceTemplates.create
建立空白 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
:執行個體所在的區域。
如要查看現有執行個體的屬性,請對
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
要求。在要求中執行下列操作:在要求網址中,加入設為
RESTART
的mostDisruptiveAllowedAction
查詢參數。要求主體請使用您在上一個步驟中編輯的
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」(建立執行個體) 頁面。
在「Name」(名稱) 欄位中,輸入執行個體的名稱。
在「Region」(區域) 和「Zone」(可用區) 清單中,選取要建立執行個體的區域和可用區。
指定執行個體要使用的機器類型。
在導覽選單中,按一下「進階」。
在「預留項目」部分,選取「不使用預留項目」。
點選「建立」。
INSTANCE_NAME
:執行個體的名稱。MACHINE_TYPE
:執行個體使用的機器類型。ZONE
:要建立執行個體的區域。PROJECT_ID
:要在其中建立執行個體的專案 ID。ZONE
:要建立執行個體的區域。INSTANCE_NAME
:執行個體的名稱。MACHINE_TYPE
:執行個體使用的機器類型。IMAGE_PROJECT
:包含 OS 映像檔的映像檔專案,例如debian-cloud
。如要進一步瞭解支援的映像檔專案,請參閱「公開映像檔」。IMAGE
:指定下列其中一項:COUNT
:要建立的執行個體數量。MACHINE_TYPE
:執行個體使用的機器類型。NAME_PATTERN
:執行個體的名稱模式。如要取代執行個體名稱中的一連串數字,請使用一連串井字號 (#
) 字元。舉例來說,使用instance-#
做為名稱模式會產生執行個體,名稱開頭為instance-1
、instance-2
,並持續到COUNT
指定的執行個體數量。ZONE
:要大量建立執行個體的區域。PROJECT_ID
:要大量建立執行個體的專案 ID。ZONE
:要大量建立執行個體的區域。COUNT
:要建立的執行個體數量。NAME_PATTERN
:執行個體的名稱模式。如要取代執行個體名稱中的一連串數字,請使用一連串井字號 (#
) 字元。舉例來說,使用instance-#
做為名稱模式會產生執行個體,名稱開頭為instance-1
、instance-2
,並持續到COUNT
指定的執行個體數量。MACHINE_TYPE
:執行個體使用的機器類型。IMAGE_PROJECT
:包含 OS 映像檔的映像檔專案,例如debian-cloud
。如要進一步瞭解支援的映像檔專案,請參閱「公開映像檔」。IMAGE
:指定下列其中一項:執行下列操作時,可防止代管執行個體群組 (MIG) 中的運算執行個體耗用預訂資源:
前往 Google Cloud 控制台的「Create an instance template」(建立執行個體範本) 頁面。
在「Name」(名稱) 欄位中,輸入執行個體範本的名稱。
在「Location」(位置) 部分中,指定要建立區域 (預設) 或全域執行個體範本。
在「Machine configuration」(機器設定) 區段中,指定要用於透過範本建立執行個體的機器類型。
展開「Advanced options」(進階選項) 區段,然後執行下列操作:
展開「管理」部分。
在「預留項目」部分,選取「不使用預留項目」。
點選「建立」。
INSTANCE_TEMPLATE_NAME
:執行個體範本的名稱。REGION
:要建立執行個體範本的區域。MACHINE_TYPE
:使用執行個體範本建立執行個體時所用的機器類型。如要建立全域執行個體範本,請使用
instanceTemplates.insert
方法。如要建立區域執行個體範本,請使用
regionInstanceTemplates.insert
方法。PROJECT_ID
:要在其中建立執行個體範本的專案 ID。INSTANCE_TEMPLATE_NAME
:執行個體範本的名稱。MACHINE_TYPE
:使用執行個體範本建立執行個體時所用的機器類型。IMAGE_PROJECT
:包含 OS 映像檔的映像檔專案,例如debian-cloud
。如要進一步瞭解支援的映像檔專案,請參閱「公開映像檔」。IMAGE
:指定下列其中一項:
Go
如要在本機開發環境中使用本頁的 Go 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
Java
如要在本機開發環境中使用本頁的 Java 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
Node.js
如要在本機開發環境中使用本頁的 Node.js 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
Python
如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 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.
詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。
必要的角色
如要取得權限,防止 Compute 執行個體耗用預留資源,請要求管理員授予您專案的 Compute 執行個體管理員 (v1) (
roles/compute.instanceAdmin.v1
) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。這個預先定義的角色具備必要權限,可防止運算執行個體耗用預訂資源。如要查看確切的必要權限,請展開「必要權限」部分:
所需權限
如要防止運算執行個體使用預留項目,必須具備下列權限:
禁止使用預留項目
如要防止運算執行個體使用保留項目,請將其保留項目親和性 (
reservationAffinity
) 屬性設為不使用保留項目。這項屬性可控制執行個體是否能使用相符的保留項目、特定保留項目或不使用保留項目。如要避免一或多個執行個體使用預訂項目,請使用下列其中一種方法:
防止現有執行個體耗用配額
您可以更新執行中的執行個體,使其不再自動消耗預訂項目。如本節所述,您必須重新啟動執行個體,變更才會生效。
如要防止現有執行個體使用預留項目,請選取下列其中一個選項:
gcloud
REST
如要進一步瞭解如何更新執行個體,請參閱更新執行個體屬性。
建立執行個體時避免耗用資源
如要建立無法使用保留項目的運算執行個體,請選取下列任一選項:
主控台
gcloud
如要建立無法使用預留資源的執行個體,請使用
gcloud compute instances create
指令,並將--reservation-affinity
旗標設為none
:gcloud compute instances create INSTANCE_NAME \ --machine-type=MACHINE_TYPE \ --reservation-affinity=none \ --zone=ZONE
更改下列內容:
Go
如要建立無法使用預留項目的執行個體,請使用下列程式碼範例:
Java
如要建立無法使用預留項目的執行個體,請使用下列程式碼範例:
Node.js
如要建立無法使用預留項目的執行個體,請使用下列程式碼範例:
Python
如要建立無法使用預留項目的執行個體,請使用下列程式碼範例:
REST
如要建立無法使用保留項目的執行個體,請對
instances.insert
方法發出POST
要求。在要求主體中,加入設為NO_RESERVATION
的consumeReservationType
欄位: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" } }
更改下列內容:
如要進一步瞭解如何建立執行個體,請參閱建立及啟動 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
更改下列內容:
REST
如要大量建立無法使用保留項目的執行個體,請對
instances.bulkInsert
方法發出POST
要求。在要求主體中,加入設為NO_RESERVATION
的consumeReservationType
欄位。舉例來說,如要在單一可用區中大量建立執行個體,並指定名稱模式,請發出下列要求:
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" } } }
更改下列內容:
如要進一步瞭解如何大量建立執行個體,請參閱大量建立 VM。
建立執行個體範本時避免耗用配額
建立執行個體範本,將執行個體設定為不使用預留項目後,您可以使用該範本執行下列操作:
如要建立執行個體範本,將執行個體設定為不使用預留項目,請選取下列任一選項:
主控台
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
更改下列內容:
Go
如要建立執行個體範本,將執行個體設定為不使用預留資源,請使用下列程式碼範例:
Java
如要建立執行個體範本,將執行個體設定為不使用預留資源,請使用下列程式碼範例:
Node.js
如要建立執行個體範本,將執行個體設定為不使用預留資源,請使用下列程式碼範例:
Python
如要建立執行個體範本,將執行個體設定為不使用預留資源,請使用下列程式碼範例:
REST
如要建立執行個體範本,將執行個體設定為不使用預訂項目,請對下列其中一種方法發出
POST
要求:在要求主體中加入
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" } } }
更改下列內容:
如要進一步瞭解如何建立執行個體範本,請參閱建立執行個體範本。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-08 (世界標準時間)。
-