使用 Azure PowerShell 将 VM 部署到专用主机Deploy VMs to dedicated hosts using the Azure PowerShell

本文介绍了如何创建 Azure 专用主机来托管虚拟机 (VM)。This article guides you through how to create an Azure dedicated host to host your virtual machines (VMs).

确保已安装 Azure PowerShell 2.8.0 或更高版本,并已使用 Connect-AzAccount -Environment AzureChinaCloud 登录到 Azure 帐户。Make sure that you have installed Azure PowerShell version 2.8.0 or later, and you are signed in to an Azure account in with Connect-AzAccount -Environment AzureChinaCloud.

限制Limitations

  • 专用主机上目前不支持虚拟机规模集。Virtual machine scale sets are not currently supported on dedicated hosts.
  • 专用主机可用的大小和硬件类型因区域而异。The sizes and hardware types available for dedicated hosts vary by region. 请参阅主机定价页来了解详细信息。Refer to the host pricing page to learn more.

创建主机组Create a host group

主机组是表示专用主机集合的资源。A host group is a resource that represents a collection of dedicated hosts. 你在某个区域中创建主机组,并向其中添加主机。You create a host group in a region, and add hosts to it. 规划高可用性时,有其他选项可供选择。When planning for high availability, there are additional options. 你可以将以下一个或两个选项与专用主机一起使用:You can use one or both of the following options with your dedicated hosts:

  • 跨映射到物理机架的多个容错域。Span across multiple fault domains which are mapped to physical racks.

在这种情况下,你需要为主机组提供容错域计数。In this case, you are need to provide the fault domain count for your host group. 如果你不希望跨组中的容错域,请使用容错域计数 1。If you do not want to span fault domains in your group, use a fault domain count of 1.

此示例将创建具有 2 个容错域的主机组。This example creates a host group with 2 fault domains.

$rgName = "myDHResourceGroup"
$location = "ChinaEast2"

New-AzResourceGroup -Location $location -Name $rgName
$hostGroup = New-AzHostGroup `
   -Location $location `
   -Name myHostGroup `
   -PlatformFaultDomain 2 `
   -ResourceGroupName $rgName

创建主机Create a host

现在,让我们在主机组中创建一个专用主机。Now let's create a dedicated host in the host group. 除了主机名称外,还需要提供主机的 SKU。In addition to a name for the host, you are required to provide the SKU for the host. 主机 SKU 捕获受支持的 VM 系列以及专用主机的硬件代系。Host SKU captures the supported VM series as well as the hardware generation for your dedicated host.

有关主机 SKU 和定价的详细信息,请参阅 Azure 专用主机定价For more information about the host SKUs and pricing, see Azure Dedicated Host pricing.

如果为主机组设置了容错域计数,则系统会要求你为主机指定容错域。If you set a fault domain count for your host group, you will be asked to specify the fault domain for your host. 在此示例中,我们将主机的容错域设置为 1。In this example, we set the fault domain for the host to 1.

$dHost = New-AzHost `
   -HostGroupName $hostGroup.Name `
   -Location $location -Name myHost `
   -ResourceGroupName $rgName `
   -Sku DSv3-Type1 `
   -AutoReplaceOnFailure 1 `
   -PlatformFaultDomain 1

创建 VMCreate a VM

在专用主机上创建虚拟机。Create a virtual machine on the dedicated host.

$cred = Get-Credential
New-AzVM `
   -Credential $cred `
   -ResourceGroupName $rgName `
   -Location $location `
   -Name myVM `
   -HostId $dhost.Id `
   -Image Win2016Datacenter `
   -Size Standard_D4s_v3

警告

如果在没有足够资源的主机上创建虚拟机,则虚拟机将创建为“失败”状态。If you create a virtual machine on a host which does not have enough resources, the virtual machine will be created in a FAILED state.

检查主机的状态Check the status of the host

可以结合 -InstanceView 参数使用 GetAzHost 来查看主机运行状况以及你还可以将多少虚拟机部署到主机。You can check the host health status and how many virtual machines you can still deploy to the host using GetAzHost with the -InstanceView parameter.

Get-AzHost `
   -ResourceGroupName $rgName `
   -Name myHost `
   -HostGroupName $hostGroup.Name `
   -InstanceView

输出类似于以下内容:The output will look similar to this:

ResourceGroupName      : myDHResourceGroup
PlatformFaultDomain    : 1
AutoReplaceOnFailure   : True
HostId                 : 12345678-1234-1234-abcd-abc123456789
ProvisioningTime       : 7/28/2019 5:31:01 PM
ProvisioningState      : Succeeded
InstanceView           : 
  AssetId              : abc45678-abcd-1234-abcd-123456789abc
  AvailableCapacity    : 
    AllocatableVMs[0]  : 
      VmSize           : Standard_D2s_v3
      Count            : 32
    AllocatableVMs[1]  : 
      VmSize           : Standard_D4s_v3
      Count            : 16
    AllocatableVMs[2]  : 
      VmSize           : Standard_D8s_v3
      Count            : 8
    AllocatableVMs[3]  : 
      VmSize           : Standard_D16s_v3
      Count            : 4
    AllocatableVMs[4]  : 
      VmSize           : Standard_D32-8s_v3
      Count            : 2
    AllocatableVMs[5]  : 
      VmSize           : Standard_D32-16s_v3
      Count            : 2
    AllocatableVMs[6]  : 
      VmSize           : Standard_D32s_v3
      Count            : 2
    AllocatableVMs[7]  : 
      VmSize           : Standard_D64-16s_v3
      Count            : 1
    AllocatableVMs[8]  : 
      VmSize           : Standard_D64-32s_v3
      Count            : 1
    AllocatableVMs[9]  : 
      VmSize           : Standard_D64s_v3
      Count            : 1
  Statuses[0]          : 
    Code               : ProvisioningState/succeeded
    Level              : Info
    DisplayStatus      : Provisioning succeeded
    Time               : 7/28/2019 5:31:01 PM
  Statuses[1]          : 
    Code               : HealthState/available
    Level              : Info
    DisplayStatus      : Host available
Sku                    : 
  Name                 : DSv3-Type1
Id                     : /subscriptions/10101010-1010-1010-1010-101010101010/re
sourceGroups/myDHResourceGroup/providers/Microsoft.Compute/hostGroups/myHostGroup/hosts
/myHost
Name                   : myHost
Location               : chinaeast2
Tags                   : {}

添加现有 VMAdd an existing VM

可将现有 VM 添加到专用主机,但必须先停止/解除分配该 VM。You can add an existing VM to a dedicated host, but the VM must first be Stop\Deallocated. 在将 VM 移动到专用主机之前,请确保 VM 配置受支持:Before you move a VM to a dedicated host, make sure that the VM configuration is supported:

  • VM 大小必须属于专用主机所用的同一大小系列。The VM size must be in the same size family as the dedicated host. 例如,如果专用主机是 DSv3,则 VM 大小可以是 Standard_D4s_v3,但不能是 Standard_A4_v2。For example, if your dedicated host is DSv3, then the VM size could be Standard_D4s_v3, but it could not be a Standard_A4_v2.
  • VM 需要位于专用主机所在的同一区域。The VM needs to be located in same region as the dedicated host.
  • VM 不能是邻近放置组的一部分。The VM can't be part of a proximity placement group. 在将 VM 移动到专用主机之前,请先从邻近放置组中删除该 VM。Remove the VM from the proximity placement group before moving it to a dedicated host. 有关详细信息,请参阅将 VM 移出邻近放置组For more information, see Move a VM out of a proximity placement group
  • VM 不能位于可用性集中。The VM can't be in an availability set.

将变量值替换为你自己的信息。Replace the values of the variables with your own information.

$vmRGName = "movetohost"
$vmName = "myVMtoHost"
$dhRGName = "myDHResourceGroup"
$dhGroupName = "myHostGroup"
$dhName = "myHost"

$myDH = Get-AzHost `
   -HostGroupName $dhGroupName `
   -ResourceGroupName $dhRGName `
   -Name $dhName

$myVM = Get-AzVM `
   -ResourceGroupName $vmRGName `
   -Name $vmName

$myVM.Host = New-Object Microsoft.Azure.Management.Compute.Models.SubResource

$myVM.Host.Id = "$myDH.Id"

Stop-AzVM `
   -ResourceGroupName $vmRGName `
   -Name $vmName -Force

Update-AzVM `
   -ResourceGroupName $vmRGName `
   -VM $myVM -Debug

Start-AzVM `
   -ResourceGroupName $vmRGName `
   -Name $vmName

清理Clean up

即使没有部署虚拟机,也会对专用主机收费。You are being charged for your dedicated hosts even when no virtual machines are deployed. 你应删除当前未使用的任何主机以节省成本。You should delete any hosts you are currently not using to save costs.

只有当不再有虚拟机使用主机时,才能删除该主机。You can only delete a host when there are no any longer virtual machines using it. 使用 Remove-AzVM 删除 VM。Delete the VMs using Remove-AzVM.

Remove-AzVM -ResourceGroupName $rgName -Name myVM

删除 VM 后,可以使用 Remove-AzHost 删除主机。After deleting the VMs, you can delete the host using Remove-AzHost.

Remove-AzHost -ResourceGroupName $rgName -Name myHost

删除所有主机后,可以使用 Remove-AzHostGroup 删除主机组。Once you have deleted all of your hosts, you may delete the host group using Remove-AzHostGroup.

Remove-AzHost -ResourceGroupName $rgName -Name myHost

还可以使用 Remove-AzResourceGroup 通过单条命令删除整个资源组。You can also delete the entire resource group in a single command using Remove-AzResourceGroup. 这会删除在组中创建的所有资源,包括所有 VM、主机和主机组。This will delete all resources created in the group, including all of the VMs, hosts and host groups.

Remove-AzResourceGroup -Name $rgName

后续步骤Next steps

  • 此处有一个示例模板,该模板使用区域和容错域来最大限度地提高在某个地区的复原能力。There is sample template, found here, that uses both zones and fault domains for maximum resiliency in a region.

  • 也可以使用 Azure 门户专用主机。You can also deploy dedicated hosts using the Azure portal.