教學課程 - 使用 Azure CLI 管理 Azure 磁碟Tutorial - Manage Azure disks with the Azure CLI

Azure 虛擬機器 (VM) 使用磁碟來儲存作業系統、應用程式和資料。Azure virtual machines (VMs) use disks to store the operating system, applications, and data. 建立 VM 時,請務必選擇適合所預期工作負載的磁碟大小和組態。When you create a VM, it is important to choose a disk size and configuration appropriate to the expected workload. 本教學課程說明如何部署及管理 VM 磁碟。This tutorial shows you how to deploy and manage VM disks. 您將了解:You learn about:

  • OS 磁碟和暫存磁碟OS disks and temporary disks
  • 資料磁碟Data disks
  • 標準和進階磁碟Standard and Premium disks
  • 磁碟效能Disk performance
  • 連結及準備資料磁碟Attaching and preparing data disks
  • 調整磁碟的大小Resizing disks
  • 磁碟快照集Disk snapshots

預設 Azure 磁碟Default Azure disks

建立 Azure 虛擬機器後,有兩個磁碟會自動連結到虛擬機器。When an Azure virtual machine is created, two disks are automatically attached to the virtual machine.

作業系統磁碟 - 作業系統磁碟可裝載 VM 作業系統,其大小可以高達 2 TB。Operating system disk - Operating system disks can be sized up to 2 TB, and hosts the VMs operating system. OS 磁碟預設會標示為 /dev/sda。The OS disk is labeled /dev/sda by default. OS 磁碟的磁碟快取組態已針對 OS 效能進行最佳化。The disk caching configuration of the OS disk is optimized for OS performance. 因為此組態,OS 磁碟不得用於應用程式或資料。Because of this configuration, the OS disk should not be used for applications or data. 請對應用程式和資料使用資料磁碟,本教學課程稍後會詳細說明。For applications and data, use data disks, which are detailed later in this tutorial.

暫存磁碟 - 暫存磁碟會使用與 VM 位於相同 Azure 主機的固態磁碟機。Temporary disk - Temporary disks use a solid-state drive that is located on the same Azure host as the VM. 暫存磁碟的效能非常好,可用於暫存資料處理等作業。Temp disks are highly performant and may be used for operations such as temporary data processing. 不過,如果 VM 移至新的主機,則會移除儲存在暫存磁碟上的任何資料。However, if the VM is moved to a new host, any data stored on a temporary disk is removed. 暫存磁碟的大小取決於 VM 大小。The size of the temporary disk is determined by the VM size. 暫存磁碟會標示為 /dev/sdb,其掛接點為 /mnt。Temporary disks are labeled /dev/sdb and have a mountpoint of /mnt.

Azure 資料磁碟Azure data disks

若要安裝應用程式和儲存資料,可以新增額外的資料磁碟。To install applications and store data, additional data disks can be added. 資料磁碟應使用於任何需要持久且有回應之資料儲存體的情況。Data disks should be used in any situation where durable and responsive data storage is desired. 虛擬機器的大小會決定可連結到 VM 的資料磁碟數目。The size of the virtual machine determines how many data disks can be attached to a VM. 每個 VM vCPU 可以連結四個資料磁碟。For each VM vCPU, four data disks can be attached.

VM 磁碟類型VM disk types

Azure 提供兩種類型的磁碟,分別是標準和進階。Azure provides two types of disks, standard and Premium.

標準磁碟Standard disk

標準儲存體是以 HDD 為後盾,既可提供符合成本效益的儲存體,又可保有效能。Standard Storage is backed by HDDs, and delivers cost-effective storage while still being performant. 標準磁碟適合用於具成本效益的開發和測試工作負載。Standard disks are ideal for a cost effective dev and test workload.

進階磁碟Premium disk

進階磁碟是以 SSD 為基礎的高效能、低延遲磁碟為後盾。Premium disks are backed by SSD-based high-performance, low-latency disk. 最適合用於執行生產工作負載的 VM。Perfect for VMs running production workload. 進階儲存體支援 DS 系列、DSv2 系列、GS 系列和 FS 系列 VM。Premium Storage supports DS-series, DSv2-series, GS-series, and FS-series VMs. 當您選取磁碟大小時,其值會上調為下一個類型。When you select a disk size, the value is rounded up to the next type. 例如,如果磁碟大小少於 128 GB,則磁碟類型為 P10。For example, if the disk size is less than 128 GB, the disk type is P10. 如果磁碟大小介於 129 GB 與 512 GB 之間,則大小為 P20。If the disk size is between 129 GB and 512 GB, the size is a P20. 如果超過 512 GB,則大小為 P30。Over, 512 GB, the size is a P30.

進階磁碟效能Premium disk performance

進階 SSD 的大小Premium SSD sizes  P4P4 P6P6 P10P10 P15P15 P20P20 P30P30 P40P40 P50P50 P60P60 P70P70 P80P80
磁碟大小 (以 GiB 為單位)Disk size in GiB 3232 6464 128128 256256 512512 1,0241,024 2,0482,048 4,0964,096 8,1928,192 16,38416,384 32,76732,767
每一磁碟的 IOPSIOPS per disk 最高 120Up to 120 最高 240Up to 240 最多 500Up to 500 最高 1,100Up to 1,100 最高 2,300Up to 2,300 最高 5,000Up to 5,000 最高 7,500Up to 7,500 最高 7,500Up to 7,500 最高 16,000Up to 16,000 最高 18,000Up to 18,000 最高 20,000Up to 20,000
每一磁碟的輸送量Throughput per disk 最高 25 MiB/秒Up to 25 MiB/sec 最高 50 MiB/秒Up to 50 MiB/sec 最高 100 MiB/秒Up to 100 MiB/sec 最高 125 MiB/秒Up to 125 MiB/sec 最高 150 MiB/秒Up to 150 MiB/sec 最高 200 MiB/秒Up to 200 MiB/sec 最高 250 MiB/秒Up to 250 MiB/sec 最高 250 MiB/秒Up to 250 MiB/sec 最高 500 MiB/秒Up to 500 MiB/sec 最高 750 MiB/秒Up to 750 MiB/sec 最高 900 MiB/秒Up to 900 MiB/sec

雖然上表指出每個磁碟的最大 IOPS,但可藉由分割多個資料磁碟來達到較高等級的效能。While the above table identifies max IOPS per disk, a higher level of performance can be achieved by striping multiple data disks. 例如,Standard_GS5 VM 最高可達到 80,000 IOPS。For instance, a Standard_GS5 VM can achieve a maximum of 80,000 IOPS. 如需每部 VM 的最大 IOPS 詳細資訊,請參閱 Linux VM 大小For detailed information on max IOPS per VM, see Linux VM sizes.

啟動 Azure Cloud ShellLaunch Azure Cloud Shell

Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. 它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。It has common Azure tools preinstalled and configured to use with your account.

若要開啟 Cloud Shell,請選取程式碼區塊右上角的 [試試看]。To open Cloud Shell, select Try it from the upper right corner of a code block. 您也可以移至 https://shell.azure.com/powershell,從另一個瀏覽器索引標籤啟動 Cloud Shell。You can also launch Cloud Shell in a separate browser tab by going to https://shell.azure.com/powershell. 選取 [複製] 即可複製程式碼區塊,將它貼到 Cloud Shell 中,然後按 enter 鍵加以執行。Select Copy to copy the blocks of code, paste it into the Cloud Shell, and press enter to run it.

建立和連結磁碟Create and attach disks

您可以建立資料磁碟並在建立 VM 時連結,或連結至現有的 VM。Data disks can be created and attached at VM creation time or to an existing VM.

在建立 VM 時連結磁碟Attach disk at VM creation

使用 az group create 命令來建立資源群組。Create a resource group with the az group create command.

az group create --name myResourceGroupDisk --location eastus

使用 az vm create 命令來建立 VM。Create a VM using the az vm create command. 下列範例會建立名為 myVM 的 VM,新增名為 azureuser 的使用者帳戶,並產生 SSH 金鑰 (如果沒有這些金鑰的話)。The following example creates a VM named myVM, adds a user account named azureuser, and generates SSH keys if they do not exist. --datadisk-sizes-gb 引數用來指定應該建立一個額外的磁碟並連結至虛擬機器。The --datadisk-sizes-gb argument is used to specify that an additional disk should be created and attached to the virtual machine. 若要建立並連結多個磁碟,請使用以空格分隔的磁碟大小值清單。To create and attach more than one disk, use a space-delimited list of disk size values. 在下列範例中,會建立具有兩個資料磁碟 (均為 128 GB) 的 VM。In the following example, a VM is created with two data disks, both 128 GB. 因為磁碟大小是 128 GB,所以這些磁碟都會設為 P10,其可提供每個磁碟最高 500 IOPS。Because the disk sizes are 128 GB, these disks are both configured as P10s, which provide maximum 500 IOPS per disk.

az vm create \
  --resource-group myResourceGroupDisk \
  --name myVM \
  --image UbuntuLTS \
  --size Standard_DS2_v2 \
  --generate-ssh-keys \
  --data-disk-sizes-gb 128 128

將磁碟連結至現有的 VMAttach disk to existing VM

若要建立新的磁碟並將它連結至現有的虛擬機器,請使用 az vm disk attach 命令。To create and attach a new disk to an existing virtual machine, use the az vm disk attach command. 下列範例會建立進階磁碟 (大小為 128 GB),並將它連結至最後一個步驟中建立的 VM。The following example creates a premium disk, 128 gigabytes in size, and attaches it to the VM created in the last step.

az vm disk attach \
    --resource-group myResourceGroupDisk \
    --vm-name myVM \
    --disk myDataDisk \
    --size-gb 128 \
    --sku Premium_LRS \
    --new

準備資料磁碟Prepare data disks

將磁碟連結到虛擬機器後,必須將作業系統設定為使用該磁碟。Once a disk has been attached to the virtual machine, the operating system needs to be configured to use the disk. 下列範例示範如何手動設定磁碟。The following example shows how to manually configure a disk. 使用 cloud-init (涵蓋於稍後的教學課程中) 也可以將此程序自動化。This process can also be automated using cloud-init, which is covered in a later tutorial.

建立虛擬機器的 SSH 連線。Create an SSH connection with the virtual machine. 以虛擬機器的公用 IP 位址取代範例 IP 位址。Replace the example IP address with the public IP of the virtual machine.

ssh 10.101.10.10

使用 fdisk 分割磁碟。Partition the disk with fdisk.

(echo n; echo p; echo 1; echo ; echo ; echo w) | sudo fdisk /dev/sdc

使用 mkfs 命令將檔案系統寫入至磁碟分割。Write a file system to the partition by using the mkfs command.

sudo mkfs -t ext4 /dev/sdc1

掛接新磁碟,使其可在作業系統中存取。Mount the new disk so that it is accessible in the operating system.

sudo mkdir /datadrive && sudo mount /dev/sdc1 /datadrive

現在可以透過 datadrive 掛接點存取磁碟,而執行 df -h 命令可驗證此掛接點。The disk can now be accessed through the datadrive mountpoint, which can be verified by running the df -h command.

df -h

輸出會顯示掛接在 /datadrive 上的新磁碟機。The output shows the new drive mounted on /datadrive.

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        30G  1.4G   28G   5% /
/dev/sdb1       6.8G   16M  6.4G   1% /mnt
/dev/sdc1        50G   52M   47G   1% /datadrive

為了確保磁碟機會在重新開機之後重新掛接,必須將磁碟機新增至 /etc/fstab 檔案。To ensure that the drive is remounted after a reboot, it must be added to the /etc/fstab file. 若要這麼做,請使用 blkid 公用程式取得磁碟的 UUID。To do so, get the UUID of the disk with the blkid utility.

sudo -i blkid

輸出會顯示磁碟機的 UUID,在此情況下為 /dev/sdc1The output displays the UUID of the drive, /dev/sdc1 in this case.

/dev/sdc1: UUID="33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e" TYPE="ext4"

將類似以下的一行新增至 /etc/fstab 檔案。Add a line similar to the following to the /etc/fstab file.

UUID=33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e   /datadrive  ext4    defaults,nofail   1  2

現在已設定磁碟,請關閉 SSH 工作階段。Now that the disk has been configured, close the SSH session.

exit

擷取磁碟快照集Take a disk snapshot

當您建立磁碟快照集時,Azure 會建立磁碟的唯讀、時間點複本。When you take a disk snapshot, Azure creates a read only, point-in-time copy of the disk. 進行組態變更之前,Azure VM 快照集可用於快速儲存 VM 的狀態。Azure VM snapshots are useful to quickly save the state of a VM before you make configuration changes. 發生問題或錯誤時,便可使用快照集來還原 VM。In the event of an issue or error, VM can be restored using a snapshot. 當 VM 有多個磁碟時,每個磁碟會各自產生快照集。When a VM has more than one disk, a snapshot is taken of each disk independently of the others. 若要進行應用程式一致備份,請考慮在建立磁碟快照集之前停止 VM。To take application consistent backups, consider stopping the VM before you take disk snapshots. 或者,使用 Azure 備份服務,其可讓您在 VM 執行時執行自動化備份。Alternatively, use the Azure Backup service, which enables you to perform automated backups while the VM is running.

建立快照集Create snapshot

建立虛擬機器磁碟快照集之前,需要磁碟的識別碼或名稱。Before you create a virtual machine disk snapshot, the ID or name of the disk is needed. 使用 az vm show 命令傳回磁碟識別碼。Use the az vm show command to return the disk ID. 在此範例中,磁碟識別碼會儲存在變數中,以便用於稍後的步驟。In this example, the disk ID is stored in a variable so that it can be used in a later step.

osdiskid=$(az vm show \
   -g myResourceGroupDisk \
   -n myVM \
   --query "storageProfile.osDisk.managedDisk.id" \
   -o tsv)

您現在有虛擬機器磁碟的識別碼,下列命令會建立磁碟的快照集。Now that you have the ID of the virtual machine disk, the following command creates a snapshot of the disk.

az snapshot create \
    --resource-group myResourceGroupDisk \
    --source "$osdiskid" \
    --name osDisk-backup

從快照集建立磁碟Create disk from snapshot

此快照集可以接著轉換成磁碟,進而用於重新建立虛擬機器。This snapshot can then be converted into a disk, which can be used to recreate the virtual machine.

az disk create \
   --resource-group myResourceGroupDisk \
   --name mySnapshotDisk \
   --source osDisk-backup

從快照集還原虛擬機器Restore virtual machine from snapshot

若要示範虛擬機器復原,請刪除現有的虛擬機器。To demonstrate virtual machine recovery, delete the existing virtual machine.

az vm delete \
--resource-group myResourceGroupDisk \
--name myVM

從快照磁碟建立新的虛擬機器。Create a new virtual machine from the snapshot disk.

az vm create \
    --resource-group myResourceGroupDisk \
    --name myVM \
    --attach-os-disk mySnapshotDisk \
    --os-type linux

重新連結資料磁碟Reattach data disk

所有資料磁碟都必須重新連結至虛擬機器。All data disks need to be reattached to the virtual machine.

首先使用 az disk list 命令尋找資料磁碟名稱。First find the data disk name using the az disk list command. 此範例會將磁碟名稱放入名為 datadisk 的變數,該變數使用於下一個步驟。This example places the name of the disk in a variable named datadisk, which is used in the next step.

datadisk=$(az disk list \
   -g myResourceGroupDisk \
   --query "[?contains(name,'myVM')].[id]" \
   -o tsv)

使用 az vm disk attach 命令來連結磁碟。Use the az vm disk attach command to attach the disk.

az vm disk attach \
   –g myResourceGroupDisk \
   --vm-name myVM \
   --disk $datadisk

後續步驟Next steps

在本教學課程中,您已了解 VM 磁碟的相關主題,像是:In this tutorial, you learned about VM disks topics such as:

  • OS 磁碟和暫存磁碟OS disks and temporary disks
  • 資料磁碟Data disks
  • 標準和進階磁碟Standard and Premium disks
  • 磁碟效能Disk performance
  • 連結及準備資料磁碟Attaching and preparing data disks
  • 調整磁碟的大小Resizing disks
  • 磁碟快照集Disk snapshots

請前進到下一個教學課程,以了解如何自動設定 VM。Advance to the next tutorial to learn about automating VM configuration.