如何使用 Visual Studio 创建虚拟机规模集How to create a Virtual Machine Scale Set with Visual Studio

本文介绍如何使用 Visual Studio 资源组部署来部署 Azure 虚拟机规模集。This article shows you how to deploy an Azure Virtual Machine Scale Set using a Visual Studio Resource Group deployment.

Azure 虚拟机规模集是一种 Azure 计算资源,可通过自动缩放和负载均衡部署和管理一组类似的虚拟机。Azure Virtual Machine Scale Sets is an Azure Compute resource to deploy and manage a collection of similar virtual machines with autoscale and load balancing. 可使用 Azure 资源管理器模板预配和部署虚拟机规模集。You can provision and deploy Virtual Machine Scale Sets using Azure Resource Manager Templates. 可以使用 Azure CLI、PowerShell、REST 来部署 Azure 资源管理器模板,也可直接从 Visual Studio 部署。Azure Resource Manager templates can be deployed using Azure CLI, PowerShell, REST and also directly from Visual Studio. Visual Studio 提供了一组示例模板,这些模板可以作为 Azure 资源组部署项目的一部分进行部署。Visual Studio provides a set of example templates, which you can deploy as part of an Azure Resource Group deployment project.

Azure 资源组部署是一种通过单个部署操作将相关的一组 Azure 资源组合并进行发布的方式。Azure Resource Group deployments are a way to group and publish a set of related Azure resources in a single deployment operation. 有关详细信息,请参阅通过 Visual Studio 创建和部署 Azure 资源组For more information, see Creating and deploying Azure resource groups through Visual Studio.

必备条件Prerequisites

若要开始在 Visual Studio 中部署虚拟机规模集,需要具备以下先决条件:To get started deploying Virtual Machine Scale Sets in Visual Studio, you need the following prerequisites:

  • Visual Studio 2013 或更高版本Visual Studio 2013 or later
  • Azure SDK 2.7、2.8 或 2.9Azure SDK 2.7, 2.8 or 2.9

备注

本文将 Visual Studio 2019 与 Azure SDK 2.8 配合使用。This article uses Visual Studio 2019 with Azure SDK 2.8.

创建项目 Create a Project

  1. 打开 Visual Studio 并选择“创建新项目” 。Open Visual Studio and select Create a new project.

  2. 在“创建新项目” 中,选择用于 C# 的“Azure 资源组” ,然后选择“下一步” 。In Create a new project, choose Azure Resource Group for C# and then select Next.

  3. 在“配置新项目”中输入名称,然后选择“创建”。 In Configure your new project, enter a name and select Create.

    命名并创建项目

  4. 在模板列表中,选择“Windows 虚拟机规模集”或“Linux 虚拟机规模集”模板。 From the list of templates, choose either the Windows Virtual Machine Scale Set or Linux Virtual Machine Scale Set template. 选择“确定” 。Select OK.

    选择虚拟机模板

创建项目后,解决方案资源管理器会包含 PowerShell 部署脚本、Azure 资源管理器模板和虚拟机规模集的参数文件。After you create your project, Solution Explorer contains a PowerShell deployment script, an Azure Resource Manager template, and a parameter file for the Virtual Machine Scale Set.

自定义项目Customize your project

现在可以编辑模板以根据应用程序的需求自定义它。Now you can edit the template to customize it for your application's needs. 可以添加虚拟机扩展属性或编辑负载均衡规则。You could add virtual machine extension properties or edit load-balancing rules. 默认情况下,虚拟机规模集模板已配置为部署 AzureDiagnostics 扩展,如此即可轻松添加自动缩放规则。By default, the Virtual Machine Scale Set templates are configured to deploy the AzureDiagnostics extension, which makes it easy to add autoscale rules. 这些模板还部署了具有公共 IP 地址且配置有入站 NAT 规则的负载均衡器。The templates also deploy a load balancer with a public IP address, configured with inbound NAT rules.

通过负载均衡器,可以使用 SSH (Linux) 或 RDP (Windows) 连接到虚拟机实例。The load balancer lets you connect to the virtual machine instances with SSH (Linux) or RDP (Windows). 前端端口范围从 50000 开始。The front-end port range starts at 50000. 对于 Linux,如果通过 SSH 连接到端口 50000,则会路由到规模集中第一个虚拟机的端口 22。For Linux, if you SSH to port 50000, load balancing routes you to port 22 of the first virtual machine in the Scale Set. 连接到端口 50001 将路由到第二个虚拟机的端口 22,依此类推。Connecting to port 50001 is routed to port 22 of the second virtual machine, and so on.

使用 Visual Studio 编辑模板的一种好方法是使用“JSON 概要”。 A good way to edit your templates with Visual Studio is to use the JSON Outline. 根据组织参数、变量和资源。You can organize the parameters, variables, and resources. 了解架构后,Visual Studio 可以在部署前指出模板中的错误。With an understanding of the schema, Visual Studio can point out errors in your template before you deploy it.

JSON 资源管理器

部署项目Deploy the project

部署 Azure 资源管理器模板来创建虚拟机规模集资源:Deploy the Azure Resource Manager template to create the Virtual Machine Scale Set resource:

  1. 在“解决方案资源管理器”中,右键单击该项目并选择“部署” > “新建”。In Solution Explorer, right-click the project and choose Deploy > New.

    部署项目

  2. 在“部署到资源组”中,选择要使用的订阅并选择资源组。 In Deploy to Resource Group, choose which subscription to use and select a resource group. 可以根据需要创建资源组。You can create a resource group, if necessary.

  3. 接下来,选择“编辑参数” ,输入要传递给模板的参数。Next, select Edit Parameters to enter parameters that are passed to your template.

    输入订阅和资源组

  4. 为操作系统提供用户名和密码。Provide the username and password for the operating system. 这些值是创建部署所需的。These values are required to create the deployment. 如果未安装用于 Visual Studio 的 PowerShell 工具,请选择“保存密码” ,以避免隐藏的 PowerShell 命令提示符,或使用 Key Vault 支持If you don't have PowerShell Tools for Visual Studio installed, select Save passwords to avoid a hidden PowerShell command prompt, or use Key Vault support. 选择“保存”以继续操作。 Select Save to continue.

    编辑部署参数

  5. 在“部署到资源组”中,选择“部署”。 In Deploy to Resource Group, select Deploy. 该操作运行 Deploy-AzureResourceGroup.ps1 脚本。The action runs the Deploy-AzureResourceGroup.ps1 script. “输出” 窗口显示部署进度。The Output window shows the deployment progress.

    输出显示结果

探索虚拟机规模集 Explore your Virtual Machine Scale Set

选择“查看” > “Cloud Explorer”,查看新的虚拟机规模集。Select View > Cloud Explorer to view the new Virtual Machine Scale Set. 根据需要使用“全部刷新” 。Use Refresh All, if necessary.

云资源管理器

Cloud Explorer 让你可以在开发应用程序时管理 Visual Studio 中的 Azure 资源。Cloud Explorer lets you manage Azure resources in Visual Studio while developing applications. 还可以在 Azure 门户中查看虚拟机规模集。You can also view your Virtual Machine Scale Set in the Azure portal.

门户是使用 Web 浏览器时管理 Azure 基础结构的最佳方法。The portal provides the best way to manage your Azure infrastructure with a web browser. 可以使用 Azure 资源浏览器轻松地浏览和调试 Azure 资源。Azure Resource Explorer provides an easy way to explore and debug Azure resources. Azure 资源浏览器提供实例视图,并为你要查看的资源显示 PowerShell 命令。Azure Resource Explorer offers the instance view and also shows PowerShell commands for the resources you're looking at.

后续步骤Next steps

通过 Visual Studio 成功部署虚拟机规模集后,便可进一步自定义项目以满足应用程序需求。Once you've successfully deployed Virtual Machine Scale Sets through Visual Studio, you can further customize your project to suit your application requirements. 例如,通过添加 Insights 资源来配置自动缩放。For example, configure autoscale by adding an Insights resource. 可以将基础结构(例如独立虚拟机)添加到模板,或者使用自定义脚本扩展来部署应用程序。You could add infrastructure to your template, such as standalone virtual machines, or deploy applications using the custom script extension. 可以在 Azure 快速入门模板 GitHub 存储库中找到很好的示例模板。Good example templates can be found in the Azure Quickstart Templates GitHub repository. 搜索 vmssSearch for vmss.