建立新的 Hyperdisk 磁碟區


您可以完成下列工作,在 Compute Engine 執行個體中使用 Hyperdisk Balanced、Hyperdisk Extreme 或 Hyperdisk Throughput 磁碟分割區:

  • 建立空白、非開機和可用區 Hyperdisk 磁碟區,並在建立執行個體時或之後將其附加至執行個體。
  • 格式化及掛接磁碟區,以便存取資料或檔案系統。

對於 Hyperdisk Balanced 磁碟區,您也可以建立啟動磁碟和資料磁碟。

如需 Hyperdisk 的一般資訊,請參閱「關於 Hyperdisk」。

如要為執行個體新增 Hyperdisk 平衡高可用性磁碟,請按照下列程序操作:

事前準備

  • 在將 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.

    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.

    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.

    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.

    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.

    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.

    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.

    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.

必要角色和權限

如要取得在執行個體中新增 Hyperdisk 磁碟區所需的權限,請要求管理員為您授予專案的下列 IAM 角色:

如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

這些預先定義的角色包含在執行個體中新增 Hyperdisk 磁碟區所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:

所需權限

如要將 Hyperdisk 磁碟區新增至執行個體,您必須具備下列權限:

  • 如要建立及附加 Hyperdisk 磁碟區,請按照下列步驟操作:
    • compute.disks.create 專案
    • 執行個體的 compute.instances.attachDisk 權限
    • compute.disks.use 在要連結至執行個體的磁碟上
  • 如要格式化及掛接已連結的磁碟區,請按照下列步驟操作: compute.instances.setMetadata 在執行個體上

您或許還可透過自訂角色或其他預先定義的角色取得這些權限。

Hyperdisk 磁碟區支援的值

建立或修改 Hyperdisk 磁碟區時使用的值,必須落在 Hyperdisk 磁碟區限制中所述的最大值和最小值範圍內。

如果您要修改已連結至 VM 的 Hyperdisk 磁碟區大小,新值不得超過 每個 VM 的 Hyperdisk 限制

Hyperdisk 磁碟區的已佈建 IOPS 和處理量必須遵循「 關於 Hyperdisk 的 IOPS 和處理量佈建」一文中所述的規則。

在執行個體中新增 Hyperdisk 磁碟區

您可以使用 Google Cloud 控制台、Google Cloud CLI 或 REST 建立及連結 Hyperdisk 磁碟區。

建立 Hyperdisk 磁碟區時,您指定的大小、傳輸量和 IOPS 必須在支援的值範圍內。

建立 Hyperdisk 平衡式磁碟區時,您可以選擇在多重寫入模式下建立磁碟,讓多個執行個體同時存取磁碟。

主控台

  1. 前往「VM instances」(VM 執行個體) 頁面。

    前往 VM 執行個體

  2. 按一下您要為其新增磁碟的執行個體名稱。

  3. 在「VM instance details」(VM 執行個體詳細資料) 頁面上,按一下 [Edit] (編輯)

  4. 在「Additional disks」(其他磁碟) 標題下方,點選「Add new disk」(增加新磁碟)

  5. 指定磁碟名稱,並視需要加入說明。選取「磁碟來源類型」的「空白磁碟」

  6. 在「磁碟設定」下方,從下列清單中選擇磁碟類型。您指定的值必須在支援的值範圍內。

    1. Hyperdisk Balanced。您也可以變更預設磁碟的大小已配置 IOPS已配置的傳輸量設定。
    2. Hyperdisk Extreme。您也可以變更預設磁碟的大小已配置 IOPS 設定。
    3. Hyperdisk ML。您也可以變更預設磁碟大小已配置的傳輸量設定。
    4. Hyperdisk 處理量。您也可以變更預設磁碟的大小已佈建的處理量設定。
  7. 選用:針對 Hyperdisk 平衡磁區,您可以透過在多重寫入模式下建立磁碟,啟用將磁碟連接至多個執行個體的功能。在「存取模式」下方,選取「以讀寫模式連接多個 VM」

  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 處理量:從 2 TiB 到 32 TiB (含),以 1 GiB 為單位遞增。
    • DISK_TYPE:磁碟類型。請使用下列其中一個值:hyperdisk-balancedhyperdisk-extremehyperdisk-mlhyperdisk-throughput
    • IOPS_LIMIT:選用:對於 Hyperdisk Balanced 或 Hyperdisk Extreme 磁碟,這是磁碟可處理的每秒輸入/輸出作業數 (IOPS)。
    • THROUGHPUT_LIMIT:選用:對於 Hyperdisk Balanced、Hyperdisk ML 或 Hyperdisk Throughput 磁碟區,此為整數,代表磁碟可提供的最大處理量 (以每秒 MiB 為單位)。
    • DISK_ACCESS_MODE:選用:計算機例如何存取磁碟上的資料。支援的值如下:

      • READ_WRITE_SINGLE,用於從單一執行個體讀取/寫入資料。這是目前的預設做法。
      • READ_WRITE_MANY (僅限 Hyperdisk Balanced 和 Hyperdisk Balanced High Availability),用於從多個執行個體同時讀寫存取。
      • READ_ONLY_MANY (僅限 Hyperdisk ML),適用於從多個執行個體同時存取唯讀資料。

      如要設定 Hyperdisk 平衡高可用性磁碟的存取模式,請參閱「建立區域磁碟」。

  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 High Availability),用於從多個執行個體同時讀寫存取。
      • READ_ONLY_MANY (僅限 Hyperdisk ML),適用於從多個執行個體同時存取唯讀資料。

      如要設定 Hyperdisk 平衡高可用性磁碟的存取模式,請參閱「建立區域磁碟」。

  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();

建立磁碟後,您可以將磁碟連接至任何執行中或已停止的執行個體。

格式化及掛接磁碟

建立並將新磁碟連接至執行個體後,必須格式化和掛接磁碟,好讓作業系統能使用可用的儲存空間。

後續步驟