创建并启动 Compute Engine 实例


使用 Compute Engine,您可以在 Google 基础架构上创建和运行实例。本文档介绍如何创建 Compute Engine 实例。

术语“Compute Engine 实例”“计算实例”或“实例”是同义词。根据您指定的机器类型,实例可以是裸金属实例或虚拟机 (VM) 实例,如下所示:

  • 如果机器类型的名称以 -metal 结尾,则该实例是裸金属实例,没有安装 Hypervisor。
  • 否则,该实例为虚拟机实例。术语“虚拟机实例”“VM 实例”和“虚拟机”是同义词。

在文档和 Google Cloud 界面(例如Google Cloud consolegcloud 命令行工具和 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

    1. 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.

    2. Set a default region and zone.
    3. Terraform

      如需在本地开发环境中使用本页面上的 Terraform 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。

      1. Install the Google Cloud CLI.

      2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      3. To initialize the gcloud CLI, run the following command:

        gcloud init
      4. 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.

      5. 如需了解详情,请参阅 Set up authentication for a local development environment

      C#

      如需在本地开发环境中使用本页面上的 .NET 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。

      1. Install the Google Cloud CLI.

      2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      3. To initialize the gcloud CLI, run the following command:

        gcloud init
      4. 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.

      5. 如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC

      Go

      如需在本地开发环境中使用本页面上的 Go 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。

      1. Install the Google Cloud CLI.

      2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      3. To initialize the gcloud CLI, run the following command:

        gcloud init
      4. 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.

      5. 如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC

      Java

      如需在本地开发环境中使用本页面上的 Java 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。

      1. Install the Google Cloud CLI.

      2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      3. To initialize the gcloud CLI, run the following command:

        gcloud init
      4. 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.

      5. 如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC

      Node.js

      如需在本地开发环境中使用本页面上的 Node.js 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。

      1. Install the Google Cloud CLI.

      2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      3. To initialize the gcloud CLI, run the following command:

        gcloud init
      4. 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.

      5. 如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC

      PHP

      如需在本地开发环境中使用本页面上的 PHP 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。

      1. Install the Google Cloud CLI.

      2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      3. To initialize the gcloud CLI, run the following command:

        gcloud init
      4. 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.

      5. 如需了解详情,请参阅 Set up authentication for a local development environment

      Python

      如需在本地开发环境中使用本页面上的 Python 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。

      1. Install the Google Cloud CLI.

      2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      3. To initialize the gcloud CLI, run the following command:

        gcloud init
      4. 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.

      5. 如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC

      Ruby

      如需在本地开发环境中使用本页面上的 Ruby 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。

      1. Install the Google Cloud CLI.

      2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      3. To initialize the gcloud CLI, run the following command:

        gcloud init
      4. 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.

      5. 如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC

      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 身份验证文档中的使用 REST 时进行身份验证

所需的角色

如需获得创建实例所需的权限,请让您的管理员为您授予项目的 Compute Instance Admin (v1) (roles/compute.instanceAdmin.v1) IAM 角色。如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

此预定义角色可提供创建实例所需的权限。如需查看所需的确切权限,请展开所需权限部分:

所需权限

创建实例需要以下权限:

  • 针对项目的 compute.instances.create 权限
  • 使用自定义映像创建虚拟机:针对映像的 compute.images.useReadOnly 权限
  • 使用快照创建虚拟机:针对快照的 compute.snapshots.useReadOnly 权限
  • 使用实例模板创建虚拟机:针对实例模板的 compute.instanceTemplates.useReadOnly 权限
  • 为虚拟机分配旧版网络:针对项目的 compute.networks.use 权限
  • 为虚拟机指定静态 IP 地址:针对项目的 compute.addresses.use 权限
  • 使用旧版网络时为虚拟机分配外部 IP 地址:针对项目的 compute.networks.useExternalIp 权限
  • 为虚拟机指定子网:针对项目或所选子网的 compute.subnetworks.use 权限
  • 在使用 VPC 网络时为虚拟机分配外部 IP 地址:针对项目或所选子网的 compute.subnetworks.useExternalIp 权限
  • 为虚拟机设置虚拟机实例元数据:针对项目的 compute.instances.setMetadata 权限
  • 为虚拟机设置标记:针对虚拟机的 compute.instances.setTags 权限
  • 为虚拟机设置标签:针对虚拟机的 compute.instances.setLabels 权限
  • 为虚拟机设置要使用的服务账号:针对虚拟机的 compute.instances.setServiceAccount 权限
  • 为虚拟机创建新磁盘:针对项目的 compute.disks.create 权限
  • 以只读或读写模式挂接现有磁盘:针对磁盘的 compute.disks.use 权限
  • 以只读模式挂接现有磁盘:针对磁盘的 compute.disks.useReadOnly 权限

您也可以使用自定义角色或其他预定义角色来获取这些权限。

创建和启动实例的方法

本部分介绍了您可以使用的基本方法来创建和启动 Compute Engine 实例。具体而言,在学习如何创建实例时,我们建议您使用 Google Cloud 控制台、Google Cloud CLI 或 REST API。如需了解使用上述每种方法创建实例的一般流程,请查看以下说明。

控制台

  1. 在 Google Cloud 控制台中,前往创建实例页面。

    前往“创建实例”

    此时将显示创建实例屏幕,并显示机器配置窗格。

  2. 如需配置实例属性,请使用导航菜单中的选项,如下所示。

    1. 如需配置与名称、位置或机器配置相关的实例属性,请点击机器配置。在随即显示的机器配置窗格中,为要配置的属性指定值。

    2. 如需配置与启动磁盘、操作系统 (OS) 和其他非启动存储空间选项相关的实例属性,请点击操作系统和存储空间。在显示的操作系统和存储空间窗格中,为要配置的属性指定值。

    3. 如需配置与网络接口和防火墙设置相关的实例属性,请点击网络。在显示的网络窗格中,为要配置的属性指定值。

    4. 如需配置与 Ops Agent 和虚拟显示屏相关的实例属性,请点击可观测性。在显示的可观测性窗格中,为要配置的属性指定值。

    5. 如需配置与安全和访问权限相关的实例属性,请点击安全。在打开的安全性窗格中,为要配置的属性指定值。

    6. 如需配置与元数据、预订、资源组织、配置类型和单租户相关的实例属性,请点击高级。在随即打开的高级窗格中,为要配置的属性指定值。

  3. 如需创建并启动该实例,请点击创建

gcloud

如需使用您自己的配置创建实例,请使用 gcloud compute instances create 命令

您无法使用此命令批量创建实例运行容器映像的实例。请改为执行以下操作:

REST

如需使用您自己的配置创建实例,请向 instances.insert 方法发出 POST 请求。

您无法使用此方法批量创建实例。请改为发出 POST 请求,调用 instances.bulkInsert 方法

创建和启动具有特定配置的实例

以下每篇文档都提供了有关如何创建和启动使用一个或多个特定配置选项的实例的说明。根据您的用例,您可以通过合并说明来创建一个实例,该实例使用多个文档中的配置选项。如需了解您可以在创建实例时配置的各种参数,请参阅实例创建期间可用的配置选项

为您预先配置
创建 Google 配置的针对工作负载进行了优化的实例
自定义机器配置
使用自定义主机名创建实例
使用自定义机器类型创建实例
创建挂接了 GPU 的实例
为实例指定最低 CPU 平台
自定义操作系统配置
通过公共映像创建实例
通过自定义映像创建实例
通过共享映像创建实例
使用 RHEL BYOS 映像创建实例
自定义网络配置
在特定子网中创建实例
创建使用 gVNIC 网络接口的实例
配置带宽更高的实例
自定义的可观测性配置
创建为 Ops Agent 监控和日志记录配置的实例
在实例上启用虚拟显示
自定义安全配置
创建使用用户管理的服务账号的实例
创建启用了受管工作负载身份的虚拟机
在创建虚拟机期间启用 OS Login
从备份中恢复
通过机器映像创建实例
通过磁盘快照创建实例
从备份保险库恢复实例
从现有配置中
通过实例模板创建实例
创建与现有实例类似的实例
针对特定工作负载进行定制
创建用于部署容器的实例
创建 Windows Server 实例
创建 SQL Server 实例
创建使用高性能计算 (HPC) 映像的实例
自定义预配类型
创建抢占式实例
创建使用预留实例的实例
一次性创建多个虚拟机和实例
批量创建实例
创建托管式实例组 (MIG)
单租户节点
在单租户节点上创建实例
高效实例
创建附带实例调度的实例

问题排查

如需查找解决常见实例创建错误的方法,请参阅排查实例创建问题

后续步骤

自行试用

如果您是 Google Cloud 新手,请创建一个账号来评估 Compute Engine 在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。

免费试用 Compute Engine