使用 PowerShell 将 VM 部署到邻近放置组Deploy VMs to proximity placement groups using PowerShell

若要让 VM 尽可能靠近,将延迟尽可能降至最低,应将 VM 部署到一个邻近放置组中。To get VMs as close as possible, achieving the lowest possible latency, you should deploy them within a proximity placement group.

邻近放置组是一种逻辑分组,用于确保 Azure 计算资源在物理上彼此靠近。A proximity placement group is a logical grouping used to make sure that Azure compute resources are physically located close to each other. 邻近放置组用于要求低延迟的工作负荷。Proximity placement groups are useful for workloads where low latency is a requirement.

创建邻近放置组Create a proximity placement group

使用 New-AzProximityPlacementGroup cmdlet 创建邻近放置组。Create a proximity placement group using the New-AzProximityPlacementGroup cmdlet.

$resourceGroup = "myPPGResourceGroup"
$location = "China East"
$ppgName = "myPPG"
New-AzResourceGroup -Name $resourceGroup -Location $location
$ppg = New-AzProximityPlacementGroup `
   -Location $location `
   -Name $ppgName `
   -ResourceGroupName $resourceGroup `
   -ProximityPlacementGroupType Standard

列出邻近放置组List proximity placement groups

可以使用 Get-AzProximityPlacementGroup cmdlet 列出所有邻近放置组。You can list all of the proximity placement groups using the Get-AzProximityPlacementGroup cmdlet.

Get-AzProximityPlacementGroup

创建 VMCreate a VM

通过 New-AzVM 创建 VM 时,请在邻近放置组中创建 VM,使用 -ProximityPlacementGroup $ppg.Id 引用邻近放置组 ID。Create a VM in the proximity placement group using -ProximityPlacementGroup $ppg.Id to refer to the proximity placement group ID when you use New-AzVM to create the VM.

$vmName = "myVM"

New-AzVm `
  -ResourceGroupName $resourceGroup `
  -Name $vmName `
  -Location $location `
  -OpenPorts 3389 `
  -ProximityPlacementGroup $ppg.Id

可以使用 Get-AzProximityPlacementGroup 查看放置组中的 VM。You can see the VM in the placement group using Get-AzProximityPlacementGroup.

Get-AzProximityPlacementGroup -ResourceId $ppg.Id |
    Format-Table -Property VirtualMachines -Wrap

将现有 VM 移到邻近放置组中Move an existing VM into a proximity placement group

还可以将现有 VM 添加到邻近放置组。You can also add an existing VM to a proximity placement group. 需先停止\解除分配 VM,然后更新 VM 并重启。You need to stop\deallocate the VM first, then update the VM and restart.

$ppg = Get-AzProximityPlacementGroup -ResourceGroupName myPPGResourceGroup -Name myPPG
$vm = Get-AzVM -ResourceGroupName myResourceGroup -Name myVM
Stop-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName
Update-AzVM -VM $vm -ResourceGroupName $vm.ResourceGroupName -ProximityPlacementGroupId $ppg.Id
Start-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName

将现有 VM 移出邻近放置组Move an existing VM out of a proximity placement group

若要从邻近放置组中删除 VM,需先停止\解除分配 VM,然后更新 VM 并重启。To remove a VM from a proximity placement group, you need to stop\deallocate the VM first, then update the VM and restart.

$ppg = Get-AzProximityPlacementGroup -ResourceGroupName myPPGResourceGroup -Name myPPG
$vm = Get-AzVM -ResourceGroupName myResourceGroup -Name myVM
Stop-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName
$vm.ProximityPlacementGroup = ""
Update-AzVM -VM $vm -ResourceGroupName $vm.ResourceGroupName 
Start-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName

可用性集Availability Sets

还可以在邻近放置组中创建可用性集。You can also create an availability set in your proximity placement group. 将同一 -ProximityPlacementGroup 参数与 New-AzAvailabilitySet cmdlet 一起使用来创建一个可用性集,并且也将在同一邻近放置组中创建在该可用性集中创建的所有 VM。Use the same -ProximityPlacementGroup parameter with the New-AzAvailabilitySet cmdlet to create an availability set and all of the VMs created in the availability set will also be created in the same proximity placement group.

若要在邻近放置组中添加或删除现有可用性集,需先停止该可用性集中的所有 VM。To add or remove an existing availability set to a proximity placement group, you first need to stop all of the VMs in the availability set.

将现有可用性集移到邻近放置组中Move an existing availability set into a proximity placement group

$resourceGroup = "myResourceGroup"
$avSetName = "myAvailabilitySet"
$avSet = Get-AzAvailabilitySet -ResourceGroupName $resourceGroup -Name $avSetName
$vmIds = $avSet.VirtualMachinesReferences
foreach ($vmId in $vmIDs){
    $string = $vmID.Id.Split("/")
    $vmName = $string[8]
    Stop-AzVM -ResourceGroupName $resourceGroup -Name $vmName -Force
    } 

$ppg = Get-AzProximityPlacementGroup -ResourceGroupName myPPG -Name myPPG
Update-AzAvailabilitySet -AvailabilitySet $avSet -ProximityPlacementGroupId $ppg.Id
foreach ($vmId in $vmIDs){
    $string = $vmID.Id.Split("/")
    $vmName = $string[8]
    Start-AzVM -ResourceGroupName $resourceGroup -Name $vmName 
    } 

将现有可用性集移出邻近放置组Move an existing availability set out of a proximity placement group

$resourceGroup = "myResourceGroup"
$avSetName = "myAvailabilitySet"
$avSet = Get-AzAvailabilitySet -ResourceGroupName $resourceGroup -Name $avSetName
$vmIds = $avSet.VirtualMachinesReferences
foreach ($vmId in $vmIDs){
    $string = $vmID.Id.Split("/")
    $vmName = $string[8]
    Stop-AzVM -ResourceGroupName $resourceGroup -Name $vmName -Force
    } 

$avSet.ProximityPlacementGroup = ""
Update-AzAvailabilitySet -AvailabilitySet $avSet 
foreach ($vmId in $vmIDs){
    $string = $vmID.Id.Split("/")
    $vmName = $string[8]
    Start-AzVM -ResourceGroupName $resourceGroup -Name $vmName 
    } 

规模集Scale sets

还可以在邻近放置组中创建规模集。You can also create a scale set in your proximity placement group. 将同一 -ProximityPlacementGroup 参数与 New-AzVmss 一起使用来创建规模集,并且将在同一邻近放置组中创建所有实例。Use the same -ProximityPlacementGroup parameter with New-AzVmss to create a scale set and all of the instances will be created in the same proximity placement group.

若要在邻近放置组中添加或删除现有规模集,需先停止该规模集。To add or remove an existing scale set to a proximity placement group, you first need to stop the scale set.

将现有规模集移到邻近放置组中Move an existing scale set into a proximity placement group

$ppg = Get-AzProximityPlacementGroup -ResourceGroupName myPPG -Name myPPG
$vmss = Get-AzVmss -ResourceGroupName myVMSSResourceGroup -VMScaleSetName myScaleSet
Stop-AzVmss -VMScaleSetName $vmss.Name -ResourceGroupName $vmss.ResourceGroupName
Update-AzVmss -VMScaleSetName $vmss.Name -ResourceGroupName $vmss.ResourceGroupName -ProximityPlacementGroupId $ppg.Id
Start-AzVmss -VMScaleSetName $vmss.Name -ResourceGroupName $vmss.ResourceGroupName

将现有规模集移出邻近放置组Move an existing scale set out of a proximity placement group

$vmss = Get-AzVmss -ResourceGroupName myVMSSResourceGroup -VMScaleSetName myScaleSet
Stop-AzVmss -VMScaleSetName $vmss.Name -ResourceGroupName $vmss.ResourceGroupName
$vmss.ProximityPlacementGroup = ""
Update-AzVmss -VirtualMachineScaleSet $vmss -VMScaleSetName $vmss.Name -ResourceGroupName $vmss.ResourceGroupName  
Start-AzVmss -VMScaleSetName $vmss.Name -ResourceGroupName $vmss.ResourceGroupName

后续步骤Next steps

也可使用 Azure CLI 创建邻近放置组。You can also use the Azure CLI to create proximity placement groups.