您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

如何使用可用性集How to use availability sets

本教程介绍如何使用称作“可用性集”的功能提高 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 nodes in a cluster. 这样,就可以确保当 Azure 中发生硬件或软件故障时,只有一部分 VM 会受到影响,整体解决方案仍可使用和操作。Doing this ensures that if a hardware or software failure within Azure happens, only a subset of your VMs are 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 顾问Check Azure Advisor

启动 Azure Cloud ShellLaunch Azure Cloud Shell

Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的步骤。The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. 它预安装有常用 Azure 工具并将其配置与帐户一起使用。It has common Azure tools preinstalled and configured to use with your account. 请直接单击“复制”对代码进行复制,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。Just click the Copy to copy the code, paste it into the Cloud Shell, and then press enter to run it. 可通过多种方式来启动 Cloud Shell:There are a few ways to launch the Cloud Shell:

单击代码块右上角的“试用”。Click Try It in the upper right corner of a code block. 本文中的 Cloud Shell
在浏览器中打开 Cloud Shell。Open Cloud Shell in your browser. https://shell.azure.com/powershellhttps://shell.azure.com/powershell
单击 Azure 门户右上角菜单上的“Cloud Shell”按钮。Click the Cloud Shell button on the menu in the upper right of the Azure portal. 门户中的 Cloud ShellCloud Shell in the portal

如果选择在本地安装并使用 PowerShell,则本教程需要 Azure PowerShell 模块 5.3 或更高版本。If you choose to install and use the PowerShell locally, this tutorial requires the Azure PowerShell module version 5.3 or later. 运行 Get-Module -ListAvailable AzureRM 即可查找版本。Run Get-Module -ListAvailable AzureRM to find the version. 如果需要升级,请参阅安装 Azure PowerShell 模块If you need to upgrade, see Install Azure PowerShell module. 如果在本地运行 PowerShell,则还需运行 Login-AzureRmAccount 以创建与 Azure 的连接。If you are running PowerShell locally, you also need to run Login-AzureRmAccount to create a connection with Azure.

可用性集概述Availability set overview

可用性集是一种逻辑分组功能,在 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 2 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.

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

创建可用性集Create an availability set

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

创建资源组。Create a resource group.

New-AzureRmResourceGroup -Name myResourceGroupAvailability -Location EastUS

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

New-AzureRmAvailabilitySet `
   -Location "EastUS" `
   -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 are correctly distributed across the hardware. 创建后,无法将现有 VM 添加到可用性集中。You can't add an existing VM to an availability set after it is created.

同一位置的硬件分为多个更新域和容错域。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-AzureRmVM 创建 VM 时,请使用 -AvailabilitySetName 参数指定可用性集的名称。When you create a VM with New-AzureRmVM, 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-AzureRmVM 创建两个 VM。Now create two VMs with New-AzureRmVM in the availability set.

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

-AsJob 参数以后台任务的方式创建 VM,因此 PowerShell 提示符会返回到你所在的位置。The -AsJob parameter creates the VM as a background task, so the PowerShell prompts return to you. 可以通过 Job cmdlet 查看后台作业的详细信息。You can view details of background jobs with the Job cmdlet. 创建和配置这两个 VM 需要几分钟的时间完成。It takes a few minutes to create and configure both VMs. 完成后,你将拥有两个跨基础硬件分布的虚拟机。When finished, you have two virtual machines distributed across the underlying hardware.

如果通过转到“资源组”>“我的资源组可用性”>“我的可用性集”在门户中查看可用性集,则应查看如何跨两个容错域和更新域分布 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-AzureRMVMSize 列出可用性集的硬件群集上所有可用的大小。Use Get-AzureRMVMSize to list all the available sizes on the hardware cluster for the availability set.

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

检查 Azure 顾问Check Azure Advisor

还可使用 Azure 顾问获取有关提高 VM 可用性的方法的详细信息。You can also use Azure Advisor to get more information on ways to improve the availability of your VMs. Azure 顾问可帮助遵循最佳做法来优化 Azure 部署。Azure Advisor helps you follow best practices to optimize your Azure deployments. 它可分析资源配置和遥测使用情况,并推荐解决方案,有助于提高 Azure 资源的经济效益、性能、高可用性和安全性。It analyzes your resource configuration and usage telemetry and then recommends solutions that can help you improve the cost effectiveness, performance, high availability, and security of your Azure resources.

登录到 Azure 门户,选择“所有服务”,然后键入“顾问”。Sign in to the Azure portal, select All services, and type Advisor. 顾问仪表板显示针对所选订阅的个性化建议。The Advisor dashboard displays 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.