您现在访问的是微软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 sub-set 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 PowerShell 模块 3.6 或更高版本。This tutorial requires the Azure PowerShell module version 3.6 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.

可用性集概述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 的典型解决方案包含 4 个前端 Web 服务器,以及 2 个托管数据库的后端 VM。Let’s consider a typical VM-based solution where you might have 4 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, we 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-AzureRMVMConfig 创建 VM 配置时,可以使用 -AvailabilitySetId 参数指定可用性集的 ID。When you create a VM configuration using New-AzureRMVMConfig you use the -AvailabilitySetId parameter to specify the ID of the availability set.

在可用性集中使用 New-AzureRmVM 创建两个 VM。Create two VMs with New-AzureRmVM in the availability set.

$availabilitySet = Get-AzureRmAvailabilitySet `
    -ResourceGroupName myResourceGroupAvailability `
    -Name myAvailabilitySet

$cred = Get-Credential -Message "Enter a username and password for the virtual machine."

$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig `
    -Name mySubnet `
    -AddressPrefix 192.168.1.0/24
$vnet = New-AzureRmVirtualNetwork `
    -ResourceGroupName myResourceGroupAvailability `
    -Location EastUS `
    -Name myVnet `
    -AddressPrefix 192.168.0.0/16 `
    -Subnet $subnetConfig

$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig `
    -Name myNetworkSecurityGroupRuleRDP `
    -Protocol Tcp `
    -Direction Inbound `
    -Priority 1000 `
    -SourceAddressPrefix * `
    -SourcePortRange * `
    -DestinationAddressPrefix * `
    -DestinationPortRange 3389 `
    -Access Allow

$nsg = New-AzureRmNetworkSecurityGroup `
    -Location eastus `
    -Name myNetworkSecurityGroup `
    -ResourceGroupName myResourceGroupAvailability `
    -SecurityRules $nsgRuleRDP

# Apply the network security group to a subnet
Set-AzureRmVirtualNetworkSubnetConfig `
    -VirtualNetwork $vnet `
    -Name mySubnet `
    -NetworkSecurityGroup $nsg `
    -AddressPrefix 192.168.1.0/24

# Update the virtual network
Set-AzureRmVirtualNetwork -VirtualNetwork $vnet

for ($i=1; $i -le 2; $i++)
{
   $pip = New-AzureRmPublicIpAddress `
        -ResourceGroupName myResourceGroupAvailability `
        -Location EastUS `
        -Name "mypublicdns$(Get-Random)" `
        -AllocationMethod Static `
        -IdleTimeoutInMinutes 4

   $nic = New-AzureRmNetworkInterface `
        -Name myNic$i `
        -ResourceGroupName myResourceGroupAvailability `
        -Location EastUS `
        -SubnetId $vnet.Subnets[0].Id `
        -PublicIpAddressId $pip.Id `
        -NetworkSecurityGroupId $nsg.Id

   # Here is where we specify the availability set
   $vm = New-AzureRmVMConfig `
        -VMName myVM$i `
        -VMSize Standard_D1 `
        -AvailabilitySetId $availabilitySet.Id

   $vm = Set-AzureRmVMOperatingSystem `
        -ComputerName myVM$i `
        -Credential $cred `
        -VM $vm `
        -Windows `
        -EnableAutoUpdate `
        -ProvisionVMAgent
   $vm = Set-AzureRmVMSourceImage `
        -VM $vm `
        -PublisherName MicrosoftWindowsServer `
        -Offer WindowsServer `
        -Skus 2016-Datacenter `
        -Version latest
   $vm = Set-AzureRmVMOSDisk `
        -VM $vm `
        -Name myOsDisk$i `
        -DiskSizeInGB 128 `
        -CreateOption FromImage `
        -Caching ReadWrite
   $vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
   New-AzureRmVM `
        -ResourceGroupName myResourceGroupAvailability `
        -Location EastUS `
        -VM $vm
}

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

如果通过转到“资源组”>“我的资源组可用性”>“我的可用性集”在门户中查看可用性集,则应查看如何跨 2 个容错域和更新域分布 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 2 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 `
   -AvailabilitySetName myAvailabilitySet `
   -ResourceGroupName myResourceGroupAvailability  

检查 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 More 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.