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

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

确保已安装了 Azure CLI 2.0.70 或更高版本,并已使用 az login 登录到 Azure 帐户。Make sure that you have installed Azure CLI version 2.0.70 or later, and signed in to an Azure account using az login.

限制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 resource group

Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed. 使用 az group create 创建资源组。Create the resource group with az group create. 以下示例在“中国东部 2”位置创建名为 myDHResourceGroup 的资源组。 The following example creates a resource group named myDHResourceGroup in the China East 2 location.

az group create --name myDHResourceGroup --location chinaeast2 

创建主机组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.

在此示例中,我们将通过 az vm host group create 使用容错域创建主机组。In this example, we will use az vm host group create to create a host group using fault domains.

az vm host group create \
   --name myHostGroup \
   -g myDHResourceGroup \
   --platform-fault-domain-count 2 

创建主机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.

使用 az vm host create 创建主机。Use az vm host create to create a host. 如果为主机组设置了容错域计数,则系统会要求你为主机指定容错域。If you set a fault domain count for your host group, you will be asked to specify the fault domain for your host.

az vm host create \
   --host-group myHostGroup \
   --name myHost \
   --sku DSv3-Type1 \
   --platform-fault-domain 1 \
   -g myDHResourceGroup

创建虚拟机Create a virtual machine

使用 az vm create 在专用主机中创建虚拟机。Create a virtual machine within a dedicated host using az vm create.

az vm create \
   -n myVM \
   --image debian \
   --generate-ssh-keys \
   --host-group myHostGroup \
   --host myHost \
   --generate-ssh-keys \
   --size Standard_D4s_v3 \
   -g myDHResourceGroup

Warning

如果在没有足够资源的主机上创建虚拟机,则虚拟机将创建为“失败”状态。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

可以使用 az vm host get-instance-view 来查看主机运行状况以及还可以将多少虚拟机部署到主机。You can check the host health status and how many virtual machines you can still deploy to the host using az vm host get-instance-view.

az vm host get-instance-view \
   -g myDHResourceGroup \
   --host-group myHostGroup \
   --name myHost

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

{
  "autoReplaceOnFailure": true,
  "hostId": "6de80643-0f45-4e94-9a4c-c49d5c777b62",
  "id": "/subscriptions/10101010-1010-1010-1010-101010101010/resourceGroups/myDHResourceGroup/providers/Microsoft.Compute/hostGroups/myHostGroup/hosts/myHost",
  "instanceView": {
    "assetId": "12345678-1234-1234-abcd-abc123456789",
    "availableCapacity": {
      "allocatableVms": [
        {
          "count": 31.0,
          "vmSize": "Standard_D2s_v3"
        },
        {
          "count": 15.0,
          "vmSize": "Standard_D4s_v3"
        },
        {
          "count": 7.0,
          "vmSize": "Standard_D8s_v3"
        },
        {
          "count": 3.0,
          "vmSize": "Standard_D16s_v3"
        },
        {
          "count": 1.0,
          "vmSize": "Standard_D32-8s_v3"
        },
        {
          "count": 1.0,
          "vmSize": "Standard_D32-16s_v3"
        },
        {
          "count": 1.0,
          "vmSize": "Standard_D32s_v3"
        },
        {
          "count": 1.0,
          "vmSize": "Standard_D48s_v3"
        },
        {
          "count": 0.0,
          "vmSize": "Standard_D64-16s_v3"
        },
        {
          "count": 0.0,
          "vmSize": "Standard_D64-32s_v3"
        },
        {
          "count": 0.0,
          "vmSize": "Standard_D64s_v3"
        }
      ]
    },
    "statuses": [
      {
        "code": "ProvisioningState/succeeded",
        "displayStatus": "Provisioning succeeded",
        "level": "Info",
        "message": null,
        "time": "2019-07-24T21:22:40.604754+00:00"
      },
      {
        "code": "HealthState/available",
        "displayStatus": "Host available",
        "level": "Info",
        "message": null,
        "time": null
      }
    ]
  },
  "licenseType": null,
  "location": "chinaeast2",
  "name": "myHost",
  "platformFaultDomain": 1,
  "provisioningState": "Succeeded",
  "provisioningTime": "2019-07-24T21:22:40.604754+00:00",
  "resourceGroup": "myDHResourceGroup",
  "sku": {
    "capacity": null,
    "name": "DSv3-Type1",
    "tier": null
  },
  "tags": null,
  "type": null,
  "virtualMachines": [
    {
      "id": "/subscriptions/10101010-1010-1010-1010-101010101010/resourceGroups/MYDHRESOURCEGROUP/providers/Microsoft.Compute/virtualMachines/MYVM",
      "resourceGroup": "MYDHRESOURCEGROUP"
    }
  ]
}

作为模板导出Export as a template

如果现在要使用相同参数创建额外的开发环境或与其匹配的生产环境,则可以导出模板。You can export a template if you now want to create an additional development environment with the same parameters, or a production environment that matches it. Resource Manager 使用定义了所有环境参数的 JSON 模板。Resource Manager uses JSON templates that define all the parameters for your environment. 通过引用此 JSON 模板构建出整个环境。You build out entire environments by referencing this JSON template. 可以手动构建 JSON 模板,也可以通过导出现有环境来替你创建 JSON 模板。You can build JSON templates manually or export an existing environment to create the JSON template for you. 使用 az group export 导出资源组。Use az group export to export your resource group.

az group export --name myDHResourceGroup > myDHResourceGroup.json 

此命令在当前工作目录中创建 myDHResourceGroup.json 文件。This command creates the myDHResourceGroup.json file in your current working directory. 从此模板创建环境时,系统会提示输入所有资源名称。When you create an environment from this template, you are prompted for all the resource names. 可以通过将 --include-parameter-default-value 参数添加到 az group export 命令在模板文件中填充这些名称。You can populate these names in your template file by adding the --include-parameter-default-value parameter to the az group export command. 请编辑 JSON 模板以指定资源名称,或创建 parameters.json 文件来指定资源名称。Edit your JSON template to specify the resource names, or create a parameters.json file that specifies the resource names.

若要通过模板创建环境,请使用 az group deployment createTo create an environment from your template, use az group deployment create.

az group deployment create \ 
    --resource-group myNewResourceGroup \ 
    --template-file myDHResourceGroup.json 

清理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. 使用 az vm delete 删除 VM。Delete the VMs using az vm delete.

az vm delete -n myVM -g myDHResourceGroup

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

az vm host delete -g myDHResourceGroup --host-group myHostGroup --name myHost 

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

az vm host group delete -g myDHResourceGroup --host-group myHostGroup  

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

az group delete -n myDHResourceGroup 

后续步骤Next steps