教程:使用 Azure CLI 创建和部署高度可用的虚拟机Tutorial: Create and deploy highly available virtual machines with the Azure CLI

本教程介绍如何使用称作“可用性集”的功能提高 Azure 上虚拟机解决方案的可用性和可靠性。In this tutorial, you learn how to increase the availability and reliability of your Virtual Machine solutions on Azure using a capability called Availability Sets. 可用性集可确保在 Azure 上部署的 VM 能够跨多个隔离的硬件群集分布。Availability sets ensure that the VMs you deploy on Azure are distributed across multiple isolated hardware clusters. 这样,就可以确保当 Azure 中发生硬件或软件故障时,只有一部分 VM 受到影响,整体解决方案仍可使用和操作。Doing this ensures that if a hardware or software failure within Azure happens, only a subset of your VMs is impacted and that your overall solution remains available and operational.

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 创建可用性集Create an availability set
  • 在可用性集中创建 VMCreate a VM in an availability set
  • 检查可用的 VM 大小Check available VM sizes


在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

如果选择在本地安装并使用 CLI,本教程要求运行 Azure CLI 2.0.30 或更高版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.0.30 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.


可用性集是一种逻辑分组功能,在 Azure 中使用它可以确保将 VM 资源部署在 Azure 数据中心后,这些资源相互隔离。An Availability Set is a logical grouping capability that you can use in Azure to ensure that the VM resources you place within it are isolated from each other when they are deployed within an Azure datacenter. Azure 确保可用性集中部署的 VM 能够跨多个物理服务器、计算机架、存储单元和网络交换机运行。Azure ensures that the VMs you place within an Availability Set run across multiple physical servers, compute racks, storage units, and network switches. 如果出现硬件或 Azure 软件故障,只有一部分 VM 会受到影响,整体应用程序仍会保持运行,可供客户使用。If a hardware or Azure software failure occurs, only a subset of your VMs are impacted, and your overall application stays up and continues to be available to your customers. 如果想要构建可靠的云解决方案,可用性集是一项关键功能。Availability Sets are an essential capability when you want to build reliable cloud solutions.

假设某个基于 VM 的典型解决方案包含四个前端 Web 服务器,并使用两个托管数据库的后端 VM。Let's consider a typical VM-based solution where you might have four front-end web servers and use two back-end VMs that host a database. 在 Azure 中,需要在部署 VM 之前先定义两个可用性集:一个可用性集用于“Web”层,另一个可用性集用于“数据库”层。With Azure, you'd want to define two availability sets before you deploy your VMs: one availability set for the "web" tier and one availability set for the "database" tier. 创建新的 VM 时,可在 az vm create 命令中指定可用性集作为参数,Azure 会自动确保在可用性集中创建的 VM 在多个物理硬件资源之间保持独立。When you create a new VM you can then specify the availability set as a parameter to the az vm create command, and Azure automatically ensures that the VMs you create within the available set are isolated across multiple physical hardware resources. 如果运行某个 Web 服务器或数据库服务器的物理硬件有问题,可以确信 Web 服务器和数据库 VM 的其他实例会保持运行状态,因为它们位于不同的硬件上。If the physical hardware that one of your Web Server or Database Server VMs is running on has a problem, you know that the other instances of your Web Server and Database VMs remain running because they are on different hardware.

创建可用性集Create an availability set

可使用 az vm availability-set create 创建可用性集。You can create an availability set using az vm availability-set create. 在本示例中,myResourceGroupAvailability 资源组中名为 myAvailabilitySet 的可用性集的更新域数和容错域数均设置为 2In this example, the number of update and fault domains is set to 2 for the availability set named myAvailabilitySet in the myResourceGroupAvailability resource group.

首先,使用 az group create 创建资源组,然后创建可用性集:First, create a resource group with az group create, then create the availability set:

az group create --name myResourceGroupAvailability --location chinaeast2

az vm availability-set create \
    --resource-group myResourceGroupAvailability \
    --name myAvailabilitySet \
    --platform-fault-domain-count 2 \
    --platform-update-domain-count 2

使用可用性集可跨容错域和更新域隔离资源。Availability Sets allow you to isolate resources across fault domains and update domains. 容错域代表服务器、网络和存储资源的隔离集合。A fault domain represents an isolated collection of server + network + storage resources. 在前面的示例中,在部署 VM 时,可用性集至少分布在两个容错域中。In the preceding example, the availability set is distributed across at least two fault domains when the VMs are deployed. 可用性集还分布在两个更新域中。The availability set is also distributed across two update domains. 两个更新域确保当 Azure 执行软件更新时,VM 资源是隔离的,防止在 VM 上运行的所有软件同时更新。Two update domains ensure that when Azure performs software updates, the VM resources are isolated, preventing all the software that runs on the VM from being updated at the same time.

在可用性集内创建 VMCreate VMs inside an availability set

必须在可用性集中创建 VM,确保它们正确地分布在硬件中。VMs must be created within the availability set to make sure they are correctly distributed across the hardware. 在创建可用性集后,无法将现有 VM 添加到可用性集中。An existing VM cannot be added to an availability set after it is created.

通过 az vm create 创建 VM 时,请使用 --availability-set 参数指定可用性集的名称。When a VM is created with az vm create, use the --availability-set parameter to specify the name of the availability set.

for i in `seq 1 2`; do
   az vm create \
     --resource-group myResourceGroupAvailability \
     --name myVM$i \
     --availability-set myAvailabilitySet \
     --size Standard_DS1_v2  \
     --vnet-name myVnet \
     --subnet mySubnet \
     --image UbuntuLTS \
     --admin-username azureuser \

现在,可用性集内有两台虚拟机。There are now two virtual machines within the availability set. 由于它们在同一可用性集中,Azure 会确保 VM 及其所有资源(包括数据磁盘)分布在隔离的物理硬件上。Because they are in the same availability set, Azure ensures that the VMs and all their resources (including data disks) are distributed across isolated physical hardware. 这种分布方式有助于确保提高整体 VM 解决方案的可用性。This distribution helps ensure much higher availability of the overall VM solution.

可以在门户中转到“资源组”> myResourceGroupAvailability > myAvailabilitySet 来查看可用性集分布。The availability set distribution can be viewed in the portal by going to Resource Groups > myResourceGroupAvailability > myAvailabilitySet. VM 分布在两个容错和更新域中,如以下示例所示:The VMs are distributed across the two fault and update domains, as shown in the following example:


检查可用的 VM 大小Check for available VM sizes

以后可以向可用性集添加更多 VM(VM 大小可在硬件上获得)。Additional VMs can be added to the availability set later, where VM sizes are available on the hardware. 使用 az vm availability-set list-sizes 列出可用性集的硬件群集上所有可用的大小:Use az vm availability-set list-sizes to list all the available sizes on the hardware cluster for the availability set:

az vm availability-set list-sizes \
     --resource-group myResourceGroupAvailability \
     --name myAvailabilitySet \
     --output table

后续步骤Next steps

在本教程中,你了解了如何执行以下操作:In this tutorial, you learned how to:

  • 创建可用性集Create an availability set
  • 在可用性集中创建 VMCreate a VM in an availability set
  • 检查可用的 VM 大小Check available VM sizes

请转到下一教程,了解虚拟机规模集。Advance to the next tutorial to learn about virtual machine scale sets.

  • 有关可用性集和可用性区域的更多文档也可以在此处获得。More documentation about both availability sets and availability zones is also available here.