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

利用虚拟机规模集,可以部署和管理一组自动缩放的虚拟机。A virtual machine scale set allows you to deploy and manage a set of 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 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 CLI 创建虚拟机规模集并部署一个示例应用程序。In this quickstart, you create a virtual machine scale set and deploy a sample application with the Azure CLI.

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

如果选择在本地安装和使用 CLI,本教程要求运行 Azure CLI 2.0.29 或更高版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.0.29 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

创建规模集Create a scale set

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

az group create --name myResourceGroup --location chinanorth

现在,使用 az vmss create 创建虚拟机规模集。Now create a virtual machine scale set with az vmss create. 以下示例创建名为 myScaleSet 的规模集,该规模集设置为在应用更改时自动更新;如果 ~/.ssh/id_rsa 中没有 SSH 密钥,此示例还会生成 SSH 密钥。The following example creates a scale set named myScaleSet that is set to automatically update as changes are applied, and generates SSH keys if they do not exist in ~/.ssh/id_rsa. 登录到 VM 实例时需要使用这些 SSH 密钥。These SSH keys are used if you need to log in to the VM instances. 若要使用一组现有的 SSH 密钥,请改用 --ssh-key-value 参数并指定密钥的位置。To use an existing set of SSH keys, instead use the --ssh-key-value parameter and specify the location of your keys.

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

创建和配置所有的规模集资源和 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 实例上安装应用程序。The Azure Custom Script Extension is used to download and run a script that installs an application 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.

使用自定义脚本扩展安装基本的 NGINX Web 服务器。Use the Custom Script Extension to install a basic NGINX web server. 使用 az vmss extension set 应用可安装 NGINX 的自定义脚本扩展,如下所示:Apply the Custom Script Extension that installs NGINX with az vmss extension set as follows:

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"}'

允许流量发往应用程序Allow traffic to application

创建规模集时,已自动部署一个 Azure 负载均衡器。When the scale set was created, an Azure load balancer was automatically deployed. 该负载均衡器会将流量分配到规模集中的 VM 实例。The load balancer distributes traffic to the VM instances in the scale set. 若要允许流量抵达示例 Web 应用程序,请使用 az network lb rule create 创建一个负载均衡器规则。To allow traffic to reach the sample web application, 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

测试规模集Test your scale set

若要查看正在运行的规模集,请在 Web 浏览器中访问示例 Web 应用程序。To see your scale set in action, access the sample web application in a web browser. 使用 az network public-ip show 获取负载均衡器的公共 IP 地址。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 中的默认网页

清理资源Clean up resources

如果不再需要资源组、规模集和所有相关的资源,可以使用 az group delete 命令将其删除,如下所示。When no longer needed, you can use az group delete to remove the resource group, scale set, and all related resources as follows. --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 --yes --no-wait

后续步骤Next steps

在本快速入门中,我们已创建一个基本的规模集,并使用自定义脚本扩展在 VM 实例上安装了一个基本的 NGINX Web 服务器。In this quickstart, you created a basic scale set and used the Custom Script Extension to install a basic NGINX 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.