您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程 - 使用 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 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 Cloud ShellOpen 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. Cloud Shell 中预安装并配置了常用 Azure 工具供你与帐户一起使用。Common Azure tools are preinstalled and configured in Cloud Shell for you to use with your account. 只需选择“复制”按钮即可复制代码,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。Just select the Copy button to copy the code, paste it in Cloud Shell, and then press Enter to run it. 可通过多种方式打开 Cloud Shell:There are a few ways to open Cloud Shell:

选择代码块右上角的“试用”。Select Try It in the upper-right corner of a code block. 本文中的 Cloud Shell
在浏览器中打开 Cloud Shell。Open Cloud Shell in your browser. https://shell.azure.com/bash
选择 Azure 门户右上角菜单上的“Cloud Shell”按钮。Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal. 门户中的 Cloud Shell

如果选择在本地安装并使用 CLI,本教程要求运行 Azure CLI 2.0.30 或更高版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.0.30 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

默认 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.

临时磁盘大小Temporary disk sizes

TypeType 常见大小Common sizes 临时磁盘大小上限 (GiB)Max temp disk size (GiB)
常规用途General purpose A、B、D 系列A, B, and D series 16001600
计算优化Compute optimized F 系列F series 576576
内存优化Memory optimized D、E、G、M 系列D, E, G, and M series 61446144
存储优化Storage optimized L 系列L series 56305630
GPUGPU N 系列N series 14401440
高性能High performance A 和 H 系列A and H series 20002000

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. 每个数据磁盘的最大容量为 4 TB。Each data disk has a maximum capacity of 4 TB. 虚拟机的大小决定可附加到 VM 的数据磁盘数。The size of the virtual machine determines how many data disks can be attached to a VM. 对于每个 VM vCPU,都可以附加两个数据磁盘。For each VM vCPU, two data disks can be attached.

每个 VM 的最大数据磁盘数Max data disks per VM

TypeType VM 大小VM Size 每个 VM 的最大数据磁盘数Max data disks per VM
常规用途General purpose A、B、D 系列A, B, and D series 6464
计算优化Compute optimized F 系列F series 6464
内存优化Memory optimized D、E 和 G 系列D, E, and G series 6464
存储优化Storage optimized L 系列L series 6464
GPUGPU N 系列N series 6464
高性能High performance A 和 H 系列A and H series 6464

VM 磁盘类型VM disk types

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

标准磁盘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

高级存储磁盘类型Premium storage disk type P4P4 P6P6 P10P10 P20P20 P30P30 P40P40 P50P50
磁盘大小(向上舍入)Disk size (round up) 32 GB32 GB 64 GB64 GB 128 GB128 GB 512 GB512 GB 1,024 GB (1 TB)1,024 GB (1 TB) 2,048 GB (2 TB)2,048 GB (2 TB) 4,095 GB (4 TB)4,095 GB (4 TB)
每个磁盘的最大 IOPSMax IOPS per disk 120120 240240 500500 2,3002,300 5,0005,000 7,5007,500 7,5007,500
每个磁盘的吞吐量Throughput per disk 25 MB/秒25 MB/s 50 MB/秒50 MB/s 100 MB/秒100 MB/s 150 MB/秒150 MB/s 200 MB/秒200 MB/s 250 MB/秒250 MB/s 250 MB/秒250 MB/s

尽管上表确定了每个磁盘的最大 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.

创建并附加磁盘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. 在以下示例中,创建的 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 \
  --admin-username azureuser \
  --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.

手动配置Manual configuration

创建与虚拟机的 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 azureuser@52.174.34.95

使用 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

调整 VM 磁盘大小Resize VM disk

部署 VM 后,可增加操作系统磁盘或任何附加数据磁盘的大小。Once a VM has been deployed, the operating system disk or any attached data disks can be increased in size. 需要更多存储空间或更高级别的性能(如 P10、P20 或 P30)时,增加磁盘大小很有用。Increasing the size of a disk is beneficial when you need more storage space or a higher level of performance (such as a P10, P20, or P30). 不能降低磁盘大小。Disks cannot be decreased in size.

增加磁盘大小之前,需要磁盘 ID 或名称。Before you increase disk size, the ID or name of the disk is needed. 使用 az disk list 命令返回资源组中的所有磁盘。Use the az disk list command to return all disks in a resource group. 记下要调整大小的磁盘名称。Take note of the disk name that you would like to resize.

az disk list \
    --resource-group myResourceGroupDisk \
    --query '[*].{Name:name,Gb:diskSizeGb,Tier:accountType}' \
    --output table

必须解除分配 VM。The VM must be deallocated. 使用 az vm deallocate 命令停止和解除分配 VM。Use the az vm deallocate command to stop and deallocate the VM.

az vm deallocate --resource-group myResourceGroupDisk --name myVM

使用 az disk update 命令调整磁盘大小。Use the az disk update command to resize the disk. 本示例将名为“myDataDisk”的磁盘的大小调整为 1 TB。This example resizes a disk named myDataDisk to 1 terabyte.

az disk update --name myDataDisk --resource-group myResourceGroupDisk --size-gb 1023

完成调整大小操作后,启动 VM。Once the resize operation has completed, start the VM.

az vm start --resource-group myResourceGroupDisk --name myVM

如果调整操作系统磁盘的大小,则会自动扩展分区。If you resize the operating system disk, the partition is automatically expanded. 如果调整数据磁盘的大小,则必须在 VM 操作系统中扩展任何当前分区。If you resize a data disk, any current partitions must be expanded in the VM's operating system.

拍摄 Azure 磁盘快照Snapshot Azure disks

创建磁盘快照时,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 the configuration changes prove to be undesired, VM state can be restored using the 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 virtual machine disk snapshot, the ID or name of the disk is needed. 使用 az vm show 命令返回磁盘 ID。Use the az vm show command to return 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 后,使用以下命令可创建磁盘快照。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')].[name]" -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.