教程:使用 Azure PowerShell 在虚拟机规模集中安装应用程序Tutorial: Install applications in virtual machine scale sets with Azure PowerShell

若要在规模集中的虚拟机 (VM) 实例上运行应用程序,首先需要安装应用程序组件和所需文件。To run applications on virtual machine (VM) instances in a scale set, you first need to install the application components and required files. 前一篇教程介绍了如何创建自定义 VM 映像并使用它来部署 VM 实例。In a previous tutorial, you learned how to create and use a custom VM image to deploy your VM instances. 使用此自定义映像可以手动安装和配置应用程序。This custom image included manual application installs and configurations. 也可以在部署每个 VM 实例之后,将应用程序自动安装到规模集,或者更新已在规模集中运行的应用程序。You can also automate the install of applications to a scale set after each VM instance is deployed, or update an application that already runs on a scale set. 本教程介绍如何执行下列操作:In this tutorial you learn how to:

  • 将应用程序自动安装到规模集Automatically install applications to your scale set
  • 使用 Azure 自定义脚本扩展Use the Azure Custom Script Extension
  • 更新规模集中运行的应用程序Update a running application on a scale set

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

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

什么是 Azure 自定义脚本扩展?What is the Azure Custom Script Extension?

自定义脚本扩展在 Azure VM 上下载和执行脚本。The Custom Script Extension downloads and executes scripts on Azure VMs. 此扩展适用于部署后配置、软件安装或其他任何配置/管理任务。This extension is useful for post deployment configuration, software installation, or any other configuration / management task. 可以从 Azure 存储或 GitHub 下载脚本,或者在扩展运行时将脚本提供给 Azure 门户。Scripts can be downloaded from Azure storage or GitHub, or provided to the Azure portal at extension run-time.

将自定义脚本扩展与 Azure 资源管理器模板集成。The Custom Script extension integrates with Azure Resource Manager templates. 它还可以与 Azure CLI、Azure PowerShell、Azure 门户或 REST API 配合使用。It can also be used with the Azure CLI, Azure PowerShell, Azure portal, or the REST API. 有关详细信息,请参阅自定义脚本扩展概述For more information, see the Custom Script Extension overview.

若要查看自定义脚本扩展的运行方式,请创建一个可以安装 IIS Web 服务器并输出规模集 VM 实例主机名的规模集。To see the Custom Script Extension in action, create a scale set that installs the IIS web server and outputs the hostname of the scale set VM instance. 自定义脚本扩展定义从 GitHub 下载示例脚本,安装所需的包,然后将 VM 实例主机名写入一个基本的 HTML 页面。The Custom Script Extension definition downloads a sample script from GitHub, installs the required packages, then writes the VM instance hostname to a basic HTML page.

创建规模集Create a scale set

现在,使用 New-AzVmss 创建虚拟机规模集。Now create a virtual machine scale set with New-AzVmss. 若要将流量分配到单独的 VM 实例,则还要创建负载均衡器。To distribute traffic to the individual VM instances, a load balancer is also created. 负载均衡器包含在 TCP 端口 80 上分发流量所需的规则。The load balancer includes rules to distribute traffic on TCP port 80. 它还允许 TCP 端口 3389 上的远程桌面流量,以及 TCP 端口 5985 上的 PowerShell 远程流量。It also allows remote desktop traffic on TCP port 3389 and PowerShell remoting on TCP port 5985. 出现提示时,可以针对规模集中的 VM 实例设置自己的管理凭据:When prompted, you can set your own administrative credentials for the VM instances in the scale set:

New-AzVmss `
  -ResourceGroupName "myResourceGroup" `
  -VMScaleSetName "myScaleSet" `
  -Location "ChinaNorth" `
  -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.

创建自定义脚本扩展定义Create Custom Script Extension definition

Azure PowerShell 使用哈希表存储要下载的文件和要执行的命令。Azure PowerShell uses a hashtable to store the file to download and the command to execute. 以下示例使用 GitHub 中的示例脚本。In the following example, a sample script from GitHub is used. 首先,按如下所示创建此配置对象:First, create this configuration object as follows:

$customConfig = @{
  "fileUris" = (,"https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/automate-iis.ps1");
  "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File automate-iis.ps1"
}

接下来,使用 Add-AzVmssExtension 应用自定义脚本扩展。Now, apply the Custom Script Extension with Add-AzVmssExtension. 将前面定义的配置对象传递给扩展。The configuration object previously defined is passed to the extension. 使用 Update-AzVmss 在 VM 实例上更新并运行扩展。Update and run the extension on the VM instances with Update-AzVmss.

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

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

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

规模集中的每个 VM 实例将下载并运行 GitHub 中的脚本。Each VM instance in the scale set downloads and runs the script from GitHub. 在更复杂的示例中,可以安装多个应用程序组件和文件。In a more complex example, multiple application components and files could be installed. 如果规模集已纵向扩展,则新 VM 实例会自动应用相同的自定义脚本扩展定义,并安装所需的应用程序。If the scale set is scaled up, the new VM instances automatically apply the same Custom Script Extension definition and install the required application.

允许流量发往应用程序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.


#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

测试规模集Test your scale set

若要查看运行中的 Web 服务器,请使用 Get-AzPublicIpAddress 获取负载均衡器的公共 IP 地址。To see your web server in action, 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 中的基本网页

让 Web 浏览器保持打开状态,以便在下一步骤中可以看到更新的版本。Leave the web browser open so that you can see an updated version in the next step.

更新应用部署Update app deployment

在规模集的整个生命周期内,都可能需要部署应用程序的更新版本。Throughout the lifecycle of a scale set, you may need to deploy an updated version of your application. 使用自定义脚本扩展可以引用更新的部署脚本,然后将扩展重新应用到规模集。With the Custom Script Extension, you can reference an updated deploy script and then reapply the extension to your scale set. 在上一步骤中创建规模集时,-UpgradePolicyMode 已设置为 AutomaticWhen the scale set was created in a previous step, the -UpgradePolicyMode was set to Automatic. 此设置可让规模集中的 VM 实例自动更新应用程序并应用其最新版本。This setting allows the VM instances in the scale set to automatically update and apply the latest version of your application.

创建名为 customConfigv2 的新配置定义。Create a new config definition named customConfigv2. 此定义运行应用程序安装脚本的 v2 更新版本:This definition runs an updated v2 version of the application install script:

$customConfigv2 = @{
  "fileUris" = (,"https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/automate-iis-v2.ps1");
  "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File automate-iis-v2.ps1"
}

将自定义脚本扩展配置更新为规模集中的 VM 实例。Update the Custom Script Extension configuration to the VM instances in your scale set. customConfigv2 定义用于应用更新版本的应用程序。The customConfigv2 definition is used to apply the updated version of the application:

$vmss = Get-AzVmss `
          -ResourceGroupName "myResourceGroup" `
          -VMScaleSetName "myScaleSet"
 
$vmss.VirtualMachineProfile.ExtensionProfile[0].Extensions[0].Settings = $customConfigv2
 
Update-AzVmss `
  -ResourceGroupName "myResourceGroup" `
  -Name "myScaleSet" `
  -VirtualMachineScaleSet $vmss

规模集中的所有 VM 实例将使用示例网页的最新版本自动更新。All VM instances in the scale set are automatically updated with the latest version of the sample web page. 若要查看更新的版本,请在浏览器中刷新网站:To see the updated version, refresh the web site in your browser:

IIS 中更新的网页

清理资源Clean up resources

若要删除规模集和其他资源,请使用 Remove-AzResourceGroup 删除资源组及其所有资源。To remove your scale set and additional resources, delete the resource group and all its resources with Remove-AzResourceGroup. -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

本教程已介绍如何使用 Azure PowerShell 在规模集中自动安装和更新应用程序:In this tutorial, you learned how to automatically install and update applications on your scale set with Azure PowerShell:

  • 将应用程序自动安装到规模集Automatically install applications to your scale set
  • 使用 Azure 自定义脚本扩展Use the Azure Custom Script Extension
  • 更新规模集中运行的应用程序Update a running application on a scale set

请继续学习下一教程,了解如何自动缩放规模集。Advance to the next tutorial to learn how to automatically scale your scale set.