Compute Engine 可讓您在 Google 基礎架構上建立及執行執行個體。本文說明如何建立 Compute Engine 執行個體。
「Compute Engine 執行個體」、「運算執行個體」或「執行個體」是同義詞。根據您指定的機器類型,執行個體可以是裸機執行個體或虛擬機器 (VM) 執行個體,如下所示:
- 如果機器類型名稱結尾為
-metal
,則執行個體為裸機執行個體,未安裝虛擬機器管理程式。 - 否則,執行個體就是 VM 執行個體。「虛擬機器執行個體」、「VM 執行個體」和「VM」是同義詞。
在說明文件和 Google Cloud 介面 (例如 Google Cloud 主控台、gcloud 指令列工具和 REST API) 中,同義詞會互相替換使用。
本文件中的操作說明僅介紹如何建立執行個體,並提供建立執行個體的起點。如需建立具有特定或複雜設定的執行個體的詳細步驟,請改為參閱「建立並啟動具有特定設定的執行個體」。
事前準備
- 請參閱建立執行個體的相關基本資訊。
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 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.
Terraform
To use the Terraform 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.
C#
To use the .NET 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.
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.
PHP
To use the PHP 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.
Ruby
To use the Ruby 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.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 設定標記:
compute.instances.setTags
在 VM 上 -
如要為 VM 設定標籤,請按照下列步驟操作:
compute.instances.setLabels
VM 上 -
如要設定 VM 使用的服務帳戶,請按照下列步驟操作:
compute.instances.setServiceAccount
在 VM 上 -
如要為 VM 建立新磁碟,請按照下列步驟操作:
compute.disks.create
專案 -
如要以唯讀或讀寫模式連接現有磁碟:
磁碟的
compute.disks.use
權限 -
如要以唯讀模式連接現有磁碟,請執行下列操作:
compute.disks.useReadOnly
磁碟
建立及啟動執行個體的方法
本節將介紹用於建立及啟動 Compute Engine 執行個體的基本方法。具體來說,在您學習如何建立執行個體時,建議您使用 Google Cloud 控制台、Google Cloud CLI 或 REST API。如要瞭解使用這些方法建立執行個體的一般程序,請參閱下列操作說明。
主控台
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
前往「Create an instance」(建立執行個體)
「Create an instance」畫面隨即顯示,並顯示「Machine configuration」窗格。
如要設定執行個體屬性,請使用下列導覽選單中的選項。
如要設定與名稱、位置或機器設定相關的執行個體屬性,請按一下「Machine configuration」(機器設定)。在隨即顯示的「機器設定」窗格中,指定要設定的屬性值。
如要設定與開機磁碟、作業系統 (OS) 和其他非開機儲存空間選項相關的執行個體屬性,請按一下「OS and storage」。在隨即顯示的「Operating system and storage」(作業系統和儲存空間) 窗格中,指定要設定的屬性值。
如要設定與網路介面和防火牆設定相關的執行個體屬性,請按一下「網路」。在隨即顯示的「Networking」窗格中,指定要設定的屬性值。
如要設定與 Ops Agent 和虛擬顯示器相關的執行個體屬性,請按一下「可觀察性」。在隨即顯示的「可觀察性」窗格中,指定要設定的屬性值。
如要設定與安全性和存取權相關的執行個體資源,請按一下「安全性」。在隨即開啟的「安全性」窗格中,為要設定的屬性指定值。
如要設定與中繼資料、預留、資源組織、佈建類型和單一租戶相關的執行個體屬性,請按一下「進階」。在隨即開啟的「進階」窗格中,為要設定的屬性指定值。
如要建立並啟動執行個體,請按一下「建立」。
gcloud
如要建立自訂設定的執行個體,請使用 gcloud compute instances create
指令。
您無法使用這項指令來大量建立執行個體,或執行容器映像檔的執行個體。請改用下列方法:
- 如要大量建立執行個體,請使用
gcloud compute instances bulk create
指令。 - 如要建立執行個體來部署容器,請使用
gcloud compute instances create-with-container
指令。
REST
如要建立採用自訂設定的執行個體,請對 instances.insert
方法發出 POST
要求。
您無法使用這個方法大量建立執行個體。請改為對 instances.bulkInsert
方法發出 POST
要求。
使用特定設定建立及啟動執行個體
以下每份文件都提供操作說明,說明如何建立及啟動使用一或多個特定設定選項的執行個體。根據用途,您可以結合指示來建立執行個體,以便使用多個文件中的設定選項。如要瞭解您在建立執行個體時可以設定的各種參數,請參閱「建立執行個體時可用的設定選項」。
- 為您預先設定
- 建立由 Google 設定且針對工作負載進行最佳化的執行個體
- 自訂機器設定
- 建立具有自訂主機名稱的執行個體
- 建立具有自訂機器類型的執行個體
- 建立附加 GPU 的執行個體
- 為執行個體指定最低 CPU 平台
- 自訂 OS 設定
- 從公開映像檔建立執行個體
- 從自訂映像檔建立執行個體
- 從共用映像檔建立執行個體
- 使用 RHEL BYOS 映像檔建立執行個體
- 自訂網路設定
- 在特定子網路中建立執行個體
- 建立使用 gVNIC 網路介面的執行個體
- 設定頻寬較高的執行個體
- 自訂觀測能力設定
- 建立已針對作業套件代理程式監控和記錄功能進行設定的執行個體
- 在執行個體上啟用虛擬顯示器
- 自訂安全性設定
- 建立使用使用者管理服務帳戶的執行個體
- 啟用受管理的工作負載身分建立 VM
- 在建立 VM 時啟用 OS 登入功能
- 從備份還原
- 透過機器映像檔建立執行個體
- 透過磁碟快照建立執行個體
- 從備份金庫還原執行個體
- 從現有設定
- 利用執行個體範本建立執行個體
- 建立類似現有執行個體的執行個體
- 可針對特定工作負載進行客製化
- 建立執行個體以部署容器
- 建立 Windows Server 執行個體
- 建立 SQL Server 執行個體
- 使用高效能運算 (HPC) 映像檔建立執行個體
- 自訂佈建類型
- 建立 Spot 執行個體
- 建立使用預留執行個體的執行個體
- 一次執行多個 VM 和執行個體
- 大量建立執行個體
- 建立代管執行個體群組 (MIG)
- 單一用戶群節點
- 在單一用戶群節點上建立執行個體
- 效率極佳的執行個體
- 建立附有執行個體時間表的執行個體
疑難排解
如要瞭解如何解決常見的執行個體建立錯誤,請參閱「疑難排解執行個體建立作業」一文。
後續步驟
- 瞭解如何查看執行個體的狀態,瞭解執行個體何時可以使用。
- 瞭解如何連線至執行個體。
- 瞭解如何將執行個體擴展為一組執行個體。
- 瞭解如何為執行個體預留容量。
- 瞭解如何透過承諾使用折扣和續用折扣節省執行個體成本。
歡迎試用
如果您未曾使用過 Google Cloud,歡迎建立帳戶,親自體驗實際使用 Compute Engine 的成效。新客戶可以獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
免費試用 Compute Engine