教程 - 使用 Azure PowerShell 管理 Azure 磁盘Tutorial - Manage Azure disks with Azure PowerShell

Azure 虚拟机使用磁盘来存储 VM 操作系统、应用程序和数据。Azure virtual machines use disks to store the VMs operating system, applications, and data. 创建 VM 时,请务必选择适用于所需工作负荷的磁盘大小和配置。When creating a VM, it's important to choose a disk size and configuration appropriate to the expected workload. 本教程介绍如何部署和管理 VM 磁盘。This tutorial covers deploying and managing VM disks. 学习内容:You learn about:

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

启动 Azure PowerShellLaunch Azure PowerShell

打开 Azure Powershell 控制台,并以管理员权限运行以下脚本。Open the Azure Powershell console and run the following scripts with Administrator priviledge.

Note

本文进行了更新,以便使用新的 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

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

操作系统磁盘 - 操作系统磁盘大小最大可达 4 TB,并可托管 VM 操作系统。Operating system disk - Operating system disks can be sized up to 4 terabytes, and hosts the VMs operating system. 如果从 Azure 市场映像创建新的虚拟机 (VM),通常为 127 GB(但某些映像的 OS 磁盘更小)。If you create a new virtual machine (VM) from an Azure Marketplace image, the typically 127 GB (but some images have smaller OS disk sizes). OS 磁盘默认分配有一个 C: 驱动器号。The OS disk is assigned a drive letter of C: by default. 已针对 OS 性能优化了 OS 磁盘的磁盘缓存配置。The disk caching configuration of the OS disk is optimized for OS performance. OS 磁盘不得 承载应用程序或数据。The OS disk should not host applications or data. 对于应用程序和数据,请使用数据磁盘,详情请参见本文稍后部分。For applications and data, use a data disk, which is 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. 临时磁盘具有高性能,可用于临时数据处理等操作。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. 临时磁盘默认分配有一个 D: 驱动器号。Temporary disks are assigned a drive letter of D: by default.

Azure 数据磁盘Azure data disks

可添加额外的数据磁盘,用于安装应用程序和存储数据。Additional data disks can be added for installing applications and storing data. 在任何需要持久和灵敏数据存储的情况下,都应使用数据磁盘。Data disks should be used in any situation where durable and responsive data storage is needed. 虚拟机的大小决定可附加到 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. 高级存储支持 DS 系列、DSv2 系列和 FS 系列 VM。Premium Storage supports DS-series, DSv2-series, and FS-series VMs. 高级磁盘分为 5 种类型(P10、P20、P30、P40、P50),磁盘大小决定磁盘类型。Premium disks come in five types (P10, P20, P30, P40, P50), the size of the disk determines the disk type. 选择时,磁盘大小值舍入为下一类型。When selecting, a disk size the value is rounded up to the next type. 例如,如果大小不到 128 GB,则磁盘类型为 P10;如果大小在 129 GB 到 512 GB 之间,则磁盘类型为 P20。For example, if the size is below 128 GB the disk type is P10, or between 129 GB and 512 GB the disk is P20.

高级磁盘性能Premium disk performance

高级 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 MiB/秒25 MiB/sec 25 MiB/秒25 MiB/sec 25 MiB/秒25 MiB/sec 25 MiB/秒25 MiB/sec 50 MiB/秒50 MiB/sec 100 MiB/秒100 MiB/sec 125 MiB/秒125 MiB/sec 150 MiB/秒150 MiB/sec 200 MiB/秒200 MiB/sec 250 MiB/秒250 MiB/sec 250 MiB/秒250 MiB/sec 500 MiB/秒500 MiB/sec 750 MiB/秒750 MiB/sec 900 MiB/秒900 MiB/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 MiB/秒170 MiB/sec 170 MiB/秒170 MiB/sec 170 MiB/秒170 MiB/sec 170 MiB/秒170 MiB/sec 170 MiB/秒170 MiB/sec 170 MiB/秒170 MiB/sec 170 MiB/秒170 MiB/sec 170 MiB/秒170 MiB/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,但还可通过条带化多个数据磁盘实现更高级别的性能。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 附加 64 个数据磁盘。For instance, 64 data disks can be attached to Standard_GS5 VM. 如果这些磁盘的大小都为 P30,则最大可实现 80,000 IOPS。If each of these disks is sized as a P30, a maximum of 80,000 IOPS can be achieved. 若要详细了解每个 VM 的最大 IOPS,请参阅 VM 类型和大小For detailed information on max IOPS per VM, see VM types and sizes.

创建并附加磁盘Create and attach disks

若要完成本教程中的示例,必须具备现有虚拟机。To complete the example in this tutorial, you must have an existing virtual machine. 需要时,使用以下命令创建虚拟机。If needed, create a virtual machine with the following commands.

使用 Get-Credential 设置虚拟机上管理员帐户所需的用户名和密码:Set the username and password needed for the administrator account on the virtual machine with Get-Credential:

使用 New-AzVM 创建虚拟机。Create the virtual machine with New-AzVM. 系统将提示你输入 VM 的管理员帐户的用户名和密码。You'll be prompted to enter a username and password for the administrators account for the VM.

New-AzVm `
    -ResourceGroupName "myResourceGroupDisk" `
    -Name "myVM" `
    -Location "China East" `
    -VirtualNetworkName "myVnet" `
    -SubnetName "mySubnet" `
    -SecurityGroupName "myNetworkSecurityGroup" `
    -PublicIpAddressName "myPublicIpAddress" 

使用 New-AzDiskConfig 创建初始配置。Create the initial configuration with New-AzDiskConfig. 以下示例配置大小为 128 GB 的磁盘。The following example configures a disk that is 128 gigabytes in size.

$diskConfig = New-AzDiskConfig `
    -Location "ChinaEast" `
    -CreateOption Empty `
    -DiskSizeGB 128

使用 New-AzDisk 命令创建数据磁盘。Create the data disk with the New-AzDisk command.

$dataDisk = New-AzDisk `
    -ResourceGroupName "myResourceGroupDisk" `
    -DiskName "myDataDisk" `
    -Disk $diskConfig

使用 Get-AzVM 命令获取要向其添加数据磁盘的虚拟机。Get the virtual machine that you want to add the data disk to with the Get-AzVM command.

$vm = Get-AzVM -ResourceGroupName "myResourceGroupDisk" -Name "myVM"

使用 Add-AzVMDataDisk 命令向虚拟机配置添加数据磁盘。Add the data disk to the virtual machine configuration with the Add-AzVMDataDisk command.

$vm = Add-AzVMDataDisk `
    -VM $vm `
    -Name "myDataDisk" `
    -CreateOption Attach `
    -ManagedDiskId $dataDisk.Id `
    -Lun 1

使用 Update-AzVM 命令更新虚拟机。Update the virtual machine with the Update-AzVM command.

Update-AzVM -ResourceGroupName "myResourceGroupDisk" -VM $vm

准备数据磁盘Prepare data disks

将磁盘附加到虚拟机后,需要将操作系统配置为使用该磁盘。Once a disk has been attached to the virtual machine, the operating system needs to be configured to use the disk. 以下示例演示如何手动配置添加到 VM 的第一个磁盘。The following example shows how to manually configure the first disk added to the VM. 还可使用自定义脚本扩展自动执行此过程。This process can also be automated using the custom script extension.

手动配置Manual configuration

创建与虚拟机的 RDP 连接。Create an RDP connection with the virtual machine. 打开 PowerShell 并运行此脚本。Open up PowerShell and run this script.

Get-Disk | Where partitionstyle -eq 'raw' |
    Initialize-Disk -PartitionStyle MBR -PassThru |
    New-Partition -AssignDriveLetter -UseMaximumSize |
    Format-Volume -FileSystem NTFS -NewFileSystemLabel "myDataDisk" -Confirm:$false

验证数据磁盘Verify the data disk

若要验证是否已附加数据磁盘,请查看附加 DataDisksStorageProfileTo verify that the data disk is attached, view the StorageProfile for the attached DataDisks.

$vm.StorageProfile.DataDisks

输出应该类似于以下示例:The output should look something like this example:

Name            : myDataDisk
DiskSizeGB      : 128
Lun             : 1
Caching         : None
CreateOption    : Attach
SourceImage     :
VirtualHardDisk :

后续步骤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

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