创建新的 Hyperdisk 卷


您可以通过完成以下任务,将 Hyperdisk Balanced、Hyperdisk Extreme 或 Hyperdisk Throughput 卷与 Compute Engine 实例搭配使用:

  • 创建空白的非启动、可用区级 Hyperdisk 卷,并在实例创建期间或之后将其挂接到实例。
  • 格式化卷并进行装载,以提供对数据或文件系统的访问权限。

对于 Hyperdisk Balanced 卷,您还可以创建启动磁盘和数据磁盘。

如需了解 Hyperdisk 的一般信息,请参阅关于 Hyperdisk

如需向实例添加 Hyperdisk Balanced 高可用性磁盘,请按照以下步骤操作:

准备工作

  • 在将 Hyperdisk 卷添加到计算实例之前,请查看 Hyperdisk 限制
  • 如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 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. 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

      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 时进行身份验证

所需的角色和权限

如需获得将 Hyperdisk 卷添加到实例所需的权限,请让您的管理员为您授予项目的以下 IAM 角色:

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

这些预定义角色可提供将 Hyperdisk 卷添加到实例所需的权限。如需查看所需的确切权限,请展开所需权限部分:

所需权限

如需将 Hyperdisk 磁盘卷添加到实例,您需要具备以下权限:

  • 为了创建并挂接 Hyperdisk 卷:
    • 针对项目的 compute.disks.create 权限
    • 针对实例的 compute.instances.attachDisk 权限
    • 针对您要挂接到实例的卷的 compute.disks.use
  • 为了格式化和装载挂接的卷:针对实例的 compute.instances.setMetadata

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

Hyperdisk 卷支持的值

大小限制

您为 Hyperdisk 卷指定的大小必须在下表中列出的范围内。

Hyperdisk 类型 大小下限 大小上限 默认大小
平衡 Hyperdisk 4 GiB 64 TiB 100 GiB
Hyperdisk Balanced 高可用性 4 GiB 64 TiB 100 GiB
Hyperdisk Extreme 64 GiB 64 TiB 1 TiB
Hyperdisk ML 4 GiB 64 TiB 100 GiB
Hyperdisk Throughput 2 TiB 32 TiB 2 TiB

不过,挂接到虚拟机的 Hyperdisk 卷的大小不能超过该虚拟机的 Hyperdisk 限制

性能限制

下表列出了您可以为每种 Hyperdisk 类型指定的性能限制。不过,您可以指定的 IOPS 和吞吐量级别也取决于卷的大小。如需详细了解每项限制,请参阅 Hyperdisk BalancedHyperdisk Balanced 高可用性Hyperdisk ExtremeHyperdisk MLHyperdisk 吞吐量大小和性能限制部分。

Hyperdisk 类型 IOPS 限制 吞吐量限制 (MiB/s)
平衡 Hyperdisk 160,000 2400
Hyperdisk Balanced 高可用性 100000 1,200
Hyperdisk Extreme 350,000 您无法为 Hyperdisk Extreme 卷预配吞吐量级别。每个卷每达到 1,000 IOPS 可获得 250 MiB/s 的吞吐量,最多为 5,000 MiB/s。
Hyperdisk ML 您无法预配 IOPS 级别,但每 MiB/s 预配吞吐量可带来 16 IOPS,最多可达 19,200,000 IOPS。 1,200,000
Hyperdisk Throughput 您无法预配 IOPS 级别,但每 MiB/s 的预配吞吐量可带来 4 IOPS,最多 2,400 IOPS。 600

Hyperdisk 类型变量

下表列出了每种 Hyperdisk 类型的参考值。使用 Google Cloud CLI、REST 或 Cloud Client Libraries for Compute Engine 创建 Hyperdisk 卷时,请通过提供表格中的相应值来指明要创建的 Hyperdisk 类型。

磁盘类型 参考名称
平衡 Hyperdisk hyperdisk-balanced
Hyperdisk Balanced 高可用性 hyperdisk-balanced-high-availability
Hyperdisk Extreme hyperdisk-extreme
Hyperdisk Throughput hyperdisk-throughput
Hyperdisk ML hyperdisk-ml

将 Hyperdisk 卷添加到实例

您可以使用Google Cloud 控制台、Google Cloud CLI 或 REST 创建并挂接 Hyperdisk 卷。

您在创建 Hyperdisk 卷时指定的大小、吞吐量和 IOPS 必须在支持的值范围内。

创建 Hyperdisk Balanced 卷时,您可以视情况通过采用多写入者模式创建磁盘来允许多个实例同时访问磁盘。

控制台

  1. 转到虚拟机实例页面。

    转到虚拟机实例

  2. 点击要在其中添加磁盘的实例的名称。

  3. 虚拟机实例详情页面上,点击修改

  4. 额外磁盘标题下,点击添加新磁盘

  5. 为磁盘指定一个名称,并视需要添加说明。选择空白磁盘作为磁盘来源类型

  6. 磁盘设置下,从以下列表中选择磁盘类型。您指定的值必须在支持的值范围内。

    1. Hyperdisk Balanced。您还可以更改默认磁盘的大小预配 IOPS预配吞吐量设置。
    2. Hyperdisk Extreme。您还可以更改默认磁盘的大小预配 IOPS 设置。
    3. Hyperdisk ML。您还可以更改默认磁盘的大小预配吞吐量设置。
    4. Hyperdisk Throughput。您还可以更改默认磁盘大小预配吞吐量设置。
  7. 可选:对于 Hyperdisk Balanced 卷,您可以通过采用多写入者模式创建磁盘来允许将磁盘挂接到多个实例。在访问模式下,选择从多个虚拟机进行读写

  8. 点击保存

  9. 如需将更改应用于实例,请点击保存

gcloud

  1. 使用 gcloud compute disks create 命令创建 Hyperdisk 卷。

    gcloud compute disks create DISK_NAME \
       --zone=ZONE \
       --size=DISK_SIZE \
       --type=DISK_TYPE \
       --provisioned-iops=IOPS_LIMIT
       --provisioned-throughput=THROUGHPUT_LIMIT
       --access-mode=DISK_ACCESS_MODE
    

    替换以下内容:

    • DISK_NAME:新磁盘的名称。
    • ZONE:要在其中创建新磁盘的可用区的名称。
    • DISK_SIZE:可选:新磁盘的大小。该值必须是一个整数,后跟大小单位(GB 表示吉比字节,或 TB 表示太比字节)。如果未指定大小单位,则使用 100 GB 作为默认值。接受的磁盘大小值包括:
      • Hyperdisk Balanced:从 4 GiB 到 64 TiB(含边界值),以 1 GiB 为增量。
      • Hyperdisk Extreme:从 64 GiB 到 64 TiB(含边界值),以 1 GiB 为增量。
      • Hyperdisk ML:从 4 GiB 到 64 TiB(含边界值),以 1 GiB 为增量。
      • Hyperdisk Throughput:从 2 TiB 到 32 TiB(含边界值),以 1 GiB 为增量。
    • DISK_TYPE:磁盘类型。请使用以下值之一:hyperdisk-balancedhyperdisk-extremehyperdisk-mlhyperdisk-throughput
    • IOPS_LIMIT(可选):对于 Hyperdisk Balanced 磁盘或 Hyperdisk Extreme 磁盘,这是磁盘可处理的每秒 I/O 操作数 (IOPS)。
    • THROUGHPUT_LIMIT(可选):对于 Hyperdisk Balanced、Hyperdisk ML 或 Hyperdisk Throughput 卷,这是一个整数,表示磁盘可提供的最大吞吐量(以 MiB/秒为单位)。
    • DISK_ACCESS_MODE(可选):计算实例如何访问磁盘上的数据。支持的值包括:

      • READ_WRITE_SINGLE,表示从一个实例进行读写访问。 这是默认设置。
      • READ_WRITE_MANY(仅限 Hyperdisk Balanced 和 Hyperdisk Balanced 高可用性),用于从多个实例进行并发读写访问。
      • READ_ONLY_MANY(仅限 Hyperdisk ML),用于从多个实例进行并发只读访问。

      如需为 Hyperdisk Balanced 高可用性磁盘设置访问模式,请参阅创建区域性磁盘

  2. 可选:使用 gcloud compute disks describe DISK_NAME 命令查看磁盘的说明。

  3. 创建磁盘后,您可以将磁盘挂接到实例

REST

  1. 使用 disks.insert 方法构建 POST 请求以创建可用区级 Hyperdisk。 请求应包含 namesizeGbtypeprovisionedIopsprovisionedThroughput 属性。 如需创建一个空白且未格式化的非启动磁盘,请不要为此磁盘指定来源映像或来源快照。

    POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/disks
    {
       "name": "DISK_NAME",
       "sizeGb": "DISK_SIZE",
       "type": "https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/diskTypes/DISK_TYPE",
       "provisionedIops": "IOPS_LIMIT",
       "provisionedThroughput": "THROUGHPUT_LIMIT",
       "accessMode": "DISK_ACCESS_MODE"
    }
    

    替换以下内容:

    • PROJECT_ID:您的项目 ID
    • ZONE:您的实例和新磁盘所在的可用区
    • DISK_NAME:新磁盘的名称
    • DISK_SIZE:可选:新磁盘的大小。该值必须是一个整数,后跟大小单位(GB 表示吉比字节,或 TB 表示太比字节)。
    • DISK_TYPE:磁盘类型。如需创建 Hyperdisk 卷,请使用以下值之一:hyperdisk-balancedhyperdisk-extremehyperdisk-mlhyperdisk-throughput
    • IOPS_LIMIT(可选):对于 Hyperdisk Balanced 和 Hyperdisk Extreme,这是磁盘可处理的每秒 I/O 操作数。
    • THROUGHPUT_LIMIT(可选):对于 Hyperdisk Balanced、Hyperdisk ML 或 Hyperdisk Throughput 卷,这是一个整数,表示磁盘可提供的最大吞吐量(以 MiB/秒为单位)。
    • DISK_ACCESS_MODE(可选):计算实例如何访问磁盘上的数据。支持的值包括:

      • READ_WRITE_SINGLE,表示从一个实例进行读写访问。 这是默认设置。
      • READ_WRITE_MANY(仅适用于 Hyperdisk Balanced 和 Hyperdisk Balanced 高可用性),用于从多个实例进行并发读写访问。
      • READ_ONLY_MANY(仅限 Hyperdisk ML),用于从多个实例进行并发只读访问。

      如需为 Hyperdisk Balanced 高可用性磁盘设置访问模式,请参阅创建区域性磁盘

  2. 可选:使用 compute.disks.get 方法查看磁盘的说明。

  3. 创建磁盘后,您可以将磁盘挂接到任何正在运行或已停止的实例。

Go

// createHyperdisk creates a new Hyperdisk in the specified project and zone.
func createHyperdisk(w io.Writer, projectId, zone, diskName string) error {
	//   projectID := "your_project_id"
	//   zone := "europe-central2-b"
	//   diskName := "your_disk_name"

	ctx := context.Background()
	client, err := compute.NewDisksRESTClient(ctx)
	if err != nil {
		return fmt.Errorf("NewDisksRESTClient: %v", err)
	}
	defer client.Close()

	// use format "zones/{zone}/diskTypes/(hyperdisk-balanced|hyperdisk-throughput)".
	diskType := fmt.Sprintf("zones/%s/diskTypes/hyperdisk-balanced", zone)

	// Create the disk
	disk := &computepb.Disk{
		Name:   proto.String(diskName),
		Type:   proto.String(diskType),
		SizeGb: proto.Int64(10),
		Zone:   proto.String(zone),
	}

	req := &computepb.InsertDiskRequest{
		Project:      projectId,
		Zone:         zone,
		DiskResource: disk,
	}

	op, err := client.Insert(ctx, req)
	if err != nil {
		return fmt.Errorf("Insert disk request failed: %v", err)
	}

	// Wait for the insert disk operation to complete
	if err = op.Wait(ctx); err != nil {
		return fmt.Errorf("unable to wait for the operation: %w", err)
	}

	fmt.Fprintf(w, "Hyperdisk created: %v\n", diskName)
	return nil
}

Java


import com.google.cloud.compute.v1.Disk;
import com.google.cloud.compute.v1.DisksClient;
import com.google.cloud.compute.v1.InsertDiskRequest;
import com.google.cloud.compute.v1.Operation;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateHyperdisk {
  public static void main(String[] args)
          throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";
    // Name of the zone in which you want to create the disk.
    String zone = "europe-central2-b";
    // Name of the disk you want to create.
    String diskName = "YOUR_DISK_NAME";
    // The type of disk you want to create. This value uses the following format:
    // "zones/{zone}/diskTypes/(hyperdisk-balanced|hyperdisk-extreme|hyperdisk-throughput)".
    // For example: "zones/us-west3-b/diskTypes/hyperdisk-balanced"
    String diskType = String.format("zones/%s/diskTypes/hyperdisk-balanced", zone);
    // Size of the new disk in gigabytes.
    long diskSizeGb = 10;
    // Optional: For Hyperdisk Balanced or Hyperdisk Extreme disks,
    // this is the number of I/O operations per second (IOPS) that the disk can handle
    long provisionedIops = 3000;
    // Optional: For Hyperdisk Balanced or Hyperdisk Throughput volumes,
    // this is an integer that represents the throughput,
    // measured in MiB per second, that the disk can handle.
    long provisionedThroughput = 140;

    createHyperdisk(projectId, zone, diskName, diskType, diskSizeGb,
            provisionedIops, provisionedThroughput);
  }

  // Creates a hyperdisk in a project
  public static Disk createHyperdisk(String projectId, String zone, String diskName,
                                     String diskType, long diskSizeGb, long provisionedIops,
                                     long provisionedThroughput)
          throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (DisksClient client = DisksClient.create()) {
      // Create a disk.
      Disk disk = Disk.newBuilder()
              .setZone(zone)
              .setName(diskName)
              .setType(diskType)
              .setSizeGb(diskSizeGb)
              .setProvisionedIops(provisionedIops)
              .setProvisionedThroughput(provisionedThroughput)
              .build();

      InsertDiskRequest request = InsertDiskRequest.newBuilder()
              .setProject(projectId)
              .setZone(zone)
              .setDiskResource(disk)
              .build();

      // Wait for the insert disk operation to complete.
      Operation operation = client.insertAsync(request).get(1, TimeUnit.MINUTES);

      if (operation.hasError()) {
        System.out.println("Disk creation failed!");
        throw new Error(operation.getError().toString());
      }

      // Wait for server update
      TimeUnit.SECONDS.sleep(10);

      Disk hyperdisk = client.get(projectId, zone, diskName);

      System.out.printf("Hyperdisk '%s' has been created successfully", hyperdisk.getName());

      return hyperdisk;
    }
  }
}

Node.js

// Import the Compute library
const computeLib = require('@google-cloud/compute');
const compute = computeLib.protos.google.cloud.compute.v1;

// Instantiate a diskClient
const disksClient = new computeLib.DisksClient();
// Instantiate a zoneOperationsClient
const zoneOperationsClient = new computeLib.ZoneOperationsClient();

/**
 * TODO(developer): Update/uncomment these variables before running the sample.
 */
// Project ID or project number of the Google Cloud project you want to use.
const projectId = await disksClient.getProjectId();

// The zone where your VM and new disk are located.
// zone = 'europe-central2-b';

// The name of the new disk
// diskName = 'disk-name';

// The type of disk. This value uses the following format:
// "zones/{zone}/diskTypes/(hyperdisk-balanced|hyperdisk-extreme|hyperdisk-ml|hyperdisk-throughput)".
// For example: "zones/us-west3-b/diskTypes/hyperdisk-balanced"
const diskType = `zones/${zone}/diskTypes/hyperdisk-balanced`;
// Size of the new disk in gigabytes.
const diskSizeGb = 10;
// Optional: For Hyperdisk Balanced or Hyperdisk Extreme disks,
// this is the number of I/O operations per second (IOPS) that the disk can handle.
const provisionedIops = 3000;
// Optional: For Hyperdisk Balanced or Hyperdisk Throughput volumes,
// this is an integer that represents the throughput,
// measured in MiB per second, that the disk can handle.
const provisionedThroughput = 140;

async function callCreateComputeHyperdisk() {
  // Create a disk
  const disk = new compute.Disk({
    sizeGb: diskSizeGb,
    name: diskName,
    zone,
    type: diskType,
    provisionedIops,
    provisionedThroughput,
  });

  const [response] = await disksClient.insert({
    project: projectId,
    zone,
    diskResource: disk,
  });

  let operation = response.latestResponse;

  // Wait for the create disk operation to complete.
  while (operation.status !== 'DONE') {
    [operation] = await zoneOperationsClient.wait({
      operation: operation.name,
      project: projectId,
      zone: operation.zone.split('/').pop(),
    });
  }

  console.log(`Disk: ${diskName} created.`);
}

await callCreateComputeHyperdisk();

创建磁盘后,您可以将磁盘挂接到任何正在运行或已停止的实例。

格式化并装载磁盘

创建新磁盘并将其挂接到实例后,必须格式化并装载磁盘,以便操作系统可以使用可用的存储空间。

后续步骤