本文說明如何在 Compute Engine 中使用預留容量。如要瞭解如何在其他 Google Cloud 產品中使用預訂功能,請參閱下列說明文件:
建立預留項目後,或 Compute Engine 自動為您建立預留項目以便日後使用,Compute Engine 就會為您保留所需的資源。接著,您可以使用這些已保留的資源,建立符合預留項目屬性的 Compute Engine 執行個體。這項操作稱為「使用預訂」。您可以使用預留容量建立執行個體,直到預留容量完全用盡為止。
限制
您無法使用預留項目建立下列 Compute Engine 資源:
Spot 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.
-
必要的角色
如要取得使用預留主機所需的權限,請要求管理員為您授予專案的 Compute 執行個體管理員 (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
在專案上
使用預留項目
以下各節的範例說明如何建立單一運算單元,以便使用預留容量。您也可以使用不同的部署選項建立符合保留項目屬性的執行個體,或是更新現有執行個體的屬性,以便與自動使用的保留項目相符。
如要使用預留項目,請使用下列其中一種方法:
如要建立會使用自動保留項目的執行個體,無論該保留項目的建立方式為何,請參閱「使用自動使用的保留項目」。
如要建立會使用您建立的特定保留項目的執行個體,請參閱「使用指定目標的保留項目」一文。
使用自動使用的預留項目
建立自動使用的保留項目時,符合保留項目屬性的運算執行個體會自動使用該保留項目。這項用量行為適用於新執行和現有執行的執行個體。當您建立預留項目,或 Compute Engine 自動建立預留項目以滿足未來預留項目時,預留項目類型會是預設設定。
如果單一專案自動保留項目和共用自動保留項目的屬性相符,專案中的執行個體會先使用單一專案保留項目,然後再使用共用保留項目。詳情請參閱預留項目的消耗順序。
如要建立及使用自動預留項目範例,請選取下列任一選項:
主控台
以下範例說明如何在區域 us-central1-a
中為三個 N2 執行個體建立自動保留項目,其中包含 32 個 vCPU,並將 Intel Cascade Lake 做為最低 CPU 平台。並說明如何建立單一執行個體來使用保留項目。
如要建立自動預留項目範例並加以使用,請按照下列步驟操作:
如要建立範例預訂,請完成下列步驟:
在 Google Cloud 控制台,前往「Reservations」(預留項目) 頁面。
在「隨需預留項目」分頁 (預設) 中,按一下
「Create reservation」。「Create a reservation」(建立預留項目) 頁面隨即顯示。在「名稱」欄位中,輸入預訂名稱。例如輸入
reservation-01
。選取要保留資源的「Region」(區域)和「Zone」(可用區)。例如分別選取「us-central1」和「us-central1-a」。
在「分享類型」專區,執行下列任一操作:
如要建立單一專案的預留項目,請選取「本機」。
如要建立共用保留項目,請選取「共用」,然後指定要共用保留項目的專案。
在「Use with VM instance」(搭配 VM 執行個體使用) 區段中,選取「Use reservation automatically」(自動使用保留項目)(如果尚未選取)。
在「Number of VM instances」(VM 執行個體數量) 欄位中輸入
3
。在「一般用途」分頁中,選取「N2」。
在「Machine type」部分的「Preset」分頁中 (預設),選取「n2-standard-32」。
展開「CPU 平台和 GPU」,然後在「CPU 平台」欄位中選取「Intel Cascade Lake 或以上版本」。
按一下 [建立]。
如要建立會使用示例保留項目的執行個體,請完成下列步驟:
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
前往「Create an instance」(建立執行個體)
系統隨即會顯示「Create an instance」(建立執行個體) 頁面,並顯示「Machine configuration」(機器設定) 窗格。
在「Machine configuration」(機器設定) 窗格中,執行下列操作:
在「名稱」欄位中輸入執行個體的名稱。在這個範例中,請輸入
instance-01
。指定要預留資源的「Region」(區域) 和「Zone」(可用區)。在本範例中,請分別選取 us-central1 和 us-central1-a。
在「一般用途」分頁中,選取「N2」。
在「Machine type」部分的「Preset」分頁中 (預設),選取「n2-standard-32」。
展開「進階選項」部分,然後在「CPU 平台」欄位中選取「Intel Cascade Lake 或更新版本」。
在導覽選單中,按一下「進階」。
在「進階」窗格中的「預訂」部分,選取「使用自動選取」(如果尚未選取)。
按一下 [建立]。
gcloud
以下範例說明如何在區域 us-central1-a
中為三個 N2 執行個體建立自動保留項目,其中包含 32 個 vCPU,並將 Intel Cascade Lake 做為最低 CPU 平台。並說明如何建立單一執行個體來使用保留項目。
如要建立自動預留項目範例並加以使用,請按照下列步驟操作:
如要建立範例預訂,請使用
gcloud compute reservations create
指令:gcloud compute reservations create reservation-01 \ --machine-type=n2-standard-32 \ --min-cpu-platform="Intel Cascade Lake" \ --vm-count=3 \ --zone=us-central1-a
如要建立會使用範例保留項目的執行個體,請使用
gcloud compute instances create
指令,並將--reservation-affinity
旗標設為any
。由於any
是預設設定,您也可以省略這個標記。gcloud compute instances create instance-01 \ --machine-type=n2-standard-32 \ --min-cpu-platform="Intel Cascade Lake" \ --reservation-affinity=any \ --zone=us-central1-a
Go
如要使用執行個體範本建立自動預留資源範例,並使用相同範本建立要使用預留資源的執行個體,請使用下列程式碼範例:
Java
以下範例說明如何在區域 us-central1-a
中,建立具有四個 vCPU 的 N1 執行個體,並將 Intel Skylake 做為最低 CPU 平台。執行個體會自動使用相符的預留項目。
如要建立範例例項,請使用以下程式碼範例:
Node.js
以下範例說明如何在區域 us-central1-a
中,建立具有四個 vCPU 和 Intel Skylake 做為最低 CPU 平台的 N1 執行個體。執行個體會自動使用相符的預留項目。
如要建立範例例項,請使用以下程式碼範例:
Python
以下範例說明如何在區域 us-central1-a
中為三個 N1 執行個體建立自動保留項目,每個執行個體有一個 vCPU,Intel Ivy 做為最低 CPU 平台。並說明如何建立單一執行個體來使用預留項目。
如要建立自動預留項目範例並加以使用,請按照下列步驟操作:
REST
以下範例說明如何在區域 us-central1-a
中為三個 N2 執行個體建立自動保留項目,其中包含 32 個 vCPU,並將 Intel Cascade Lake 做為最低 CPU 平台。並說明如何建立單一執行個體來使用保留項目。
如要建立自動預留項目範例並加以使用,請按照下列步驟操作:
如要建立範例預留項目,請對
reservations.insert
方法發出POST
要求:POST https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-central1-a/reservations { "name": "reservation-01", "specificReservation": { "count": "3", "instanceProperties": { "machineType": "n2-standard-32", "minCpuPlatform": "Intel Cascade Lake", } } }
如要建立會使用範例預留項目的執行個體,請對
instances.insert
方法發出POST
要求。在要求主體中加入consumeReservationType
欄位,並將其設為ANY_RESERVATION
。不過,由於ANY_RESERVATION
是預設設定,您也可以省略該欄位。POST https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-central1-a/instances { "name": "instance-01", "machineType": "zones/us-central1-a/machineTypes/n2-standard-32", "minCpuPlatform": "Intel Cascade Lake", "disks": [ { "boot": true, "initializeParams": { "sourceImage": "projects/debian-cloud/global/images/family/debian-12" } } ], "networkInterfaces": [ { "network": "global/networks/default" } ], "reservationAffinity": { "consumeReservationType": "ANY_RESERVATION" } }
使用特定指定的預留項目
明確指定的保留項目可讓新運算執行個體只在指定保留項目時使用相符的保留項目。只有在執行個體的屬性與保留項目屬性相符時,才能成功建立執行個體。否則會發生錯誤。
根據您要使用的建立方法,建立特定保留項目和指定這些保留項目的執行個體,如下所示:
建立方式 | 建立預留項目時 | 建立執行個體時 |
---|---|---|
Google Cloud 控制台 | 在「Use with VM instance」(搭配 VM 執行個體使用) 部分,選取「Select specific reservation」(選取特定保留項目)。 | 在「進階」窗格中的「預訂」部分,選取「選擇預訂」。 |
Google Cloud CLI | 加上 --require-specific-reservation 標記。 |
請加入下列旗標:
|
Go | 將 SpecificReservationRequired 欄位設為 true 。 |
在 ReservationAffinity 欄位中加入下列欄位:
|
Java | 將 setSpecificReservationRequired 欄位設為 true 。 |
在 ReservationAffinity 欄位中加入下列欄位:
|
Node.js 和 REST API | 將 specificReservationRequired 欄位設為 true 。 |
在 reservationAffinity 欄位中加入下列欄位:
|
Python 和 Terraform | 將 specific_reservation_required 欄位設為 true 。 |
在 reservation_affinity 欄位中加入下列欄位:
|
如要建立示例特定保留項目和要使用該項目的執行個體,請選取下列任一選項:
主控台
以下範例說明如何在區域 us-central1-a
中為三個 N2 執行個體建立特定保留項目,其中包含 32 個 vCPU,並將 Intel Cascade Lake 做為最低 CPU 平台。並說明如何建立單一執行個體來使用保留項目。
如要建立示例特定保留項目並加以使用,請執行下列步驟:
如要建立範例預訂,請完成下列步驟:
在 Google Cloud 控制台,前往「Reservations」(預留項目) 頁面。
在「隨需預留項目」分頁 (預設) 中,按一下
「Create reservation」。「Create a reservation」(建立預留項目) 頁面隨即顯示。在「名稱」欄位中,輸入預訂名稱。例如輸入
reservation-02
。指定要預留資源的「區域」和「可用區」。在本例中,請分別選取「us-central1」和「us-central1-a」。
在「分享類型」專區,執行下列任一操作:
如要建立單一專案的預留項目,請選取「本機」。
如要建立共用保留項目,請選取「共用」,然後指定要共用保留項目的專案。
在「Use with VM instance」(搭配 VM 執行個體使用) 區段中,選取「Select specific reservation」(選取特定保留項目)。
在「Number of VM instances」(VM 執行個體數量) 欄位中輸入
3
。在「一般用途」分頁中,選取「N2」。
在「Machine type」部分的「Preset」分頁中 (預設),選取「n2-standard-32」。
展開「CPU 平台和 GPU」,然後在「CPU 平台」欄位中選取「Intel Cascade Lake 或以上版本」。
按一下 [建立]。
如要建立會使用示例保留項目的執行個體,請完成下列步驟:
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
前往「Create an instance」(建立執行個體)
系統隨即會顯示「Create an instance」(建立執行個體) 頁面,並顯示「Machine configuration」(機器設定) 窗格。
在「Machine configuration」(機器設定) 窗格中,執行下列操作:
在「名稱」欄位中輸入執行個體的名稱。在這個範例中,請輸入
instance-02
。指定要預留資源的「區域」和「可用區」。在本例中,請分別選取「us-central1」和「us-central1-a」。
在「一般用途」分頁中,選取「N2」。
在「Machine type」部分的「Preset」分頁中 (預設),選取「n2-standard-32」。
展開「進階選項」部分,然後在「CPU 平台」欄位中選取「Intel Cascade Lake 或以上版本」。
在導覽選單中,按一下「進階」。
在「進階」窗格中的「預訂」部分,選取「選擇預訂」,然後按一下「選擇預訂」。
在隨即顯示的「Choose a reservation」窗格中,執行下列操作:
選取您在先前步驟中建立的特定保留項目。如果您想使用位於其他專案中的共用預留項目,請在「專案」清單中選取該預留項目所在的專案。
按一下「選擇」。
按一下 [建立]。
gcloud
以下範例說明如何在區域 us-central1-a
中為三個 N2 執行個體建立特定保留項目,其中包含 32 個 vCPU,並將 Intel Cascade Lake 做為最低 CPU 平台。並說明如何建立單一執行個體來使用保留項目。
如要建立示例特定保留項目並加以使用,請執行下列步驟:
如要建立範例預訂,請使用
gcloud compute reservations create
指令搭配--require-specific-reservation
旗標:gcloud compute reservations create reservation-02 \ --machine-type=n2-standard-32 \ --min-cpu-platform="Intel Cascade Lake" \ --require-specific-reservation \ --vm-count=3 \ --zone=us-central1-a
如要建立會使用範例保留作業的執行個體,請使用
gcloud compute instances create
指令搭配--reservation
和--reservation-affinity=specific
旗標:gcloud compute instances create instance-02 \ --machine-type=n2-standard-32 \ --min-cpu-platform="Intel Cascade Lake" \ --reservation-affinity=specific \ --reservation=RESERVATION_URL \ --zone=us-central1-a
請將
RESERVATION_URL
替換為預訂的網址。請指定下列其中一個值:如果您在同一個專案中建立預留項目:
reservation-02
如果保留項目位於其他專案中:
projects/PROJECT_ID/reservations/reservation-02
Go
以下範例說明如何在區域 us-central1-a
中,建立具有 32 個 vCPU 的 N2 執行個體,並將 Intel Cascade Lake 做為最低 CPU 平台,以便使用特定的符合條件保留項目:
如要建立範例例項,以便使用單一專案的特定預留空間,請使用下列程式碼範例:
如要建立範例例項,以便使用共用特定預留空間,請使用下列程式碼範例:
Java
以下範例說明如何在區域 us-central1-a
中建立 N1 執行個體,其中包含四個 vCPU,並以 Intel Skylake 做為最低 CPU 平台,以便使用特定的符合保留項目:
如要建立範例保留項目做為單一專案保留項目,並建立要用於取用的例項,請使用以下程式碼範例:
如要建立示例保留項目做為共用保留項目,並建立要使用該項目的例項,請使用下列程式碼範例:
Node.js
以下範例說明如何在區域 us-central1-a
中,建立具有 4 個 vCPU 和 Intel Skylake 做為最低 CPU 平台的 N1 執行個體,以便使用特定的符合條件保留項目:
Python
以下範例說明如何在區域 us-central1-a
中,建立具有 32 個 vCPU 的 N2 執行個體,並將 Intel Cascade Lake 做為最低 CPU 平台,以便使用特定的符合條件保留項目:
如要建立示例特定保留項目並加以使用,請執行下列步驟:
如要建立並使用範例預留作業做為單一專案預留作業,請使用下列程式碼範例:
如要建立並使用範例預訂做為共用預訂,請使用下列程式碼範例:
REST
以下範例說明如何在區域 us-central1-a
中為三個 N2 執行個體建立特定保留項目,其中包含 32 個 vCPU,並將 Intel Cascade Lake 做為最低 CPU 平台。並說明如何建立單一執行個體來使用保留項目。
如要建立示例特定保留項目並加以使用,請執行下列步驟:
如要建立範例預留項目,請對
instances.insert
方法發出POST
要求。在要求主體中,請納入設為true
的specificReservationRequired
欄位:POST https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-central1-a/reservations { "name": "reservation-02", "specificReservation": { "count": "3", "instanceProperties": { "machineType": "n2-standard-32", "minCpuPlatform": "Intel Cascade Lake", } }, "specificReservationRequired": true }
如要建立會使用範例預留項目的執行個體,請對
instances.insert
方法發出POST
要求。在要求主體的reservationAffinity
欄位中,加入下列內容:consumeReservationType
欄位設為SPECIFIC_RESERVATION
。key
欄位設為compute.googleapis.com/reservation-name
。values
欄位設為預訂項目的網址。
要求會與下列內容相似:
POST https://compute.googleapis.com/compute/v1/projects/example-project/zones/us-central1-a/instances { "name": "instance-02", "machineType": "zones/us-central1-a/machineTypes/n2-standard-32", "minCpuPlatform": "Intel Cascade Lake", "disks": [ { "boot": true, "initializeParams": { "sourceImage": "projects/debian-cloud/global/images/family/debian-12" } } ], "networkInterfaces": [ { "network": "global/networks/default" } ], "reservationAffinity": { "consumeReservationType": "SPECIFIC_RESERVATION", "key": "compute.googleapis.com/reservation-name", "values": [ "RESERVATION_URL" ] } }
請將
RESERVATION_URL
替換為預訂的網址。請指定下列其中一個值:如果您在同一個專案中建立預留項目:
reservation-02
如果保留項目位於其他專案中:
projects/PROJECT_ID/reservations/reservation-02
測試執行個體屬性是否與自動使用的保留項目相符
如要測試 Compute 執行個體的屬性是否與自動消耗的保留項目相符,請執行下列操作:
建立保留項目的副本,做為單一執行個體的特定指定保留項目。
建立測試用例,以便使用保留項目。
如果您可以建立測試例項,則其屬性會與測試保留項目的屬性相符。否則建立執行個體會失敗。
確認測試執行個體和測試保留項目的屬性相符後,請刪除保留項目和測試執行個體。
驗證保留項目用量
如要驗證預訂使用情形,您可以執行下列一或多項操作:
如要查看目前有多少 Compute 執行個體正在使用保留項目,以及還有多少執行個體可以使用保留項目,請查看保留項目。
如要監控每 30 分鐘更新一次的預訂消費資料,並在預訂完成或未完成消費時收到快訊,請監控預訂消費資料。
如要查看每 24 小時更新一次的預訂用量資料,請執行下列任一操作:
- 如要在 Cloud Storage 值區中接收使用量報表,以便分析使用量趨勢並找出未用完的預留空間,請查看預留空間使用量報表。
- 如要查看過去和預測的預留容量使用量,以便分析使用量趨勢並規劃未來的容量需求,請使用容量規劃工具。