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

虚拟机规模集使用磁盘来存储 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.

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

默认 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,都可以附加两个数据磁盘。For each VM vCPU, two data disks can be attached.

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

类型Type 常见大小Common sizes 每个 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 和 M 系列D, E, and M series 6464
GPUGPU N 系列N 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 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 disks. 建议对运行生产工作负荷的 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,请参阅 Windows VM 大小For detailed information on max IOPS per VM, see Windows 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

使用 New-AzVmss 创建虚拟机规模集。Create a virtual machine scale set with New-AzVmss. 出现提示时,请提供 VM 实例的用户名和密码。When prompted, provide a username and password for the VM instances. 若要将流量分配到单独的 VM 实例,则还要创建负载均衡器。To distribute traffic to the individual VM instances, a load balancer is also created. 负载均衡器包含的规则可在 TCP 端口 80 上分配流量,并允许 TCP 端口 3389 上的远程桌面流量,以及 TCP 端口 5985 上的 PowerShell 远程流量。The load balancer includes rules to distribute traffic on TCP port 80, as well as allow remote desktop traffic on TCP port 3389 and PowerShell remoting on TCP port 5985.

两个磁盘都是 -DataDiskSizeGb 参数创建的。Two disks are created with the -DataDiskSizeGb parameter. 第一个磁盘的大小为 64 GB,第二个磁盘的大小为 128 GB。The first disk is 64 GB in size, and the second disk is 128 GB. 出现提示时,请针对规模集中的 VM 实例提供自己的所需管理凭据:When prompted, provide your own desired administrative credentials for the VM instances in the scale set:

New-AzVmss `
  -ResourceGroupName "myResourceGroup" `
  -Location "ChinaNorth" `
  -VMScaleSetName "myScaleSet" `
  -VirtualNetworkName "myVnet" `
  -SubnetName "mySubnet" `
  -PublicIpAddressName "myPublicIPAddress" `
  -LoadBalancerName "myLoadBalancer" `
  -UpgradePolicyMode "Automatic" `
  -DataDiskSizeInGb 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. 使用在上一步创建的规模集通过 Add-AzVmssDataDisk 添加另一磁盘。Use the scale set created in the previous step to add another disk with Add-AzVmssDataDisk. 以下示例将另一个 128 GB 的磁盘附加到现有规模集:The following example attaches an additional 128 GB disk to an existing scale set:

# Get scale set object
$vmss = Get-AzVmss `
          -ResourceGroupName "myResourceGroup" `
          -VMScaleSetName "myScaleSet"

# Attach a 128 GB data disk to LUN 2
Add-AzVmssDataDisk `
  -VirtualMachineScaleSet $vmss `
  -CreateOption Empty `
  -Lun 2 `
  -DiskSizeGB 128

# Update the scale set to apply the change
Update-AzVmss `
  -ResourceGroupName "myResourceGroup" `
  -Name "myScaleSet" `
  -VirtualMachineScaleSet $vmss

准备数据磁盘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 示例存储库的脚本,使用的是 Add-AzVmssExtension 命令,该命令用于准备所有原始的附加数据磁盘:The following example executes a script from a GitHub sample repo on each VM instance with Add-AzVmssExtension that prepares all the raw attached data disks:

# Get scale set object
$vmss = Get-AzVmss `
          -ResourceGroupName "myResourceGroup" `
          -VMScaleSetName "myScaleSet"

# Define the script for your Custom Script Extension to run
$publicSettings = @{
  "fileUris" = (,"https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/prepare_vm_disks.ps1");
  "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File prepare_vm_disks.ps1"
}

# Use Custom Script Extension to prepare the attached data disks
Add-AzVmssExtension -VirtualMachineScaleSet $vmss `
  -Name "customScript" `
  -Publisher "Microsoft.Compute" `
  -Type "CustomScriptExtension" `
  -TypeHandlerVersion 1.8 `
  -Setting $publicSettings

# Update the scale set and apply the Custom Script Extension to the VM instances
Update-AzVmss `
  -ResourceGroupName "myResourceGroup" `
  -Name "myScaleSet" `
  -VirtualMachineScaleSet $vmss

若要确认磁盘是否已正确地准备好,请通过 RDP 连接到某个 VM 实例。To confirm that the disks have been prepared correctly, RDP to one of the VM instances.

首先,使用 Get-AzLoadBalancer 获取负载均衡器对象。First, get the load balancer object with Get-AzLoadBalancer. 然后使用 Get-AzLoadBalancerInboundNatRuleConfig 查看入站 NAT 规则。Then, view the inbound NAT rules with Get-AzLoadBalancerInboundNatRuleConfig. NAT 规则列出了 RDP 侦听的每个 VM 实例的 FrontendPortThe NAT rules list the FrontendPort for each VM instance that RDP listens on. 最后,使用 Get-AzPublicIpAddress 获取负载均衡器的公共 IP 地址:Finally, get the public IP address of the load balancer with Get-AzPublicIpAddress:

# Get the load balancer object
$lb = Get-AzLoadBalancer -ResourceGroupName "myResourceGroup" -Name "myLoadBalancer"

# View the list of inbound NAT rules
Get-AzLoadBalancerInboundNatRuleConfig -LoadBalancer $lb | Select-Object Name,Protocol,FrontEndPort,BackEndPort

# View the public IP address of the load balancer
Get-AzPublicIpAddress -ResourceGroupName "myResourceGroup" -Name myPublicIPAddress | Select IpAddress

若要连接到 VM,请指定所需 VM 实例对应的你自己的公共 IP 地址和端口号,如前述命令所示。To connect to your VM, specify your own public IP address and port number of the required VM instance, as shown from the preceding commands. 出现提示时,输入创建规模集时使用的凭据。When prompted, enter the credentials used when you created the scale set. 以下示例连接到 VM 实例 1The following example connects to VM instance 1:

mstsc /v 52.168.121.216:50001

打开 VM 实例上的本地 PowerShell 会话并使用 Get-disk 查看连接的磁盘:Open a local PowerShell session on the VM instance and look at the connected disks with Get-Disk:

Get-Disk

以下示例输出显示三个磁盘已附加到 VM 实例。The following example output shows that the three data disks are attached to the VM instance.

Number  Friendly Name      HealthStatus  OperationalStatus  Total Size  Partition Style
------  -------------      ------------  -----------------  ----------  ---------------
0       Virtual HD         Healthy       Online                 127 GB  MBR
1       Virtual HD         Healthy       Online                  14 GB  MBR
2       Msft Virtual Disk  Healthy       Online                  64 GB  MBR
3       Msft Virtual Disk  Healthy       Online                 128 GB  MBR
4       Msft Virtual Disk  Healthy       Online                 128 GB  MBR

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

Get-Partition

以下示例输出显示三个磁盘已分配了驱动器号:The following example output shows that the three data disks are assigned drive letters:

   DiskPath: \\?\scsi#disk&ven_msft&prod_virtual_disk#000001

PartitionNumber  DriveLetter  Offset   Size  Type
---------------  -----------  ------   ----  ----
1                F            1048576  64 GB  IFS

   DiskPath: \\?\scsi#disk&ven_msft&prod_virtual_disk#000002

PartitionNumber  DriveLetter  Offset   Size   Type
---------------  -----------  ------   ----   ----
1                G            1048576  128 GB  IFS

   DiskPath: \\?\scsi#disk&ven_msft&prod_virtual_disk#000003

PartitionNumber  DriveLetter  Offset   Size   Type
---------------  -----------  ------   ----   ----
1                H            1048576  128 GB  IFS

规模集中每个 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 实例的远程桌面连接会话。Close the remote desktop connection session with the VM instance.

列出附加的磁盘List attached disks

若要查看有关附加到规模集的磁盘的信息,请使用 Get-AzVmss,如下所示:To view information about disks attached to a scale set, use Get-AzVmss as follows:

Get-AzVmss -ResourceGroupName "myResourceGroup" -Name "myScaleSet"

VirtualMachineProfile.StorageProfile 属性下会显示 DataDisks 的列表。Under the VirtualMachineProfile.StorageProfile property, the list of DataDisks is shown. 还会显示有关磁盘大小、存储层和 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:

DataDisks[0]                            :
  Lun                                   : 0
  Caching                               : None
  CreateOption                          : Empty
  DiskSizeGB                            : 64
  ManagedDisk                           :
    StorageAccountType                  : PremiumLRS
DataDisks[1]                            :
  Lun                                   : 1
  Caching                               : None
  CreateOption                          : Empty
  DiskSizeGB                            : 128
  ManagedDisk                           :
    StorageAccountType                  : PremiumLRS
DataDisks[2]                            :
  Lun                                   : 2
  Caching                               : None
  CreateOption                          : Empty
  DiskSizeGB                            : 128
  ManagedDisk                           :
    StorageAccountType                  : PremiumLRS

分离磁盘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. 若要从规模集中分离某个磁盘,请使用 Remove-AzVmssDataDisk 并指定磁盘的 LUN。To detach a disk from a scale set, use Remove-AzVmssDataDisk and specify the LUN of the disk. LUN 显示在上一部分的 Get-AzVmss 命令的输出中。The LUNs are shown in the output from Get-AzVmss in the previous section. 以下示例从规模集分离 LUN 3The following example detaches LUN 3 from the scale set:

# Get scale set object
$vmss = Get-AzVmss `
          -ResourceGroupName "myResourceGroup" `
          -VMScaleSetName "myScaleSet"

# Detach a disk from the scale set
Remove-AzVmssDataDisk `
  -VirtualMachineScaleSet $vmss `
  -Lun 2

# Update the scale set and detach the disk from the VM instances
Update-AzVmss `
  -ResourceGroupName "myResourceGroup" `
  -Name "myScaleSet" `
  -VirtualMachineScaleSet $vmss

清理资源Clean up resources

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

Remove-AzResourceGroup -Name "myResourceGroup" -Force -AsJob

后续步骤Next steps

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

  • 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.