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

本教程介绍如何使用可用性集提高虚拟机 (VM) 的可用性和可靠性。In this tutorial, you learn how to increase the availability and reliability of your Virtual Machines (VMs) using Availability Sets. 可用性集确保在 Azure 上部署的 VM 能够跨群集中多个隔离的硬件节点分布。Availability Sets make sure the VMs you deploy on Azure are distributed across multiple, isolated hardware nodes, in a cluster.

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

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

可用性集概述Availability set overview

可用性集是一种逻辑分组功能,可将部署的 VM 资源相互隔离。An Availability Set is a logical grouping capability for isolating VM resources from each other when they're deployed. Azure 确保可用性集中部署的 VM 能够跨多个物理服务器、计算机架、存储单元和网络交换机运行。Azure makes sure that the VMs you place within an Availability Set run across multiple physical servers, compute racks, storage units, and network switches. 如果发生硬件或软件故障,只有一部分 VM 会受到影响,整体解决方案仍会保持正常运行。If a hardware or software failure happens, only a subset of your VMs are impacted and your overall solution stays operational. 可用性集对于构建可靠的云解决方案至关重要。Availability Sets are essential for building reliable cloud solutions.

假设某个基于 VM 的典型解决方案包含四个前端 Web 服务器,以及 2 个后端 VM。Let's consider a typical VM-based solution where you might have four front-end web servers and 2 back-end VMs. 在 Azure 中,若想在部署 VM 之前先定义两个可用性集:一个用于 Web 层,另一个用于后端层。With Azure, you'd want to define two availability sets before you deploy your VMs: one for the web tier and one for the back tier. 创建新 VM 时,请将可用性集指定为参数。When you create a new VM, you specify the availability set as a parameter. Azure 确保 VM 在多个物理硬件资源之间保持隔离。Azure makes sure the VMs are isolated across multiple physical hardware resources. 如果运行服务器的物理硬件有问题,可以确信服务器的其他实例保持运行,因为它们位于不同的硬件上。If the physical hardware that one of your servers is running on has a problem, you know the other instances of your servers will keep running because they're on different hardware.

在 Azure 中部署基于 VM 的可靠解决方案时,使用可用性集。Use Availability Sets when you want to deploy reliable VM-based solutions in Azure.

启动 Azure PowerShellLaunch Azure PowerShell

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

创建可用性集Create an availability set

同一位置的硬件分为多个更新域和容错域。The hardware in a location is divided in to multiple update domains and fault domains. 更新域 是一组可同时重启的 VM 和基础物理硬件。An update domain is a group of VMs and underlying physical hardware that can be rebooted at the same time. 同一个容错域 内的 VM 共享公用存储,以及公用电源和网络交换机。VMs in the same fault domain share common storage as well as a common power source and network switch.

可以使用 New-AzAvailabilitySet 创建可用性集。You can create an availability set using New-AzAvailabilitySet. 在此示例中,更新域和容错域的数目为 2,可用性集名为 myAvailabilitySetIn this example, the number of both update and fault domains is 2 and the availability set is named myAvailabilitySet.

创建资源组。Create a resource group.

Connect-AzAccount -Environment AzureChinaCloud
New-AzResourceGroup `
   -Name myResourceGroupAvailability `
   -Location ChinaEast

结合 -sku aligned 参数使用 New-AzAvailabilitySet 创建托管的可用性集。Create a managed availability set using New-AzAvailabilitySet with the -sku aligned parameter.

New-AzAvailabilitySet `
   -Location "ChinaEast" `
   -Name "myAvailabilitySet" `
   -ResourceGroupName "myResourceGroupAvailability" `
   -Sku aligned `
   -PlatformFaultDomainCount 2 `
   -PlatformUpdateDomainCount 2

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

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

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

首先,使用 Get-Credential 设置 VM 的管理员用户名和密码:First, set an administrator username and password for the VM with Get-Credential:

$cred = Get-Credential

现在,请使用 New-AzVM 在可用性集中创建两个 VM。Now create two VMs with New-AzVM in the availability set.

for ($i=1; $i -le 2; $i++)
{
    New-AzVm `
        -ResourceGroupName "myResourceGroupAvailability" `
        -Name "myVM$i" `
        -Location "China East" `
        -VirtualNetworkName "myVnet" `
        -SubnetName "mySubnet" `
        -SecurityGroupName "myNetworkSecurityGroup" `
        -PublicIpAddressName "myPublicIpAddress$i" `
        -AvailabilitySetName "myAvailabilitySet" `
        -Credential $cred
}

创建和配置这两个 VM 需要几分钟的时间。It takes a few minutes to create and configure both VMs. 完成后,你将拥有两个跨基础硬件分布的虚拟机。When finished, you have two virtual machines distributed across the underlying hardware.

如果转到“资源组” > “myResourceGroupAvailability” > “myAvailabilitySet”在门户中查看可用性集,应会看到 VM 在两个容错域和更新域之间的分布方式。 If you look at the availability set in the portal by going to Resource Groups > myResourceGroupAvailability > myAvailabilitySet, you should see how the VMs are distributed across the two fault and update domains.

门户中的可用性集

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

稍后可向可用性集添加更多 VM,但需了解在硬件上可用的 VM 大小。You can add more VMs to the availability set later, but you need to know what VM sizes are available on the hardware. 使用 Get-AzVMSize 列出可用性集的硬件群集上的所有可用大小。Use Get-AzVMSize to list all the available sizes on the hardware cluster for the availability set.

Get-AzVMSize `
   -ResourceGroupName "myResourceGroupAvailability" `
   -AvailabilitySetName "myAvailabilitySet"

检查 Azure 顾问Check Azure Advisor

还可使用 Azure 顾问获取有关如何提高 VM 可用性的详细信息。You can also use Azure Advisor to get more information on how to improve the availability of your VMs. Azure 顾问可分析配置和用量遥测数据,然后推荐解决方案来帮助提高 Azure 资源的经济效益、性能、可用性和安全性。Azure Advisor analyzes your configuration and usage telemetry, then recommends solutions that can help you improve the cost effectiveness, performance, availability, and security of your Azure resources.

登录到 Azure 门户,选择“所有服务”,然后键入“顾问” 。Sign in to the Azure portal, select All services, and type Advisor. 顾问仪表板显示针对所选订阅的个性化建议。The Advisor dashboard shows personalized recommendations for the selected subscription. 有关详细信息,请参阅 Azure 顾问入门For more information, see Get started with Azure Advisor.

后续步骤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
  • 检查 Azure 顾问Check Azure Advisor

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