使用 Azure CLI 将应用程序安装到虚拟机规模集中Install applications into a virtual machine scale set with the Azure CLI

此脚本创建运行 Ubuntu 的虚拟机规模集,并使用自定义脚本扩展安装一个基本 Web 应用程序。This script creates a virtual machine scale set running Ubuntu and uses the Custom Script Extension to install a basic web application. 运行脚本后,可以通过 Web 浏览器访问该 Web 应用。After running the script, you can access the web app through a web browser.

若要运行此示例,请安装最新版本的 Azure CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

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

示例脚本Sample script

#!/bin/bash

# Create a resource group
az group create --name myResourceGroup --location chinanorth

# Create a scale set
# Network resources such as an Azure load balancer are automatically created
az vmss create \
  --resource-group myResourceGroup \
  --name myScaleSet \
  --image UbuntuLTS \
  --upgrade-policy-mode automatic \
  --admin-username azureuser \
  --generate-ssh-keys

# Install the Azure Custom Script Extension to run an install script
az vmss extension set \
  --publisher Microsoft.Azure.Extensions \
  --version 2.0 \
  --name CustomScript \
  --resource-group myResourceGroup \
  --vmss-name myScaleSet \
  --settings '{"fileUris":["https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/automate_nginx.sh"],"commandToExecute":"./automate_nginx.sh"}'

# Create a load balancer rule to allow web traffic to reach VM instances
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

# Show the public IP address of the load balancer and view your web servers
az network public-ip show \
  --resource-group myResourceGroup \
  --name myScaleSetLBPublicIP \
  --query [ipAddress] \
  --output tsv

清理部署Clean up deployment

运行以下命令可删除资源组、规模集和所有相关资源。Run the following command to remove the resource group, scale set, and all related resources.

az group delete --name myResourceGroup

脚本说明Script explanation

此脚本使用以下命令创建资源组、虚拟机规模集和所有相关资源。This script uses the following commands to create a resource group, virtual machine scale set, and all related resources. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 注释Notes
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az vmss createaz vmss create 创建虚拟机规模集并将其连接到虚拟网络、子网和网络安全组。Creates the virtual machine scale set and connects it to the virtual network, subnet, and network security group. 负载均衡器也会被创建,以将流量分配到多个 VM 实例。A load balancer is also created to distribute traffic to multiple VM instances. 此命令还指定要使用的 VM 映像和管理凭据。This command also specifies the VM image to be used and administrative credentials.
az vmss extension setaz vmss extension set 安装 Azure 自定义脚本扩展以运行脚本,从而在每个 VM 实例上准备数据磁盘。Installs the Azure Custom Script Extension to run a script that prepares the data disks on each VM instance.
az network lb rule createaz network lb rule create 创建负载均衡器规则,以将 TCP 端口 80 上的流量分摊到规模集中的各个 VM 实例上。Creates a load balancer rule to distribute traffic on TCP port 80 to the VM instances in the scale set.
az network public-ip showaz network public-ip show 获取有关负载均衡器使用的分配公共 IP 地址的信息。Gets information on the public IP address assigned used by the load balancer.
az group deleteaz group delete 删除资源组,包括所有嵌套的资源。Deletes a resource group including all nested resources.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.