快速入门:使用 Azure PowerShell 创建虚拟机规模集Quickstart: Create a virtual machine scale set with Azure PowerShell

利用虚拟机规模集,可以部署和管理一组自动缩放的虚拟机。A virtual machine scale set allows you to deploy and manage a set of autoscaling 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 like CPU, memory demand, or network traffic. 然后,Azure 负载均衡器会将流量分配到规模集中的 VM 实例。An Azure load balancer then distributes traffic to the VM instances in the scale set. 在本快速入门中,我们将使用 Azure PowerShell 创建虚拟机规模集并部署一个示例应用程序。In this quickstart, you create a virtual machine scale set and deploy a sample application with Azure PowerShell.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a Trial before you begin.

创建规模集Create a scale set

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

New-AzResourceGroup -ResourceGroupName "myResourceGroup" -Location "ChinaNorth"

现在,使用 New-AzVmss 创建虚拟机规模集。Now 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 "myResourceGroup" `
  -Location "ChinaNorth" `
  -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 "myResourceGroup" `
            -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 "myResourceGroup" `
    -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 "myResourceGroup" `
            -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  "myResourceGroup" `
  -Location ChinaNorth `
  -Name myFrontendNSG `
  -SecurityRules $nsgFrontendRule

$vnet = Get-AzVirtualNetwork `
  -ResourceGroupName  "myResourceGroup" `
  -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 "myResourceGroup" `
    -Name "myScaleSet" `
    -VirtualMachineScaleSet $vmss

测试规模集Test your scale set

若要查看正在运行的规模集,请在 Web 浏览器中访问示例 Web 应用程序。To see your scale set in action, access the sample web application in a web browser. 使用 Get-AzPublicIpAddress 获取负载均衡器的公共 IP 地址。Get the public IP address of your load balancer with Get-AzPublicIpAddress. 以下示例显示在 myResourceGroup 资源组中创建的 IP 地址:The following example displays the IP address created in the myResourceGroup resource group:

Get-AzPublicIpAddress -ResourceGroupName "myResourceGroup" | Select IpAddress

将负载均衡器的公共 IP 地址输入到 Web 浏览器中。Enter the public IP address of the load balancer in to a web browser. 负载均衡器将流量分发到某个 VM 实例,如以下示例所示:The load balancer distributes traffic to one of your VM instances, as shown in the following example:

运行 IIS 网站

清理资源Clean up resources

如果不再需要资源组、规模集和所有相关的资源,可以使用 Remove-AzResourceGroup 命令将其删除,如下所示。When no longer needed, you can use the Remove-AzResourceGroup to remove the resource group, scale set, and all related resources as follows. -Force 参数将确认是否希望删除资源,不会显示询问是否删除的额外提示。The -Force parameter confirms that you wish to delete the resources without an additional prompt to do so. -AsJob 参数会使光标返回提示符处,不会等待操作完成。The -AsJob parameter returns control to the prompt without waiting for the operation to complete.

Remove-AzResourceGroup -Name "myResourceGroup" -Force -AsJob

后续步骤Next steps

在本快速入门中,我们已创建一个基本的规模集,并使用自定义脚本扩展在 VM 实例上安装了一个基本的 IIS Web 服务器。In this quickstart, you created a basic scale set and used the Custom Script Extension to install a basic IIS web server on the VM instances. 若要了解详细信息,请继续学习有关如何创建和管理 Azure 虚拟机规模集的教程。To learn more, continue to the tutorial for how to create and manage Azure virtual machine scale sets.