调整 Windows VM 的大小Resize a Windows VM

本文介绍如何将 VM 切换到不同的 VM 大小This article shows you how to move a VM to a different VM size.

完成创建虚拟机 (VM) 后,可以通过更改 VM 大小来扩大或缩小 VM。After you create a virtual machine (VM), you can scale the VM up or down by changing the VM size. 在某些情况下,必须先解除分配 VM。In some cases, you must deallocate the VM first. 如果新大小在当前托管 VM 的硬件群集上不可用,则可能会出现这种情况。This can happen if the new size is not available on the hardware cluster that is currently hosting the VM.

如果虚拟机使用高级存储,请确保选择 s 版本的大小以获得高级存储支持。If your VM uses Premium Storage, make sure that you choose an s version of the size to get Premium Storage support. 例如,选择 Standard_E4s_v3,而不是 Standard_E4_v3。For example, choose Standard_E4s_v3 instead of Standard_E4_v3.

使用门户Use the portal

  1. 打开 Azure 门户Open the Azure portal.
  2. 在虚拟机所对应的页。Open the page for the virtual machine.
  3. 在左侧菜单中,选择“大小”。In the left menu, select Size.
  4. 从可用大小列表中选取新大小,然后选择“重设大小”。Pick a new size from the list of available sizes and then select Resize.

如果虚拟机当前正在运行,更改其大小会导致其重启。If the virtual machine is currently running, changing its size will cause it to be restarted. 停止虚拟机可能会显示其他大小。Stopping the virtual machine may reveal additional sizes.

使用 PowerShell 重设不在可用性集中的 VM 的大小Use PowerShell to resize a VM not in an availability set

设置一些变量。Set some variables. 将值替换为自己的信息。Replace the values with your own information.

$resourceGroup = "myResourceGroup"
$vmName = "myVM"

列出托管 VM 的硬件群集上可用的 VM 大小。List the VM sizes that are available on the hardware cluster where the VM is hosted.

Get-AzVMSize -ResourceGroupName $resourceGroup -VMName $vmName 

如果列出了所需大小,运行以下命令即可重设虚拟机大小。If the size you want is listed, run the following commands to resize the VM. 如果未列出所需大小,请转到步骤 3。If the desired size is not listed, go on to step 3.

$vm = Get-AzVM -ResourceGroupName $resourceGroup -VMName $vmName
$vm.HardwareProfile.VmSize = "<newVMsize>"
Update-AzVM -VM $vm -ResourceGroupName $resourceGroup

如果未列出所需大小,运行以下命令即可解除分配虚拟机、重设其大小,并重新启动虚拟机。If the size you want is not listed, run the following commands to deallocate the VM, resize it, and restart the VM. <newVMsize> 替换为所需大小。Replace <newVMsize> with the size you want.

Stop-AzVM -ResourceGroupName $resourceGroup -Name $vmName -Force
$vm = Get-AzVM -ResourceGroupName $resourceGroup -VMName $vmName
$vm.HardwareProfile.VmSize = "<newVMSize>"
Update-AzVM -VM $vm -ResourceGroupName $resourceGroup
Start-AzVM -ResourceGroupName $resourceGroup -Name $vmName

警告

解除分配 VM 会释放分配给该 VM 的所有动态 IP 地址。Deallocating the VM releases any dynamic IP addresses assigned to the VM. OS 和数据磁盘不受影响。The OS and data disks are not affected.

使用 PowerShell 重设可用性集中的 VM 的大小Use PowerShell to resize a VM in an availability set

如果可用性集中 VM 的新大小在当前托管 VM 的硬件群集上不可用,则需要解除分配可用性集中的所有 VM 以调整 VM 大小。If the new size for a VM in an availability set is not available on the hardware cluster currently hosting the VM, then all VMs in the availability set will need to be deallocated to resize the VM. 已调整一个 VM 的大小后,可能还需要更新可用性集中其他 VM 的大小。You also might need to update the size of other VMs in the availability set after one VM has been resized. 若要调整可用性集中 VM 的大小,请执行以下步骤。To resize a VM in an availability set, perform the following steps.

$resourceGroup = "myResourceGroup"
$vmName = "myVM"

列出托管 VM 的硬件群集上可用的 VM 大小。List the VM sizes that are available on the hardware cluster where the VM is hosted.

Get-AzVMSize -ResourceGroupName $resourceGroup -VMName $vmName 

如果列出了所需大小,请运行以下命令来调整 VM 的大小。If the desired size is listed, run the following commands to resize the VM. 如果未列出所需大小,请转到下一部分。If it is not listed, go to the next section.

$vm = Get-AzVM -ResourceGroupName $resourceGroup -VMName $vmName 
$vm.HardwareProfile.VmSize = "<newVmSize>"
Update-AzVM -VM $vm -ResourceGroupName $resourceGroup

如果未列出所需大小,继续执行以下步骤即可解除分配可用性集中的所有 VM、重设 VM 大小,并重新启动它们。If the size you want is not listed, continue with the following steps to deallocate all VMs in the availability set, resize VMs, and restart them.

停止可用性集中的所有 VM。Stop all VMs in the availability set.

$availabilitySetName = "<availabilitySetName>"
$as = Get-AzAvailabilitySet -ResourceGroupName $resourceGroup -Name $availabilitySetName
$virtualMachines = $as.VirtualMachinesReferences |  Get-AzResource | Get-AzVM
$virtualMachines |  Stop-AzVM -Force -NoWait  

调整可用性集中 VM 的大小并重新启动 VM。Resize and restart the VMs in the availability set.

$availabilitySetName = "<availabilitySetName>"
$newSize = "<newVmSize>"
$as = Get-AzAvailabilitySet -ResourceGroupName $resourceGroup -Name $availabilitySetName
$virtualMachines = $as.VirtualMachinesReferences |  Get-AzResource | Get-AzVM
$virtualMachines | Foreach-Object { $_.HardwareProfile.VmSize = $newSize }
$virtualMachines | Update-AzVM
$virtualMachines | Start-AzVM

后续步骤Next steps

若要提高可伸缩性,请运行多个 VM 实例并进行横向扩展。有关详细信息,请参阅 自动缩放虚拟机规模集中的 Windows 计算机For additional scalability, run multiple VM instances and scale out. For more information, see Automatically scale Windows machines in a Virtual Machine Scale Set.