教程 - 使用 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
  • 磁盘快照Disk snapshots

默认 Azure 磁盘Default Azure disks

创建 Azure 虚拟机后,将自动向此虚拟机附加两个磁盘。When an Azure virtual machine is created, two disks are automatically attached to the virtual machine.

操作系统磁盘 - 操作系统磁盘大小可达 2 TB,并可托管 VM 操作系统。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 磁盘类型VM disk types

Azure 提供两种类型的磁盘。Azure provides two types of disks.

标准磁盘 - 受 HDD 支持,可以在确保性能的同时提供经济高效的存储。Standard disks - backed by HDDs, and delivers cost-effective storage while still being performant. 标准磁盘适用于经济高效的开发和测试工作负荷。Standard disks are ideal for a cost effective dev and test workload.

高级磁盘 - 由基于 SSD 的高性能、低延迟磁盘提供支持。Premium disks - backed by SSD-based, high-performance, low-latency disk. 完美适用于运行生产工作负荷的 VM。Perfect for VMs running production workload. 大小名称中带有“S”的 VM 大小通常支持高级存储。VM sizes with an S in the size name, typically support Premium Storage. 例如,DS 系列、DSv2 系列和 FS 系列 VM 都支持高级存储。For example, DS-series, DSv2-series, and FS-series VMs support premium storage. 选择磁盘大小时,大小值将向上舍入到下一类型。When you select a disk size, the value is rounded up to the next type. 例如,如果磁盘大小大于 64 GB,但小于 128 GB,则磁盘类型为 P10。For example, if the disk size is more than 64 GB, but less than 128 GB, the disk type is P10.


高级 SSD 大小Premium SSD sizes  P1P1 P2P2 P3P3 P4P4 P6P6 P10P10 P15P15 P20P20 P30P30 P40P40 P50P50 P60P60 P70P70 P80P80
磁盘大小 (GiB)Disk size in GiB 44 88 1616 3232 6464 128128 256256 512512 1,0241,024 2,0482,048 4,0964,096 8,1928,192 16,38416,384 32,76732,767
每个磁盘预配的 IOPSProvisioned IOPS per disk 120120 120120 120120 120120 240240 500500 1,1001,100 2,3002,300 5,0005,000 7,5007,500 7,5007,500 16,00016,000 18,00018,000 20,00020,000
每个磁盘预配的吞吐量Provisioned Throughput per disk 25 MB/秒25 MB/sec 25 MB/秒25 MB/sec 25 MB/秒25 MB/sec 25 MB/秒25 MB/sec 50 MB/秒50 MB/sec 100 MB/秒100 MB/sec 125 MB/秒125 MB/sec 150 MB/秒150 MB/sec 200 MB/秒200 MB/sec 250 MB/秒250 MB/sec 250 MB/秒250 MB/sec 500 MB/秒500 MB/sec 750 MB/秒750 MB/sec 900 MB/秒900 MB/sec
每个磁盘最大的突发 IOPSMax burst IOPS per disk 3,5003,500 3,5003,500 3,5003,500 3,5003,500 3,5003,500 3,5003,500 3,5003,500 3,5003,500
每个磁盘最大的突发吞吐量Max burst throughput per disk 170 MB/秒170 MB/sec 170 MB/秒170 MB/sec 170 MB/秒170 MB/sec 170 MB/秒170 MB/sec 170 MB/秒170 MB/sec 170 MB/秒170 MB/sec 170 MB/秒170 MB/sec 170 MB/秒170 MB/sec
最大突发持续时间Max burst duration 30 分钟30 min 30 分钟30 min 30 分钟30 min 30 分钟30 min 30 分钟30 min 30 分钟30 min 30 分钟30 min 30 分钟30 min
符合预留条件Eligible for reservation No No No No No No No No 是,最多一年Yes, up to one year 是,最多一年Yes, up to one year 是,最多一年Yes, up to one year 是,最多一年Yes, up to one year 是,最多一年Yes, up to one year 是,最多一年Yes, up to one year

预配高级存储磁盘时,可以获得该磁盘的容量、IOPS 和吞吐量保证,这与标准存储不同。When you provision a premium storage disk, unlike standard storage, you are guaranteed the capacity, IOPS, and throughput of that disk. 例如,如果创建 P50 磁盘,Azure 将为此磁盘预配 4,095-GB 存储容量、7,500 IOPS 和 250-MB/秒的吞吐量。For example, if you create a P50 disk, Azure provisions 4,095-GB storage capacity, 7,500 IOPS, and 250-MB/s throughput for that disk. 应用程序可以使用全部或部分容量与性能。Your application can use all or part of the capacity and performance. 高级 SSD 磁盘的设计目的是在 99.9% 的时间内提供较低的个位数毫秒延迟以及上表所述的目标 IOPS 和吞吐量。Premium SSD disks are designed to provide low single-digit millisecond latencies and target IOPS and throughput described in the preceding table 99.9% of the time.

尽管上表确定了每个磁盘的最大 IOPS,但还可通过条带化多个数据磁盘实现更高级别的性能。While the above table identifies max IOPS per disk, a higher level of performance can be achieved by striping multiple data disks. 若要详细了解每个 VM 的最大 IOPS,请参阅 VM 类型和大小For detailed information on max IOPS per VM, see VM types and sizes.

启动 Azure CLILaunch Azure CLI

可以在本地电脑上使用 Azure 本地 Shell 运行本文中的步骤。You can use Azure local Shell on your local PC to run the steps in this article.

备注

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

创建并附加磁盘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 chinaeast2

使用 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. 在以下示例中,创建的 VM 具有两个均为 128 GB 的数据磁盘。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 \
    --name 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

使用 parted 对磁盘进行分区。Partition the disk with parted.

sudo parted /dev/sdc --script mklabel gpt mkpart xfspart xfs 0% 100%

使用 mkfs 命令将文件系统写入分区。Write a file system to the partition by using the mkfs command. 使用 partprobe 使 OS 知道所做的更改。Use partprobe to make the OS aware of the change.

sudo mkfs.xfs /dev/sdc1
sudo partprobe /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 | grep -i "sd"

输出显示新驱动器装载在 /datadrive 上。The output shows the new drive mounted on /datadrive.

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        29G  2.0G   27G   7% /
/dev/sda15      105M  3.6M  101M   4% /boot/efi
/dev/sdb1        14G   41M   13G   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="xfs"

备注

错误地编辑 /etc/fstab 文件可能会导致系统无法引导。Improperly editing the /etc/fstab file could result in an unbootable system. 如果没有把握,请参考分发的文档来获取有关如何正确编辑该文件的信息。If unsure, refer to the distribution's documentation for information on how to properly edit this file. 另外,建议在编辑前备份 /etc/fstab 文件。It is also recommended that a backup of the /etc/fstab file is created before editing.

在文本编辑器中打开 /etc/fstab 文件,如下所示:Open the /etc/fstab file in a text editor as follows:

sudo nano /etc/fstab

向 /etc/fstab 文件中添加如下一行,并将 UUID 值替换为你自己的值。Add a line similar to the following to the /etc/fstab file, replacing the UUID value with your own.

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

编辑完文件后,使用 Ctrl+O 写入文件,然后使用 Ctrl+X 退出编辑器。When you are done editing the file, use Ctrl+O to write the file and Ctrl+X to exit the editor.

配置磁盘后,请关闭 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

创建快照之前,需要磁盘的 ID 或名称。Before you create a snapshot, you need the ID or name of the disk. 使用 az vm show 显示磁盘 ID。Use az vm show to shot the disk ID. 在此示例中,磁盘 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)

有了 ID 后,请使用 az snapshot create 创建该磁盘的快照。Now that you have the ID, use az snapshot create to create a snapshot of the disk.

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

从快照创建磁盘Create disk from snapshot

然后可以使用 az disk create 将此快照转换为可用于重新创建虚拟机的磁盘。This snapshot can then be converted into a disk using az disk create, 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

若要演示如何还原虚拟机,请使用 az vm delete 删除现有虚拟机。To demonstrate virtual machine recovery, delete the existing virtual machine using az vm delete.

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 命令找到数据磁盘名称。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 \
   --name $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
  • 磁盘快照Disk snapshots

转到下一教程,了解如何自动配置 VM。Advance to the next tutorial to learn about automating VM configuration.