适用于:✔️ Linux 虚拟机 ✔️ Windows 虚拟机 ✔️ 均匀规模集
备注
本文介绍了统一虚拟机规模集。 建议对新的工作负载使用灵活虚拟机规模集。 在 灵活虚拟机规模集概述中详细了解此新的业务流程模式。
利用虚拟机规模集,可以部署和管理一组自动缩放的虚拟机。 可以手动缩放规模集中的 VM 数,也可以定义规则,以便根据资源使用情况(如 CPU 使用率、内存需求或网络流量)进行自动缩放。 然后,Azure 负载均衡器会将流量分配到规模集中的 VM 实例。 在本快速入门中,我们将使用 Azure PowerShell 创建虚拟机规模集并部署一个示例应用程序。
如果没有 Azure 订阅,请在开始之前创建 一个免费帐户 。
Azure Cloud Shell
Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。 可以将 Bash 或 PowerShell 与 Cloud Shell 配合使用来使用 Azure 服务。 可以使用 Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。
若要启动 Azure Cloud Shell,请执行以下操作:
选项 | 示例/链接 |
---|---|
在代码或命令块右上角选择“Try It”。 选择“试用”不会自动将代码或命令复制到 Cloud Shell。 |
![]() |
转到 https://shell.azure.com 或选择“启动 Cloud Shell”按钮可在浏览器中打开 Cloud Shell。 |
![]() |
选择 Azure 门户右上角菜单栏上的 Cloud Shell 按钮。 |
![]() |
若要使用 Azure Cloud Shell,请执行以下操作:
启动 Cloud Shell。
选择代码块(或命令块)上的“复制”按钮以复制代码或命令。
在 Windows 和 Linux 上选择 CtrlShift+V,或在 macOS 上选择 CmdShift+V 将代码或命令粘贴到 Cloud Shell 会话中。
选择“Enter”运行代码或命令。
创建规模集
在创建规模集之前,请使用 New-AzResourceGroup 创建资源组。 以下示例在 eastus 位置创建名为 myResourceGroup 的资源组:
New-AzResourceGroup -ResourceGroupName "myResourceGroup" -Location "EastUS"
现在,使用 New-AzVmss 创建虚拟机规模集。 以下示例创建一个名为 myScaleSet 的规模集,该规模集使用 Windows Server 2016 Datacenter 平台映像。 虚拟网络、公共 IP 地址和负载均衡器的 Azure 网络资源均会自动创建。 出现提示时,可以针对规模集中的 VM 实例设置自己的管理凭据:
重要
从 2023 年 11 月开始,使用 PowerShell 和 Azure CLI 创建的 VM 规模集将默认为灵活业务流程模式(如果未指定业务流程模式)。 有关此更改以及您应采取的措施的详细信息,请转到 VMSS PowerShell/CLI 客户的重大更改 - Microsoft 社区中心
New-AzVmss `
-ResourceGroupName "myResourceGroup" `
-Location "EastUS" `
-VMScaleSetName "myScaleSet" `
-VirtualNetworkName "myVnet" `
-SubnetName "mySubnet" `
-PublicIpAddressName "myPublicIPAddress" `
-LoadBalancerName "myLoadBalancer" `
-OrchestrationMode 'Uniform' `
-UpgradePolicyMode "Automatic"
创建和配置所有的规模集资源和 VM 需要几分钟时间。
部署示例应用程序
若要测试规模集,请安装一个基本的 Web 应用程序。 使用 Azure 自定义脚本扩展下载并运行一个脚本,以便在 VM 实例上安装 IIS。 此扩展适用于部署后配置、软件安装或其他任何配置/管理任务。 有关详细信息,请参阅 自定义脚本扩展概述。
使用自定义脚本扩展安装基本的 IIS Web 服务器。 应用可安装 IIS 的自定义脚本扩展,如下所示:
# Define the script for your Custom Script Extension to run
$publicSettings = @{
"fileUris" = (,"https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/automate-iis.ps1");
"commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File automate-iis.ps1"
}
# Get information about the scale set
$vmss = Get-AzVmss `
-ResourceGroupName "myResourceGroup" `
-VMScaleSetName "myScaleSet"
# Use Custom Script Extension to install IIS and configure basic website
Add-AzVmssExtension -VirtualMachineScaleSet $vmss `
-Name "customScript" `
-Publisher "Microsoft.Compute" `
-Type "CustomScriptExtension" `
-TypeHandlerVersion 1.8 `
-Setting $publicSettings
# Update the scale set and apply the Custom Script Extension to the VM instances
Update-AzVmss `
-ResourceGroupName "myResourceGroup" `
-Name "myScaleSet" `
-VirtualMachineScaleSet $vmss
允许流量发往应用程序
若要允许访问基本 Web 应用程序,请使用 New-AzNetworkSecurityRuleConfig 和 New-AzNetworkSecurityGroup 创建网络安全组。 有关详细信息,请参阅 Azure 虚拟机规模集的网络配置。
# Get information about the scale set
$vmss = Get-AzVmss `
-ResourceGroupName "myResourceGroup" `
-VMScaleSetName "myScaleSet"
#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 EastUS `
-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
# Update the scale set and apply the Custom Script Extension to the VM instances
Update-AzVmss `
-ResourceGroupName "myResourceGroup" `
-Name "myScaleSet" `
-VirtualMachineScaleSet $vmss
测试规模集
若要观察虚拟机规模集的运行动作,请在 Web 浏览器中访问示例 Web 应用程序。 使用 Get-AzPublicIpAddress 获取负载均衡器的公共 IP 地址。 以下示例显示 myResourceGroup 资源组中创建的 IP 地址:
Get-AzPublicIpAddress -ResourceGroupName "myResourceGroup" | Select IpAddress
将负载均衡器的公共 IP 地址输入到 Web 浏览器中。 负载均衡器将流量分发到某个 VM 实例,如以下示例所示:
清理资源
不再需要时,可以使用 Remove-AzResourceGroup 删除资源组、规模集和所有相关资源,如下所示。
-Force
参数将确认是否希望删除资源,不会显示询问是否删除的额外提示。
-AsJob
参数会使控制返回到提示符处,而无需等待操作完成。
Remove-AzResourceGroup -Name "myResourceGroup" -Force -AsJob
后续步骤
在本快速入门中,我们已创建一个基本的规模集,并使用自定义脚本扩展在 VM 实例上安装了一个基本的 IIS Web 服务器。 若要了解详细信息,请继续学习有关如何创建和管理 Azure 虚拟机规模集的教程。