教程:通过 Azure CLI 对虚拟机规模集创建和使用磁盘Tutorial: Create and use disks with virtual machine scale set with the Azure CLI

虚拟机规模集使用磁盘来存储 VM 实例的操作系统、应用程序和数据。Virtual machine scale sets use disks to store the VM instance's operating system, applications, and data. 创建和管理规模集时,请务必选择适用于所需工作负荷的磁盘大小和配置。As you create and manage a scale set, it is important to choose a disk size and configuration appropriate to the expected workload. 本教程介绍如何创建和管理 VM 磁盘。This tutorial covers how to create and manage VM disks. 本教程介绍如何执行下列操作:In this tutorial you learn how to:

  • OS 磁盘和临时磁盘OS disks and temporary disks
  • 数据磁盘数Data disks
  • 标准磁盘和高级磁盘Standard and Premium disks
  • 磁盘性能Disk performance
  • 附加和准备数据磁盘Attach and prepare data disks

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don’t have an Azure subscription, create a Trial before you begin.

如果选择在本地安装和使用 CLI,本教程要求运行 Azure CLI 2.0.29 或更高版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.0.29 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

创建或缩放规模集时,会自动将两个磁盘附加到每个 VM 实例。When a scale set is created or scaled, two disks are automatically attached to each VM instance.

操作系统磁盘 - 操作系统磁盘大小可达 2 TB,并可托管 VM 实例的操作系统。Operating system disk - Operating system disks can be sized up to 2 TB, and hosts the VM instance's 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 host applications or data. 对于应用程序和数据,请使用数据磁盘,本文后面会对其进行详细介绍。For applications and data, use data disks, which are detailed later in this article.

临时磁盘 - 临时磁盘使用 VM 实例所在的 Azure 主机上的固态硬盘。Temporary disk - Temporary disks use a solid-state drive that is located on the same Azure host as the VM instance. 这些磁盘具有高性能,可用于临时数据处理等操作。These are high-performance disks and may be used for operations such as temporary data processing. 但是,如果将 VM 实例移到新的主机,临时磁盘上存储的数据都会删除。However, if the VM instance 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 instance size. 临时磁盘标记为“/dev/sdb” ,且装载点为 /mnt 。Temporary disks are labeled /dev/sdb and have a mountpoint of /mnt.

临时磁盘大小Temporary disk sizes

类型Type 常见大小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 和 M 系列D, E, and M series 61446144
GPUGPU N 系列N series 14401440

Azure 数据磁盘Azure data disks

可添加额外的数据磁盘,用于安装应用程序和存储数据。Additional data disks can be added if you need to install applications and store data. 在任何需要持久和灵敏数据存储的情况下,都应使用数据磁盘。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 VM instance determines how many data disks can be attached. 可以为每个 VM vCPU 附加两个数据磁盘,直至达到每个虚拟机 64 个磁盘的绝对上限。For each VM vCPU, two data disks can be attached up to an absolute maximum of 64 disks per virtual machine.

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 and performance. 标准磁盘适用于经济高效的开发和测试工作负荷。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 使用这些磁盘。These disks are recommended for VMs that run production workloads. 高级存储支持 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

可以在创建规模集时创建和附加磁盘,也可以对现有的规模集创建和附加磁盘。You can create and attach disks when you create a scale set, or with an existing scale set.

创建规模集时附加磁盘Attach disks at scale set creation

首先,使用 az group create 命令创建资源组。First, create a resource group with the az group create command. 本示例在 chinanorth 区域中创建名为 myResourceGroup 的资源组。In this example, a resource group named myResourceGroup is created in the chinanorth region.

az group create --name myResourceGroup --location chinanorth

请使用 az vmss create 命令创建虚拟机规模集。Create a virtual machine scale set with the az vmss create command. 以下示例创建名为“myScaleSet” 的规模集,并生成 SSH 密钥(如果不存在)。The following example creates a scale set named myScaleSet, and generates SSH keys if they do not exist. 两个磁盘都是 --data-disk-sizes-gb 参数创建的。Two disks are created with the --data-disk-sizes-gb parameter. 第一个磁盘的大小为 64 GB,第二个磁盘的大小为 128 GB:The first disk is 64 GB in size, and the second disk is 128 GB:

az vmss create \
  --resource-group myResourceGroup \
  --name myScaleSet \
  --image UbuntuLTS \
  --upgrade-policy automatic \
  --admin-username azureuser \
  --generate-ssh-keys \
  --data-disk-sizes-gb 64 128

创建和配置所有的规模集资源和 VM 实例需要几分钟时间。It takes a few minutes to create and configure all the scale set resources and VM instances.

将磁盘附加到现有规模集Attach a disk to existing scale set

还可以将磁盘附加到现有的规模集。You can also attach disks to an existing scale set. 使用在上一步创建的规模集通过 az vmss disk attach 添加另一磁盘。Use the scale set created in the previous step to add another disk with az vmss disk attach. 以下示例附加另一个 128 GB 的磁盘:The following example attaches an additional 128 GB disk:

az vmss disk attach \
  --resource-group myResourceGroup \
  --name myScaleSet \
  --size-gb 128

准备数据磁盘Prepare the data disks

已创建并附加到规模集 VM 实例的磁盘是原始磁盘。The disks that are created and attached to your scale set VM instances are raw disks. 将磁盘用于数据和应用程序之前,必须准备磁盘。Before you can use them with your data and applications, the disks must be prepared. 若要准备磁盘,需要创建分区、创建文件系统,并将其装载。To prepare the disks, you create a partition, create a filesystem, and mount them.

若要跨规模集中的多个 VM 实例自动完成此过程,可以使用 Azure 自定义脚本扩展。To automate the process across multiple VM instances in a scale set, you can use the Azure Custom Script Extension. 此扩展可以在每个 VM 实例上以本地方式执行脚本,以便完成各种任务,例如准备附加的数据磁盘。This extension can execute scripts locally on each VM instance, such as to prepare attached data disks. 有关详细信息,请参阅自定义脚本扩展概述For more information, see the Custom Script Extension overview.

以下示例在每个 VM 实例上执行来自 GitHub 示例存储库的脚本,使用的是 az vmss extension set 命令,该命令用于准备所有原始的附加数据磁盘:The following example executes a script from a GitHub sample repo on each VM instance with az vmss extension set that prepares all the raw attached data disks:

az vmss extension set \
  --publisher Microsoft.Azure.Extensions \
  --version 2.0 \
  --name CustomScript \
  --resource-group myResourceGroup \
  --vmss-name myScaleSet \
  --settings '{"fileUris":["https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/prepare_vm_disks.sh"],"commandToExecute":"./prepare_vm_disks.sh"}'

若要确认磁盘是否已正确地准备好,请通过 SSH 连接到某个 VM 实例。To confirm that the disks have been prepared correctly, SSH to one of the VM instances. 使用 az vmss list-instance-connection-info 列出规模集的连接信息:List the connection information for your scale set with az vmss list-instance-connection-info:

az vmss list-instance-connection-info \
  --resource-group myResourceGroup \
  --name myScaleSet

使用自己的公共 IP 地址和端口号连接到第一个 VM 实例,如以下示例所示:Use your own public IP address and port number to connect to the first VM instance, as shown in the following example:

ssh azureuser@52.226.67.166 -p 50001

检查 VM 实例上的分区,如下所示:Examine the partitions on the VM instance as follows:

sudo fdisk -l

以下示例输出显示附加到 VM 实例的有三个磁盘 - /dev/sdc/dev/sdd/dev/sdeThe following example output shows that three disks are attached to the VM instance - /dev/sdc, /dev/sdd, and /dev/sde. 每个这样的磁盘都是单个分区使用所有的可用空间:Each of those disks has a single partition that uses all the available space:

Disk /dev/sdc: 64 GiB, 68719476736 bytes, 134217728 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xa47874cb

Device     Boot Start       End   Sectors Size Id Type
/dev/sdc1        2048 134217727 134215680  64G 83 Linux

Disk /dev/sdd: 128 GiB, 137438953472 bytes, 268435456 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xd5c95ca0

Device     Boot Start       End   Sectors  Size Id Type
/dev/sdd1        2048 268435455 268433408  128G 83 Linux

Disk /dev/sde: 128 GiB, 137438953472 bytes, 268435456 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x9312fdf5

Device     Boot Start       End   Sectors  Size Id Type
/dev/sde1        2048 268435455 268433408  128G 83 Linux

检查 VM 实例上的文件系统和装载点,如下所示:Examine the filesystems and mount points on the VM instance as follows:

sudo df -h

以下示例输出表明,三个磁盘已将其文件系统正确地装载在 /datadisks 下:The following example output shows that the three disks have their filesystems correctly mounted under /datadisks:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        30G  1.3G   28G   5% /
/dev/sdb1        50G   52M   47G   1% /mnt
/dev/sdc1        63G   52M   60G   1% /datadisks/disk1
/dev/sdd1       126G   60M  120G   1% /datadisks/disk2
/dev/sde1       126G   60M  120G   1% /datadisks/disk3

规模集中每个 VM 实例上的磁盘是以同一方式自动准备的。The disks on each VM instance in your scale are automatically prepared in the same way. 规模集进行纵向扩展时,所需数据磁盘会附加到新的 VM 实例。As your scale set would scale up, the required data disks are attached to the new VM instances. 自定义脚本扩展也会运行,以便自动准备磁盘。The Custom Script Extension also runs to automatically prepare the disks.

关闭与 VM 实例的 SSH 连接:Close the SSH connection to your VM instance:

exit

列出附加的磁盘List attached disks

若要查看附加到规模集的磁盘的相关信息,请使用 az vmss show 命令并对 virtualMachineProfile.storageProfile.dataDisks 进行查询:To view information about disks attached to a scale set, use az vmss show and query on virtualMachineProfile.storageProfile.dataDisks:

az vmss show \
  --resource-group myResourceGroup \
  --name myScaleSet \
  --query virtualMachineProfile.storageProfile.dataDisks

还会显示有关磁盘大小、存储层和 LUN(逻辑单元号)的信息。Information on the disk size, storage tier, and LUN (Logical Unit Number) is shown. 以下示例输出显示了有关三个附加到规模集的数据磁盘的详细信息:The following example output details the three data disks attached to the scale set:

[
  {
    "additionalProperties": {},
    "caching": "None",
    "createOption": "Empty",
    "diskSizeGb": 64,
    "lun": 0,
    "managedDisk": {
      "additionalProperties": {},
      "storageAccountType": "Standard_LRS"
    },
    "name": null
  },
  {
    "additionalProperties": {},
    "caching": "None",
    "createOption": "Empty",
    "diskSizeGb": 128,
    "lun": 1,
    "managedDisk": {
      "additionalProperties": {},
      "storageAccountType": "Standard_LRS"
    },
    "name": null
  },
  {
    "additionalProperties": {},
    "caching": "None",
    "createOption": "Empty",
    "diskSizeGb": 128,
    "lun": 2,
    "managedDisk": {
      "additionalProperties": {},
      "storageAccountType": "Standard_LRS"
    },
    "name": null
  }
]

分离磁盘Detach a disk

不再需要某个给定的磁盘时,可以将其从规模集中分离。When you no longer need a given disk, you can detach it from the scale set. 该磁盘会从规模集的所有 VM 实例中删除。The disk is removed from all VM instances in the scale set. 若要从规模集中分离某个磁盘,请使用 az vmss disk detach 并指定磁盘的 LUN。To detach a disk from a scale set, use az vmss disk detach and specify the LUN of the disk. LUN 显示在上一部分的 az vmss show 命令的输出中。The LUNs are shown in the output from az vmss show in the previous section. 以下示例从规模集分离 LUN 2The following example detaches LUN 2 from the scale set:

az vmss disk detach \
  --resource-group myResourceGroup \
  --name myScaleSet \
  --lun 2

清理资源Clean up resources

若要删除规模集和磁盘,请使用 az group delete 删除资源组及其所有资源。To remove your scale set and disks, delete the resource group and all its resources with az group delete. --no-wait 参数会使光标返回提示符处,不会等待操作完成。The --no-wait parameter returns control to the prompt without waiting for the operation to complete. --yes 参数将确认是否希望删除资源,不会显示询问是否删除的额外提示。The --yes parameter confirms that you wish to delete the resources without an additional prompt to do so.

az group delete --name myResourceGroup --no-wait --yes

后续步骤Next steps

本教程介绍了如何通过 Azure CLI 创建和使用规模集的磁盘:In this tutorial, you learned how to create and use disks with scale sets with the Azure CLI:

  • OS 磁盘和临时磁盘OS disks and temporary disks
  • 数据磁盘数Data disks
  • 标准磁盘和高级磁盘Standard and Premium disks
  • 磁盘性能Disk performance
  • 附加和准备数据磁盘Attach and prepare data disks

请继续学习下一教程,了解如何对规模集 VM 实例使用自定义映像。Advance to the next tutorial to learn how to use a custom image for your scale set VM instances.