建立新的永久磁碟磁碟區


您可以建立空白的永久磁碟區,或是從資料來源建立磁碟。 您可以將永久磁碟做為虛擬機器 (VM) 執行個體的開機磁碟,或是連結至 VM 的資料磁碟。

本文說明如何建立空白的非開機區域永久磁碟磁碟區,並將其連結至 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

      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.

        詳情請參閱 Set up authentication for a local development environment

        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 驗證說明文件中的「Authenticate for using REST」。

限制

  • 建立 VM 時,您最多可以連結 127 個次要非開機區域永久磁碟磁碟區。
  • 每個 VM 的總連接容量為 257 TB。如要瞭解如何使用大型磁碟區來提高效能,請參閱「邏輯磁碟區大小」一文。

將非開機磁碟新增至 VM

使用 Google Cloud consoleGoogle Cloud CLIREST 建立及連結非開機區域磁碟。

永久磁碟類型變數

下表列出各類永續磁碟的參考值。使用 Google Cloud CLI、REST 或 Compute Engine 適用的 Cloud 用戶端程式庫建立永久磁碟卷時,請從下表提供對應值,指明要建立的永久磁碟類型。

如果您在 Google Cloud 控制台中建立磁碟,預設磁碟類型pd-balanced。如果您使用 gcloud CLI 或 REST 建立磁碟,預設磁碟類型為 pd-standard

磁碟類型 參照名稱
已平衡的永久磁碟 pd-balanced
效能型 (SSD) 永久磁碟 pd-ssd
標準永久磁碟 pd-standard
極端永久磁碟 pd-extreme

將磁碟附加至 VM 時,請指定自訂裝置名稱。 您指定的名稱會用於在客體 OS 中產生磁碟的符號連結,方便識別。

主控台

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

    前往 VM 執行個體頁面

  2. 按一下要新增磁碟的 VM 名稱。

  3. 在詳細資料頁面中,按一下「編輯」

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

  5. 指定磁碟的名稱,設定磁碟的屬性,然後選取「空白」做為「來源類型」

  6. 選用:在「裝置名稱」標題下方,選取「使用自訂裝置名稱」選項。您輸入的名稱會用於產生磁碟的符號連結,方便識別磁碟。

  7. 按一下[Done] (完成) 即可完成磁碟的設定。

  8. 按一下「儲存」,將變更套用到 VM 並新增磁碟。

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 使用 gcloud compute disks create 指令建立區域永久磁碟卷宗。

    gcloud compute disks create DISK_NAME \
      --size DISK_SIZE \
      --type DISK_TYPE
    

    更改下列內容:

    • DISK_NAME:新磁碟的名稱。
    • DISK_SIZE:新磁碟的大小 (以 GB 為單位)。可接受的大小範圍為 10 GB 到 65,536 GB (含首尾),調整的單位為 1 GB。
    • DISK_TYPE:永久磁碟區的類型完整或部分網址。例如:https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/diskTypes/pd-ssd
  3. 建立磁碟之後,請將其連接至任何執行中或已停止的 VM。使用 gcloud compute instances attach-disk 指令

    gcloud compute instances attach-disk VM_NAME \
      --disk DISK_NAME --device-name=DEVICE_NAME
    

    更改下列內容:

    • VM_NAME:要新增區域永久磁碟卷宗的 VM 名稱
    • DISK_NAME:要連結至 VM 的新磁碟名稱。
    • DEVICE_NAME:(選用) 客體 OS 用來識別磁碟的名稱。
  4. 使用 gcloud compute disks describe 指令查看磁碟說明。

Terraform

如要建立磁碟,請使用 google_compute_disk 資源

# Using pd-standard because it's the default for Compute Engine

resource "google_compute_disk" "default" {
  name = "disk-data"
  type = "pd-standard"
  zone = "us-west1-a"
  size = "5"
}

如要將磁碟連接至 VM,請使用 google_compute_instance 資源

resource "google_compute_instance" "test_node" {
  name         = "test-node"
  machine_type = "f1-micro"
  zone         = "us-west1-a"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }
  attached_disk {
    source      = google_compute_disk.default.id
    device_name = google_compute_disk.default.name
  }

  network_interface {
    network = "default"
    access_config {
      # Ephemeral IP
    }
  }

  # Ignore changes for persistent disk attachments
  lifecycle {
    ignore_changes = [attached_disk]
  }


}

如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。

REST

  1. 使用 disks.insert 方法建構 POST 要求,以建立區域永久磁碟。請包含 namesizeGbtype 屬性。如要將這個磁碟建立為空白且未格式化的非開機磁碟,請勿指定來源映像檔或來源快照。

    POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/disks
    
    {
     "name": "DISK_NAME",
     "sizeGb": "DISK_SIZE",
     "type": "DISK_TYPE"
    }
    

    更改下列內容:

    • PROJECT_ID:您的專案 ID。
    • ZONE:VM 和新磁碟所在的可用區。
    • DISK_NAME:新磁碟的名稱。
    • DISK_SIZE:新磁碟的大小 (以 GB 為單位)。可接受的大小範圍為 10 GB 到 65,536 GB (含首尾),調整的單位為 1 GB。
    • DISK_TYPE:永久磁碟類型的完整或部分網址。例如:https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/diskTypes/pd-ssd
  2. compute.instances.attachDisk 方法建構 POST 要求,並包含您剛建立的區域永久磁碟磁碟區的網址:

    POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/attachDisk
    
    {
     "source": "/compute/v1/projects/PROJECT_ID/zones/ZONE/disks/DISK_NAME",
     "deviceName": DEVICE_NAME
    }
    

    更改下列內容:

    • PROJECT_ID:您的專案 ID
    • ZONE:VM 和新磁碟所在的可用區
    • VM_NAME:要新增永久磁碟磁碟區的 VM 名稱
    • DISK_NAME:新磁碟的名稱
    • DEVICE_NAME:(選用) 客體 OS 用來識別磁碟的名稱。

建立新磁碟並連結至 VM 後,必須格式化並掛接磁碟,作業系統才能使用可用的儲存空間。

後續步驟