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

教程:使用 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, 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 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/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.7.0 或更高版本。If you choose to install and use the PowerShell locally, this tutorial requires the Azure PowerShell module version 5.7.0 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,则还需运行 Connect-AzureRmAccount 以创建与 Azure 的连接。If you are running PowerShell locally, you also need to run Connect-AzureRmAccount to create a connection with Azure.

规模集概述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 a scale set

使用 New-AzureRmVmss 创建虚拟机规模集。Create a virtual machine scale set with New-AzureRmVmss. 以下示例创建名为 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, provide your own desired administrative credentials for the VM instances in the scale set:

New-AzureRmVmss `
  -ResourceGroupName "myResourceGroupScaleSet" `
  -Location "EastUS" `
  -VMScaleSetName "myScaleSet" `
  -VirtualNetworkName "myVnet" `
  -SubnetName "mySubnet" `
  -PublicIpAddressName "myPublicIPAddress" `
  -LoadBalancerName "myLoadBalancer" `
  -UpgradePolicy "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-AzureRmVmss `
            -ResourceGroupName "myResourceGroupScaleSet" `
            -VMScaleSetName "myScaleSet"

# Use Custom Script Extension to install IIS and configure basic website
Add-AzureRmVmssExtension -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-AzureRmVmss `
    -ResourceGroupName "myResourceGroupScaleSet" `
    -Name "myScaleSet" `
    -VirtualMachineScaleSet $vmss

测试规模集Test your scale set

若要查看运行中的规模集,请使用 Get-AzureRmPublicIPAddress 获取负载均衡器的公共 IP 地址。To see your scale set 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 "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-AzureRmVmssVM,如下所示:To view a list of VM instances in a scale set, use Get-AzureRmVmssVM as follows:

Get-AzureRmVmssVM -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   eastus Standard_DS1_v2          0         Succeeded
MYRESOURCEGROUPSCALESET   myScaleSet_1   eastus Standard_DS1_v2          1         Succeeded

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

Get-AzureRmVmssVM -ResourceGroupName "myResourceGroupScaleSet" -VMScaleSetName "myScaleSet" -InstanceId "1"

增加或减少 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 的数目设置为 3The following example sets the number of VMs in your scale set to 3:

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

# Set and update the capacity of your scale set
$scaleset.sku.capacity = 3
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. 如果在 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. 如果在 5 分钟内平均 CPU 负载低于 30%,则将减少一个实例: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)[0].Id
$myResourceGroup = "myResourceGroupScaleSet"
$myScaleSet = "myScaleSet"
$myLocation = "East US"
$myScaleSetId = (Get-AzureRmVmss -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-AzureRmAutoscaleRule `
  -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-AzureRmAutoscaleRule `
  -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-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 $myScaleSetId `
  -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.