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

使用 PowerShell 管理 Azure 磁盘Manage Azure disks with PowerShell

Azure 虚拟机使用磁盘来存储 VM 操作系统、应用程序和数据。Azure virtual machines use disks to store the VMs operating system, applications, and data. 创建 VM 时,请务必选择适用于所需工作负荷的磁盘大小和配置。When creating a VM it is 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 Cloud ShellLaunch Azure Cloud Shell

Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的步骤。The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. 它预安装有常用 Azure 工具并将其配置与帐户一起使用。It has common Azure tools preinstalled and configured to use with your account. 请直接单击“复制”对代码进行复制,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。Just click the Copy to copy the code, paste it into the Cloud Shell, and then press enter to run it. 可通过多种方式来启动 Cloud Shell:There are a few ways to launch the Cloud Shell:

单击代码块右上角的“试用”。Click 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/powershellhttps://shell.azure.com/powershell
单击 Azure 门户右上角菜单上的“Cloud Shell”按钮。Click the Cloud Shell button on the menu in the upper right of the Azure portal. 门户中的 Cloud ShellCloud Shell in the portal

如果选择在本地安装并使用 PowerShell,则本教程需要 Azure PowerShell 模块 5.3 或更高版本。If you choose to install and use the PowerShell locally, this tutorial requires the Azure PowerShell module version 5.3 or later. 运行 Get-Module -ListAvailable AzureRM 即可查找版本。Run Get-Module -ListAvailable AzureRM to find the version. 如果需要升级,请参阅安装 Azure PowerShell 模块If you need to upgrade, see Install Azure PowerShell module. 如果在本地运行 PowerShell,则还需运行 Connect-AzureRmAccount 以创建与 Azure 的连接。If you are running PowerShell locally, you also need to run Connect-AzureRmAccount to create a connection with Azure.

默认 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 terabyte, and hosts the VMs operating system. 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.

临时磁盘大小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

可添加额外的数据磁盘,用于安装应用程序和存储数据。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 desired. 每个数据磁盘的最大容量为 4 TB。Each data disk has a maximum capacity of 4 terabytes. 虚拟机的大小决定可附加到 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 常见大小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、G、M 系列D, E, G, and M 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. 高级磁盘分为 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

高级存储磁盘类型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 附加 64 个数据磁盘。For instance, 64 data disks can be attached to Standard_GS5 VM. 如果这些磁盘的大小都为 P30,则最大可实现 80,000 IOPS。If each of these disks are 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:

$cred = Get-Credential

使用 New-AzureRmVM 创建虚拟机。Create the virtual machine with New-AzureRmVM.

New-AzureRmVm `
    -ResourceGroupName "myResourceGroupDisk" `
    -Name "myVM" `
    -Location "East US" `
    -VirtualNetworkName "myVnet" `
    -SubnetName "mySubnet" `
    -SecurityGroupName "myNetworkSecurityGroup" `
    -PublicIpAddressName "myPublicIpAddress" `
    -Credential $cred `
    -AsJob

-AsJob 参数以后台任务的方式创建 VM,因此 PowerShell 提示符会返回到你所在的位置。The -AsJob parameter creates the VM as a background task, so the PowerShell prompts return to you. 可以通过 Job cmdlet 查看后台作业的详细信息。You can view details of background jobs with the Job cmdlet.

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

$diskConfig = New-AzureRmDiskConfig `
    -Location "EastUS" `
    -CreateOption Empty `
    -DiskSizeGB 128

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

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

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

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

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

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

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

Update-AzureRmVM -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

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