教程:使用 Azure PowerShell 在 Windows 上创建虚拟机规模集和部署高度可用的应用Tutorial: Create a virtual machine scale set and deploy a highly available app on Windows with Azure PowerShell

利用虚拟机规模集,可以部署和管理一组相同的、自动缩放的虚拟机。A virtual machine scale set allows you to deploy and manage a set of identical, autoscaling virtual machines. 可以手动缩放规模集中的 VM 数。You can scale the number of VMs in the scale set manually. 也可以定义规则,以便根据 CPU、内存需求或网络流量等资源使用情况进行自动缩放。You can also 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 and 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 本地 ShellLaunch Azure local Shell

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块This article has been updated to use the new Azure PowerShell Az module. AzureRM 将继续获取关键的 bug 修复更新,但新功能将只出现在 Az 模块中。AzureRM will continue to get critical bugfix updates, but new features will be in the Az module only.

  • 若要在本地安装 Az 模块,请参阅安装 Azure PowerShellTo install the Az module locally, see Install Azure PowerShell.
  • 如果在本地安装 Az 模块,可通过运行 Enable-AzureRmAlias 来启用 AzureRM 兼容性。If you install the Az module locally, you can enable the AzureRM compatibility by running Enable-AzureRmAlias.

规模集概述Scale Set overview

利用虚拟机规模集,可以部署和管理一组相同的、自动缩放的虚拟机。A virtual machine scale set allows you to deploy and manage a set of identical, autoscaling virtual machines. 规模集中的 VM 将分布在逻辑容错域和更新域的一个或多个放置组中。VMs in a scale set are distributed across logic fault and update domains in one or more placement groups. 放置组是配置类似的 VM 的组,与可用性集相似。Placement groups 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 a scale set

使用 New-AzVmss 创建虚拟机规模集。Create a virtual machine scale set with New-AzVmss. 以下示例创建名为 myScaleSet 且使用 Windows Server 2016 Datacenter 平台映像的规模集。The following example creates a scale set named myScaleSet that uses the Windows Server 2016 Datacenter platform image. 虚拟网络、公共 IP 地址和负载均衡器的 Azure 网络资源均会自动创建。The Azure network resources for virtual network, public IP address, and load balancer are automatically created. 出现提示时,可以针对规模集中的 VM 实例设置自己的管理凭据:When prompted, you can set your own administrative credentials for the VM instances in the scale set:

New-AzVmss `
  -ResourceGroupName "myResourceGroupScaleSet" `
  -Location "ChinaEast" `
  -VMScaleSetName "myScaleSet" `
  -VirtualNetworkName "myVnet" `
  -SubnetName "mySubnet" `
  -PublicIpAddressName "myPublicIPAddress" `
  -LoadBalancerName "myLoadBalancer" `
  -UpgradePolicyMode "Automatic"

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

部署示例应用程序Deploy sample application

若要测试规模集,请安装一个基本的 Web 应用程序。To test your scale set, install a basic web application. 使用 Azure 自定义脚本扩展下载并运行一个脚本,以便在 VM 实例上安装 IIS。The Azure Custom Script Extension is used to download and run a script that installs IIS on the VM instances. 此扩展适用于部署后配置、软件安装或其他任何配置/管理任务。This extension is useful for post deployment configuration, software installation, or any other configuration / management task. 有关详细信息,请参阅自定义脚本扩展概述For more information, see the Custom Script Extension overview.

使用自定义脚本扩展安装基本的 IIS Web 服务器。Use the Custom Script Extension to install a basic IIS web server. 应用可安装 IIS 的自定义脚本扩展,如下所示:Apply the Custom Script Extension that installs IIS as follows:

# 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"
}

# Get information about the scale set
$vmss = Get-AzVmss `
  -ResourceGroupName "myResourceGroupScaleSet" `
  -VMScaleSetName "myScaleSet"

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

# Update the scale set and apply the Custom Script Extension to the VM instances
Update-AzVmss `
  -ResourceGroupName "myResourceGroupScaleSet" `
  -Name "myScaleSet" `
  -VirtualMachineScaleSet $vmss

允许流量发往应用程序Allow traffic to application

若要允许访问基本的 Web 应用程序,请使用 New-AzNetworkSecurityRuleConfigNew-AzNetworkSecurityGroup 创建网络安全组。To allow access to the basic web application, create a network security group with New-AzNetworkSecurityRuleConfig and New-AzNetworkSecurityGroup. 有关详细信息,请参阅 Azure 虚拟机规模集的网络For more information, see Networking for Azure virtual machine scale sets.

# Get information about the scale set
$vmss = Get-AzVmss `
  -ResourceGroupName "myResourceGroupScaleSet" `
  -VMScaleSetName "myScaleSet"

#Create a rule to allow traffic over port 80
$nsgFrontendRule = New-AzNetworkSecurityRuleConfig `
  -Name myFrontendNSGRule `
  -Protocol Tcp `
  -Direction Inbound `
  -Priority 200 `
  -SourceAddressPrefix * `
  -SourcePortRange * `
  -DestinationAddressPrefix * `
  -DestinationPortRange 80 `
  -Access Allow

#Create a network security group and associate it with the rule
$nsgFrontend = New-AzNetworkSecurityGroup `
  -ResourceGroupName  "myResourceGroupScaleSet" `
  -Location ChinaEast `
  -Name myFrontendNSG `
  -SecurityRules $nsgFrontendRule

$vnet = Get-AzVirtualNetwork `
  -ResourceGroupName  "myResourceGroupScaleSet" `
  -Name myVnet

$frontendSubnet = $vnet.Subnets[0]

$frontendSubnetConfig = Set-AzVirtualNetworkSubnetConfig `
  -VirtualNetwork $vnet `
  -Name mySubnet `
  -AddressPrefix $frontendSubnet.AddressPrefix `
  -NetworkSecurityGroup $nsgFrontend

Set-AzVirtualNetwork -VirtualNetwork $vnet

# Update the scale set and apply the Custom Script Extension to the VM instances
Update-AzVmss `
  -ResourceGroupName "myResourceGroupScaleSet" `
  -Name "myScaleSet" `
  -VirtualMachineScaleSet $vmss

测试规模集Test your scale set

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

Get-AzPublicIPAddress `
  -ResourceGroupName "myResourceGroupScaleSet" `
  -Name "myPublicIPAddress" | select IpAddress

将公共 IP 地址输入到 Web 浏览器中。Enter the public IP address in to a web browser. 将显示 Web 应用,包括负载均衡器将流量分发到的 VM 的主机名:The web 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-AzVmssVM,如下所示:To view a list of VM instances in a scale set, use Get-AzVmssVM as follows:

Get-AzVmssVM `
  -ResourceGroupName "myResourceGroupScaleSet" `
  -VMScaleSetName "myScaleSet"

以下示例输出显示了规模集中的两个 VM 实例:The following example output shows two VM instances in the scale set:

ResourceGroupName                 Name Location             Sku InstanceID ProvisioningState
-----------------                 ---- --------             --- ---------- -----------------
MYRESOURCEGROUPSCALESET   myScaleSet_0   chinaeast Standard_DS1_v2          0         Succeeded
MYRESOURCEGROUPSCALESET   myScaleSet_1   chinaeast Standard_DS1_v2          1         Succeeded

若要查看特定 VM 实例的其他信息,请将 -InstanceId 参数添加到 Get-AzVmssVMTo view additional information about a specific VM instance, add the -InstanceId parameter to Get-AzVmssVM. 以下示例查看 VM 实例 1 的相关信息:The following example views information about VM instance 1:

Get-AzVmssVM `
  -ResourceGroupName "myResourceGroupScaleSet" `
  -VMScaleSetName "myScaleSet" `
  -InstanceId "1"

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

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

Get-AzVmss -ResourceGroupName "myResourceGroupScaleSet" `
  -VMScaleSetName "myScaleSet" | `
  Select -ExpandProperty Sku

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

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

# Set and update the capacity of your scale set
$scaleset.sku.capacity = 3
Update-AzVmss -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. 如果在 5 分钟内平均 CPU 负载高于 60%,以下示例将增加一个实例。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-AzSubscription)[0].Id
$myResourceGroup = "myResourceGroupScaleSet"
$myScaleSet = "myScaleSet"
$myLocation = "China East"
$myScaleSetId = (Get-AzVmss -ResourceGroupName $myResourceGroup -VMScaleSetName $myScaleSet).Id 

# Create a scale up rule to increase the number instances after 60% average CPU usage exceeded for a 5-minute period
$myRuleScaleUp = New-AzAutoscaleRule `
  -MetricName "Percentage CPU" `
  -MetricResourceId $myScaleSetId `
  -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-AzAutoscaleRule `
  -MetricName "Percentage CPU" `
  -MetricResourceId $myScaleSetId `
  -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-AzAutoscaleProfile `
  -DefaultCapacity 2  `
  -MaximumCapacity 10 `
  -MinimumCapacity 2 `
  -Rule $myRuleScaleUp,$myRuleScaleDown `
  -Name "autoprofile"

# Apply the autoscale rules
Add-AzAutoscaleSetting `
  -Location $myLocation `
  -Name "autosetting" `
  -ResourceGroup $myResourceGroup `
  -TargetResourceId $myScaleSetId `
  -AutoscaleProfile $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.