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

若要在规模集中的虚拟机 (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 account before you begin.


  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。If you prefer, install the Azure CLI to run CLI reference commands.
    • 如果使用的是本地安装,请通过 Azure CLI 使用 az login 命令登录。If you're using a local install, sign in with Azure CLI by using the az login command. 若要完成身份验证过程,请遵循终端中显示的步骤。To finish the authentication process, follow the steps displayed in your terminal. 有关其他登录选项,请参阅使用 Azure CLI 登录See Sign in with Azure CLI for additional sign-in options.
    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。When you're prompted, install Azure CLI extensions on first use. 有关扩展详细信息,请参阅使用 Azure CLI 的扩展For more information about extensions, see Use extensions with Azure CLI.
    • 运行 az version 以查找安装的版本和依赖库。Run az version to find the version and dependent libraries that are installed. 若要升级到最新版本,请运行 az upgradeTo upgrade to the latest version, run az upgrade.
  • 本文需要 Azure CLI 2.0.29 或更高版本。This article requires version 2.0.29 or later of the Azure CLI.

什么是 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 资源管理器模板集成,也可以与 Azure CLI、Azure PowerShell、Azure 门户或 REST API 配合使用。The Custom Script extension integrates with Azure Resource Manager templates, and 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.

若要通过 Azure CLI 使用自定义脚本扩展,请创建 JSON 文件,用于定义要包含的文件和要执行的命令。To use the Custom Script Extension with the Azure CLI, you create a JSON file that defines what files to obtain and commands to execute. 这些 JSON 定义可以在规模集部署之间重复使用,以应用一致的应用程序安装。These JSON definitions can be reused across scale set deployments to apply consistent application installs.

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

若要查看自定义脚本扩展的运行方式,请创建一个可以安装 NGINX Web 服务器并输出规模集 VM 实例主机名的规模集。To see the Custom Script Extension in action, let's create a scale set that installs the NGINX web server and outputs the hostname of the scale set VM instance. 以下自定义脚本扩展定义从 GitHub 下载示例脚本,安装所需的包,然后将 VM 实例主机名写入一个基本的 HTML 页面。The following 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.

在当前 shell 中,创建名为“customConfig.json”的文件并粘贴下面的配置。In your current shell, create a file named customConfig.json and paste the following configuration.

  "fileUris": ["https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/automate_nginx.sh"],
  "commandToExecute": "./automate_nginx.sh"


如果决定直接在下面的 --settings 参数中引用 JSON(而不是引用 customConfig.json 文件),则可能需要在 JSON 块内反转使用单引号 (') 和双引号 (") 。You may need to invert the use of the single (') and double quotes (") within the JSON block if you decide to reference the JSON directly (instead of referencing the customConfig.json file) in the --settings parameter below.

创建规模集Create a scale set

使用 az group create 创建资源组。Create a resource group with az group create. 以下示例在“chinanorth2”位置创建名为“myResourceGroup”的资源组:The following example creates a resource group named myResourceGroup in the chinanorth2 location:

az group create --name myResourceGroup --location chinanorth2

现在,使用 az vmss create 创建虚拟机规模集。Now create a virtual machine scale set with az vmss create. 以下示例创建名为“myScaleSet”的规模集,并生成 SSH 密钥(如果不存在):The following example creates a scale set named myScaleSet, and generates SSH keys if they do not exist:

az vmss create \
  --resource-group myResourceGroup \
  --name myScaleSet \
  --image UbuntuLTS \
  --upgrade-policy-mode automatic \
  --admin-username azureuser \

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

应用自定义脚本扩展Apply the Custom Script Extension

使用 az vmss extension set 将自定义脚本扩展配置应用到规模集中的 VM 实例。Apply the Custom Script Extension configuration to the VM instances in your scale set with az vmss extension set. 以下示例将 customConfig.json 配置应用于名为 myResourceGroup 的资源组中的 myScaleSet VM 实例 :The following example applies the customConfig.json configuration to the myScaleSet VM instances in the resource group named myResourceGroup:

az vmss extension set \
  --publisher Microsoft.Azure.Extensions \
  --version 2.0 \
  --name CustomScript \
  --resource-group myResourceGroup \
  --vmss-name myScaleSet \
  --settings @customConfig.json

规模集中的每个 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.

测试规模集Test your scale set

若要允许通信流到达 Web 服务器,请使用 az network lb rule create 命令创建负载均衡器规则。To allow traffic to reach the web server, create a load balancer rule with az network lb rule create. 以下示例创建名为“myLoadBalancerRuleWeb”的规则:The following example creates a rule named myLoadBalancerRuleWeb:

az network lb rule create \
  --resource-group myResourceGroup \
  --name myLoadBalancerRuleWeb \
  --lb-name myScaleSetLB \
  --backend-pool-name myScaleSetLBBEPool \
  --backend-port 80 \
  --frontend-ip-name loadBalancerFrontEnd \
  --frontend-port 80 \
  --protocol tcp

若要查看运行中的 Web 服务器,请使用 az network public-ip show 命令获取负载均衡器的公共 IP 地址。To see your web server in action, obtain the public IP address of your load balancer with az network public-ip show. 以下示例获取创建为规模集一部分的“myScaleSetLBPublicIP”的 IP 地址:The following example obtains the IP address for myScaleSetLBPublicIP created as part of the scale set:

az network public-ip show \
  --resource-group myResourceGroup \
  --name myScaleSetLBPublicIP \
  --query [ipAddress] \
  --output tsv

将负载均衡器的公共 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:

Nginx 中的基本网页

让 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. 在上一步骤中创建规模集时,--upgrade-policy-mode 已设置为 automaticWhen the scale set was created in a previous step, the --upgrade-policy-mode 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.

在当前 shell 中,创建名为“customConfigv2.json”的文件并粘贴下面的配置。In your current shell, create a file named customConfigv2.json and paste the following configuration. 此定义运行应用程序安装脚本的 v2 更新版本:This definition runs an updated v2 version of the application install script:

  "fileUris": ["https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/automate_nginx_v2.sh"],
  "commandToExecute": "./automate_nginx_v2.sh"

使用 az vmss extension set 再次将自定义脚本扩展配置应用到规模集中的 VM 实例。Apply the Custom Script Extension configuration to the VM instances in your scale set again with az vmss extension set. customConfigv2.json 用于应用更新版本的应用程序。The customConfigv2.json is used to apply the updated version of the application:

az vmss extension set \
    --publisher Microsoft.Azure.Extensions \
    --version 2.0 \
    --name CustomScript \
    --resource-group myResourceGroup \
    --vmss-name myScaleSet \
    --settings @customConfigv2.json

规模集中的所有 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:

Nginx 中更新的网页

清理资源Clean up resources

若要删除规模集和其他资源,请使用 az group delete 删除资源组及其所有资源。To remove your scale set and additional resources, delete the resource group and all its resources with az group delete. --no-wait 参数会使光标返回提示符处,无需等待操作完成。The --no-wait parameter returns control to the prompt without waiting for the operation to complete. --yes 参数将确认是否希望删除资源,而不会有额外提示。The --yes parameter confirms that you wish to delete the resources without an additional prompt to do so.

az group delete --name myResourceGroup --no-wait --yes

后续步骤Next steps

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

  • 将应用程序自动安装到规模集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.