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

在 Windows 上创建虚拟机规模集和部署高度可用的应用Create a Virtual Machine Scale Set and deploy a highly available app on Windows

利用虚拟机规模集,可以部署和管理一组相同的、自动缩放的虚拟机。A virtual machine scale set allows you to deploy and manage a set of identical, auto-scaling virtual machines. 可以手动缩放规模集中的 VM 数,也可以定义规则,以便根据资源使用情况(如 CPU 使用率、内存需求或网络流量)进行自动缩放。You can scale the number of VMs in the scale set manually, or define rules to autoscale based on resource usage such as CPU, memory demand, or network traffic. 在本教程中,会在 Azure 中部署虚拟机规模集。In this tutorial, you deploy a virtual machine scale set in Azure. 学习如何:You learn how to:

  • 使用自定义脚本扩展定义要缩放的 IIS 站点Use the Custom Script Extension to define an IIS site to scale
  • 为规模集创建负载均衡器Create a load balancer for your scale set
  • 创建虚拟机规模集Create a virtual machine scale set
  • 增加或减少规模集中的实例数Increase or decrease the number of instances in a scale set
  • 创建自动缩放规则Create autoscale rules

本教程需要 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.

规模集概述Scale Set overview

利用虚拟机规模集,可以部署和管理一组相同的、自动缩放的虚拟机。A virtual machine scale set allows you to deploy and manage a set of identical, auto-scaling virtual machines. 规模集中的 VM 将分布在逻辑容错域和更新域的一个或多个放置组中。VMs in a scale set are distributed across logic fault and update domains in one or more placement groups. 这些放置组由配置类似的 VM 组成,与可用性集相似。These are groups of similarly configured VMs, similar to availability sets.

可以根据需要在规模集中创建 VM。VMs are created as needed in a scale set. 定义自动缩放规则,以控制如何以及何时在规模集中添加或删除 VM。You define autoscale rules to control how and when VMs are added or removed from the scale set. 这些根据 CPU 负载、内存用量或网络流量等指标触发这些规则。These rules can trigger based on metrics such as CPU load, memory usage, or network traffic.

使用 Azure 平台映像时,规模集最多支持 1,000 个 VM。Scale sets support up to 1,000 VMs when you use an Azure platform image. 对于有重要安装或 VM 自定义要求的工作负荷,可能需要创建自定义 VM 映像For workloads with significant installation or VM customization requirements, you may wish to Create a custom VM image. 使用自定义映像时,在规模集中最多可以创建 300 个 VM。You can create up to 300 VMs in a scale set when using a custom image.

创建用于缩放的应用Create an app to scale

创建规模集之前,需使用 New-AzureRmResourceGroup 创建资源组。Before you can create a scale set, create a resource group with New-AzureRmResourceGroup. 以下示例在“EastUS”位置创建名为“myResourceGroupAutomate”的资源组:The following example creates a resource group named myResourceGroupAutomate in the EastUS location:

New-AzureRmResourceGroup -ResourceGroupName myResourceGroupScaleSet -Location EastUS

在前面的教程中,已了解如何使用自定义脚本扩展来自动执行 VM 配置In an earlier tutorial, you learned how to Automate VM configuration using the Custom Script Extension. 创建规模集配置,并应用自定义脚本扩展,安装和配置 IIS:Create a scale set configuration then apply a Custom Script Extension to install and configure IIS:

# Create a config object
$vmssConfig = New-AzureRmVmssConfig `
    -Location EastUS `
    -SkuCapacity 2 `
    -SkuName Standard_DS2 `
    -UpgradePolicyMode Automatic

# Define the script for your Custom Script Extension to run
$publicSettings = @{
    "fileUris" = (,"https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/automate-iis.ps1");
    "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File automate-iis.ps1"
}

# Use Custom Script Extension to install IIS and configure basic website
Add-AzureRmVmssExtension -VirtualMachineScaleSet $vmssConfig `
    -Name "customScript" `
    -Publisher "Microsoft.Compute" `
    -Type "CustomScriptExtension" `
    -TypeHandlerVersion 1.8 `
    -Setting $publicSettings

创建规模负载均衡器Create scale load balancer

Azure 负载均衡器是位于第 4 层(TCP、UDP)的负载均衡器,通过在正常运行的 VM 之间分发传入流量提供高可用性。An Azure load balancer is a Layer-4 (TCP, UDP) load balancer that provides high availability by distributing incoming traffic among healthy VMs. 负载均衡器的运行状况探测监视每个 VM 上的给定端口,并仅将流量分发给可操作的 VM。A load balancer health probe monitors a given port on each VM and only distributes traffic to an operational VM. 有关详细信息,请参阅有关如何负载均衡 Windows 虚拟机的下一个教程。For more information, see the next tutorial on How to load balance Windows virtual machines.

创建具有公共 IP 地址、在端口 80 上分布 Web 流量的负载均衡器:Create a load balancer that has a public IP address and distributes web traffic on port 80:

# Create a public IP address
$publicIP = New-AzureRmPublicIpAddress `
  -ResourceGroupName myResourceGroupScaleSet `
  -Location EastUS `
  -AllocationMethod Static `
  -Name myPublicIP

# Create a frontend and backend IP pool
$frontendIP = New-AzureRmLoadBalancerFrontendIpConfig `
  -Name myFrontEndPool `
  -PublicIpAddress $publicIP
$backendPool = New-AzureRmLoadBalancerBackendAddressPoolConfig -Name myBackEndPool

# Create the load balancer
$lb = New-AzureRmLoadBalancer `
  -ResourceGroupName myResourceGroupScaleSet `
  -Name myLoadBalancer `
  -Location EastUS `
  -FrontendIpConfiguration $frontendIP `
  -BackendAddressPool $backendPool

# Create a load balancer health probe on port 80
Add-AzureRmLoadBalancerProbeConfig -Name myHealthProbe `
  -LoadBalancer $lb `
  -Protocol tcp `
  -Port 80 `
  -IntervalInSeconds 15 `
  -ProbeCount 2

# Create a load balancer rule to distribute traffic on port 80
Add-AzureRmLoadBalancerRuleConfig `
  -Name myLoadBalancerRule `
  -LoadBalancer $lb `
  -FrontendIpConfiguration $lb.FrontendIpConfigurations[0] `
  -BackendAddressPool $lb.BackendAddressPools[0] `
  -Protocol Tcp `
  -FrontendPort 80 `
  -BackendPort 80

# Update the load balancer configuration
Set-AzureRmLoadBalancer -LoadBalancer $lb

创建规模集Create a scale set

现在,使用 New-AzureRmVmss 创建虚拟机规模集。Now create a virtual machine scale set with New-AzureRmVmss. 下列示例创建名为“myScaleSet”的规模集:The following example creates a scale set named myScaleSet:

# Reference a virtual machine image from the gallery
Set-AzureRmVmssStorageProfile $vmssConfig `
  -ImageReferencePublisher MicrosoftWindowsServer `
  -ImageReferenceOffer WindowsServer `
  -ImageReferenceSku 2016-Datacenter `
  -ImageReferenceVersion latest

# Set up information for authenticating with the virtual machine
Set-AzureRmVmssOsProfile $vmssConfig `
  -AdminUsername azureuser `
  -AdminPassword P@ssword! `
  -ComputerNamePrefix myVM

# Create the virtual network resources
$subnet = New-AzureRmVirtualNetworkSubnetConfig `
  -Name "mySubnet" `
  -AddressPrefix 10.0.0.0/24
$vnet = New-AzureRmVirtualNetwork `
  -ResourceGroupName "myResourceGroupScaleSet" `
  -Name "myVnet" `
  -Location "EastUS" `
  -AddressPrefix 10.0.0.0/16 `
  -Subnet $subnet
$ipConfig = New-AzureRmVmssIpConfig `
  -Name "myIPConfig" `
  -LoadBalancerBackendAddressPoolsId $lb.BackendAddressPools[0].Id `
  -SubnetId $vnet.Subnets[0].Id

# Attach the virtual network to the config object
Add-AzureRmVmssNetworkInterfaceConfiguration `
  -VirtualMachineScaleSet $vmssConfig `
  -Name "network-config" `
  -Primary $true `
  -IPConfiguration $ipConfig

# Create the scale set with the config object (this step might take a few minutes)
New-AzureRmVmss `
  -ResourceGroupName myResourceGroupScaleSet `
  -Name myScaleSet `
  -VirtualMachineScaleSet $vmssConfig

创建和配置所有的规模集资源和 VM 需要几分钟时间。It takes a few minutes to create and configure all the scale set resources and VMs.

测试应用Test your app

要查看运行中的 IIS 网站,使用 Get-AzureRmPublicIPAddress 获取负载均衡器的公共 IP 地址。To see your IIS website in action, obtain the public IP address of your load balancer with Get-AzureRmPublicIPAddress. 以下示例获取创建为规模集一部分的“myPublicIP”的 IP 地址:The following example obtains the IP address for myPublicIP created as part of the scale set:

Get-AzureRmPublicIPAddress -ResourceGroupName myResourceGroupScaleSet -Name myPublicIP | select IpAddress

将公共 IP 地址输入到 Web 浏览器中。Enter the public IP address in to a web browser. 将显示应用,包括负载均衡器将流量分发到的 VM 的主机名:The app is displayed, including the hostname of the VM that the load balancer distributed traffic to:

运行 IIS 网站

若要查看规模集的运作方式,可以强制刷新 Web 浏览器,以查看负载均衡器如何在运行应用的所有 VM 之间分配流量。To see the scale set in action, you can force-refresh your web browser to see the load balancer distribute traffic across all the VMs running your app.

管理任务Management tasks

在规模集的整个生命周期内,可能需要运行一个或多个管理任务。Throughout the lifecycle of the scale set, you may need to run one or more management tasks. 此外,可能还需要创建自动执行各种生命周期任务的脚本。Additionally, you may want to create scripts that automate various lifecycle-tasks. Azure PowerShell 提供一种用于执行这些任务的快速方法。Azure PowerShell provides a quick way to do those tasks. 以下是一些常见任务。Here are a few common tasks.

查看规模集中的 VMView VMs in a scale set

若要查看规模集中运行的 VM 列表,请使用 Get-AzureRmVmssVM,如下所示:To view a list of VMs running in your scale set, use Get-AzureRmVmssVM as follows:

# Get current scale set
$scaleset = Get-AzureRmVmss `
  -ResourceGroupName myResourceGroupScaleSet `
  -VMScaleSetName myScaleSet

# Loop through the instanaces in your scale set
for ($i=1; $i -le ($scaleset.Sku.Capacity - 1); $i++) {
    Get-AzureRmVmssVM -ResourceGroupName myResourceGroupScaleSet `
      -VMScaleSetName myScaleSet `
      -InstanceId $i
}

增加或减少 VM 实例Increase or decrease VM instances

若要查看规模集中当前包含的实例数,请使用 Get-AzureRmVmss 并查询 sku.capacity:To see the number of instances you currently have in a scale set, use Get-AzureRmVmss and query on sku.capacity:

Get-AzureRmVmss -ResourceGroupName myResourceGroupScaleSet `
    -VMScaleSetName myScaleSet | `
    Select -ExpandProperty Sku

然后,可以使用 Update-AzureRmVmss 手动增加或减少规模集中虚拟机的数目。You can then manually increase or decrease the number of virtual machines in the scale set with Update-AzureRmVmss. 以下示例将规模集中 VM 的数目设置为“5”:The following example sets the number of VMs in your scale set to 5:

# Get current scale set
$scaleset = Get-AzureRmVmss `
  -ResourceGroupName myResourceGroupScaleSet `
  -VMScaleSetName myScaleSet

# Set and update the capacity of your scale set
$scaleset.sku.capacity = 5
Update-AzureRmVmss -ResourceGroupName myResourceGroupScaleSet `
    -Name myScaleSet `
    -VirtualMachineScaleSet $scaleset

这会花费数分钟来更新规模集中实例的指定数量。If takes a few minutes to update the specified number of instances in your scale set.

配置自动缩放规则Configure autoscale rules

定义自动缩放规则,而不是手动缩放规模集中实例的数目。Rather than manually scaling the number of instances in your scale set, you define autoscale rules. 这些规则监视规模集中的实例,并根据所定义的指标和阈值做出相应响应。These rules monitor the instances in your scale set and respond accordingly based on metrics and thresholds you define. 如果平均 CPU 负载高于 60% 且持续时间超过 5 分钟,以下示例将增加一个实例。The following example scales out the number of instances by one when the average CPU load is greater than 60% over a 5 minute period. 如果平均 CPU 负载低于 30% 且持续时间超过 5 分钟,则将减少一个实例:If the average CPU load then drops below 30% over a 5 minute period, the instances are scaled in by one instance:

# Define your scale set information
$mySubscriptionId = (Get-AzureRmSubscription).Id
$myResourceGroup = "myResourceGroupScaleSet"
$myScaleSet = "myScaleSet"
$myLocation = "East US"

# Create a scale up rule to increase the number instances after 60% average CPU usage exceeded for a 5 minute period
$myRuleScaleUp = New-AzureRmAutoscaleRule `
  -MetricName "Percentage CPU" `
  -MetricResourceId /subscriptions/$mySubscriptionId/resourceGroups/$myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/$myScaleSet `
  -Operator GreaterThan `
  -MetricStatistic Average `
  -Threshold 60 `
  -TimeGrain 00:01:00 `
  -TimeWindow 00:05:00 `
  -ScaleActionCooldown 00:05:00 `
  -ScaleActionDirection Increase `
  -ScaleActionValue 1

# Create a scale down rule to decrease the number of instances after 30% average CPU usage over a 5 minute period
$myRuleScaleDown = New-AzureRmAutoscaleRule `
  -MetricName "Percentage CPU" `
  -MetricResourceId /subscriptions/$mySubscriptionId/resourceGroups/$myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/$myScaleSet `
  -Operator LessThan `
  -MetricStatistic Average `
  -Threshold 30 `
  -TimeGrain 00:01:00 `
  -TimeWindow 00:05:00 `
  -ScaleActionCooldown 00:05:00 `
  -ScaleActionDirection Decrease `
  -ScaleActionValue 1

# Create a scale profile with your scale up and scale down rules
$myScaleProfile = New-AzureRmAutoscaleProfile `
  -DefaultCapacity 2  `
  -MaximumCapacity 10 `
  -MinimumCapacity 2 `
  -Rules $myRuleScaleUp,$myRuleScaleDown `
  -Name "autoprofile"

# Apply the autoscale rules
Add-AzureRmAutoscaleSetting `
  -Location $myLocation `
  -Name "autosetting" `
  -ResourceGroup $myResourceGroup `
  -TargetResourceId /subscriptions/$mySubscriptionId/resourceGroups/$myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/$myScaleSet `
  -AutoscaleProfiles $myScaleProfile

有关使用自动缩放的详细设计信息,请参阅自动缩放最佳做法For more design information on the use of autoscale, see autoscale best practices.

后续步骤Next steps

在本教程中,已创建虚拟机规模集。In this tutorial, you created a virtual machine scale set. 你已了解如何:You learned how to:

  • 使用自定义脚本扩展定义要缩放的 IIS 站点Use the Custom Script Extension to define an IIS site to scale
  • 为规模集创建负载均衡器Create a load balancer for your scale set
  • 创建虚拟机规模集Create a virtual machine scale set
  • 增加或减少规模集中的实例数Increase or decrease the number of instances in a scale set
  • 创建自动缩放规则Create autoscale rules

请继续学习下一篇教程,详细了解虚拟机的负载均衡概念。Advance to the next tutorial to learn more about load balancing concepts for virtual machines.