快速入门:使用 ARM 模板创建公共负载均衡器,以对 VM 进行负载均衡

负载均衡将传入请求分布到多个虚拟机 (VM),从而提供更高级别的可用性和可伸缩性。

本快速入门演示如何部署标准负载均衡器,以对虚拟机进行负载均衡。

与其他部署方法相比,使用 ARM 模板需要的步骤更少。

资源管理器模板是定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。 模板使用声明性语法。 在声明性语法中,你可以在不编写创建部署的编程命令序列的情况下,描述预期部署。

如果你的环境满足先决条件,并且你熟悉如何使用 ARM 模板,请选择“部署到 Azure”按钮。 Azure 门户中会打开模板。

部署到 Azure

先决条件

如果没有 Azure 订阅,请在开始前创建试用版订阅

查看模板

本快速入门中使用的模板来自 Azure 快速启动模板

负载均衡器和公共 IP SKU 必须匹配。 创建标准负载均衡器时,还必须创建一个作为该标准负载均衡器的前端配置的新标准公共 IP 地址。 若要创建基本负载均衡器,请使用此模板。 Azure 建议将标准 SKU 用于生产工作负荷。

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "projectName": {
            "type": "string",
            "metadata": {
                "description": "Specifies a project name that is used for generating resource names."
            }
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
                "description": "Specifies the location for all of the resources created by this template."
            }
        },
        "adminUsername": {
            "type": "string",
            "metadata": {
                "description": "Specifies the virtual machine administrator username."
            }
        },
        "adminPassword": {
            "type": "securestring",
            "metadata": {
                "description": "Specifies the virtual machine administrator password."
            }
        },
        "vmSize": {
            "type": "string",
            "defaultValue": "Standard_D2s_v3",
            "metadata": {
                "description": "Size of the virtual machine"
            }
        }
    },
    "variables": {
        "lbName": "[concat(parameters('projectName'),'-lb')]",
        "lbSkuName": "Standard",
        "lbPublicIpAddressName": "[concat(parameters('projectName'),'-lbPublicIP')]",
        "lbPublicIPAddressNameOutbound": "[concat(parameters('projectName'),'-lbPublicIPOutbound')]",
        "lbFrontEndName": "LoadBalancerFrontEnd",
        "lbFrontEndNameOutbound": "LoadBalancerFrontEndOutbound",
        "lbBackendPoolName": "LoadBalancerBackEndPool",
        "lbBackendPoolNameOutbound": "LoadBalancerBackEndPoolOutbound",
        "lbProbeName": "loadBalancerHealthProbe",
        "nsgName": "[concat(parameters('projectName'),'-nsg')]",
        "vNetName": "[concat(parameters('projectName'),'-vnet')]",
        "vNetAddressPrefix": "10.0.0.0/16",
        "vNetSubnetName": "BackendSubnet",
        "vNetSubnetAddressPrefix": "10.0.0.0/24",
        "bastionName": "[concat(parameters('projectName'),'-bastion')]",
        "bastionSubnetName": "AzureBastionSubnet",
        "vNetBastionSubnetAddressPrefix": "10.0.1.0/24",
        "bastionPublicIPAddressName": "[concat(parameters('projectName'),'-bastionPublicIP')]",
        "vmStorageAccountType": "Premium_LRS"
    },
    "resources": [
        {
            "type": "Microsoft.Network/networkInterfaces",
            "apiVersion": "2020-06-01",
            "name": "[concat(parameters('projectName'),'-vm', copyIndex(1), '-networkInterface')]",
            "location": "[parameters('location')]",
            "copy": {
                "name": "networkInterfaceCopy",
                "count": 3
            },
            "dependsOn": [
                "[resourceId('Microsoft.Network/virtualNetworks', variables('vNetName'))]",
                "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vNetName'), variables('vNetSubnetName'))]",
                "[resourceId('Microsoft.Network/loadBalancers', variables('lbName'))]",
                "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsgName'))]"
            ],
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1",
                        "properties": {
                            "privateIPAllocationMethod": "Dynamic",
                            "subnet": {
                                "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vNetName'), variables('vNetSubnetName'))]"
                            },
                            "loadBalancerBackendAddressPools": [
                                {
                                    "id": "[resourceId('Microsoft.Network/loadBalancers/backendAddressPools', variables('lbName'), variables('lbBackendPoolName'))]"
                                },
                                {
                                    "id": "[resourceId('Microsoft.Network/loadBalancers/backendAddressPools', variables('lbName'), variables('lbBackendPoolNameOutbound'))]"
                                }
                            ]
                        }
                    }
                ],
                "networkSecurityGroup": {
                    "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsgName'))]"
                }
            }
        },
        {
            "type": "Microsoft.Compute/virtualMachines/extensions",
            "apiVersion": "2020-06-01",
            "name": "[concat(parameters('projectName'),'-vm', copyIndex(1), '/', 'InstallWebServer')]",
            "location": "[parameters('location')]",
            "copy": {
                "name": "extensionCopy",
                "count": 3
            },
            "dependsOn": [
                "vmCopy"
            ],
            "properties": {
                "publisher": "Microsoft.Compute",
                "type": "CustomScriptExtension",
                "typeHandlerVersion": "1.7",
                "autoUpgradeMinorVersion": true,
                "settings": {
                    "commandToExecute": "powershell.exe Install-WindowsFeature -name Web-Server -IncludeManagementTools && powershell.exe remove-item 'C:\\inetpub\\wwwroot\\iisstart.htm' && powershell.exe Add-Content -Path 'C:\\inetpub\\wwwroot\\iisstart.htm' -Value $('Hello World from ' + $env:computername)"
                }
            }
        },
        {
            "type": "Microsoft.Compute/virtualMachines",
            "apiVersion": "2020-06-01",
            "name": "[concat(parameters('projectName'),'-vm', copyIndex(1))]",
            "location": "[parameters('location')]",
            "zones": [
                "[copyIndex(1)]"
            ],
            "copy": {
                "name": "vmCopy",
                "count": 3
            },
            "dependsOn": [
                "networkInterfaceCopy"
            ],
            "properties": {
                "hardwareProfile": {
                    "vmSize": "[parameters('vmSize')]"
                },
                "storageProfile": {
                    "imageReference": {
                        "publisher": "MicrosoftWindowsServer",
                        "offer": "WindowsServer",
                        "sku": "2019-Datacenter",
                        "version": "latest"
                    },
                    "osDisk": {
                        "createOption": "fromImage",
                        "managedDisk": {
                            "storageAccountType": "[variables('vmStorageAccountType')]"
                        }
                    }
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', concat(parameters('projectName'),'-vm', copyIndex(1), '-networkInterface'))]"
                        }
                    ]
                },
                "osProfile": {
                    "computerName": "[concat(parameters('projectName'),'-vm', copyIndex(1))]",
                    "adminUsername": "[parameters('adminUsername')]",
                    "adminPassword": "[parameters('adminPassword')]",
                    "windowsConfiguration": {
                        "enableAutomaticUpdates": true,
                        "provisionVmAgent": true
                    }
                }
            }
        },
        {
            "type": "Microsoft.Network/virtualNetworks/subnets",
            "apiVersion": "2020-06-01",
            "location": "[parameters('location')]",
            "name": "[concat(variables('vNetName'), '/' ,variables('bastionSubnetName'))]",
            "dependsOn": [
                "[resourceId('Microsoft.Network/virtualNetworks', variables('vNetName'))]"
            ],
            "properties": {
                "addressPrefix": "[variables('vNetBastionSubnetAddressPrefix')]"
            }
        },
        {
            "type": "Microsoft.Network/virtualNetworks/subnets",
            "apiVersion": "2020-06-01",
            "location": "[parameters('location')]",
            "name": "[concat(variables('vNetName'), '/' ,variables('vNetSubnetName'))]",
            "dependsOn": [
                "[resourceId('Microsoft.Network/virtualNetworks', variables('vNetName'))]"
            ],
            "properties": {
                "addressPrefix": "[variables('vNetSubnetAddressPrefix')]"
            }
        },
        {
            "type": "Microsoft.Network/bastionHosts",
            "apiVersion": "2020-05-01",
            "name": "[variables('bastionName')]",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[resourceId('Microsoft.Network/publicIPAddresses', variables('bastionPublicIPAddressName'))]",
                "[resourceId('Microsoft.Network/virtualNetworks', variables('vNetName'))]",
                "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vNetName'), variables('bastionSubnetName'))]"
            ],
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "IpConf",
                        "properties": {
                            "privateIPAllocationMethod": "Dynamic",
                            "publicIPAddress": {
                                "id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('bastionPublicIPAddressName'))]"
                            },
                            "subnet": {
                                "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vNetName'), variables('bastionSubnetName'))]"
                            }
                        }
                    }
                ]
            }
        },
        {
            "type": "Microsoft.Network/publicIPAddresses",
            "apiVersion": "2020-06-01",
            "name": "[variables('bastionPublicIPAddressName')]",
            "location": "[parameters('location')]",
            "sku": {
                "name": "[variables('lbSkuName')]"
            },
            "properties": {
                "publicIPAddressVersion": "IPv4",
                "publicIPAllocationMethod": "Static"
            }
        },
        {
            "type": "Microsoft.Network/loadBalancers",
            "apiVersion": "2020-06-01",
            "name": "[variables('lbName')]",
            "location": "[parameters('location')]",
            "sku": {
                "name": "[variables('lbSkuName')]"
            },
            "dependsOn": [
                "[resourceId('Microsoft.Network/publicIPAddresses', variables('lbPublicIpAddressName'))]",
                "[resourceId('Microsoft.Network/publicIPAddresses', variables('lbPublicIpAddressNameOutbound'))]"
            ],
            "properties": {
                "frontendIPConfigurations": [
                    {
                        "name": "[variables('lbFrontEndName')]",
                        "properties": {
                            "publicIPAddress": {
                                "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('lbPublicIpAddressName'))]"
                            }
                        }
                    },
                    {
                        "name": "[variables('lbFrontEndNameOutbound')]",
                        "properties": {
                            "publicIPAddress": {
                                "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('lbPublicIpAddressNameOutbound'))]"
                            }
                        }
                    }
                ],
                "backendAddressPools": [
                    {
                        "name": "[variables('lbBackendPoolName')]"
                    },
                    {
                        "name": "[variables('lbBackendPoolNameOutbound')]"
                    }
                ],
                "loadBalancingRules": [
                    {
                        "name": "myHTTPRule",
                        "properties": {
                            "frontendIPConfiguration": {
                                "id": "[resourceId('Microsoft.Network/loadBalancers/frontendIPConfigurations', variables('lbName'), variables('lbFrontEndName'))]"
                            },
                            "backendAddressPool": {
                                "id": "[resourceId('Microsoft.Network/loadBalancers/backendAddressPools', variables('lbName'), variables('lbBackendPoolName'))]"
                            },
                            "frontendPort": 80,
                            "backendPort": 80,
                            "enableFloatingIP": false,
                            "idleTimeoutInMinutes": 15,
                            "protocol": "Tcp",
                            "enableTcpReset": true,
                            "loadDistribution": "Default",
                            "disableOutboundSnat": true,
                            "probe": {
                                "id": "[resourceId('Microsoft.Network/loadBalancers/probes', variables('lbName'), variables('lbProbeName'))]"
                            }
                        }
                    }
                ],
                "probes": [
                    {
                        "name": "[variables('lbProbeName')]",
                        "properties": {
                            "protocol": "tcp",
                            "port": 80,
                            "intervalInSeconds": 5,
                            "numberOfProbes": 2
                        }
                    }
                ],
                "outboundRules": [
                    {
                        "name": "myOutboundRule",
                        "properties": {
                            "allocatedOutboundPorts": 10000,
                            "protocol": "All",
                            "enableTcpReset": false,
                            "idleTimeoutInMinutes": 15,
                            "backendAddressPool": {
                                "id": "[resourceId('Microsoft.Network/loadBalancers/backendAddressPools', variables('lbName'), variables('lbBackendPoolNameOutbound'))]"
                            },
                            "frontendIPConfigurations": [
                                {
                                    "id": "[resourceId('Microsoft.Network/loadBalancers/frontendIPConfigurations', variables('lbName'), variables('lbFrontEndNameOutbound'))]"
                                }
                            ]
                        }
                    }
                ]
            }
        },
        {
            "type": "Microsoft.Network/publicIPAddresses",
            "apiVersion": "2020-06-01",
            "name": "[variables('lbPublicIPAddressName')]",
            "location": "[parameters('location')]",
            "sku": {
                "name": "[variables('lbSkuName')]"
            },
            "properties": {
                "publicIPAddressVersion": "IPv4",
                "publicIPAllocationMethod": "Static"
            }
        },
        {
            "type": "Microsoft.Network/publicIPAddresses",
            "apiVersion": "2020-06-01",
            "name": "[variables('lbPublicIPAddressNameOutbound')]",
            "location": "[parameters('location')]",
            "sku": {
                "name": "[variables('lbSkuName')]"
            },
            "properties": {
                "publicIPAddressVersion": "IPv4",
                "publicIPAllocationMethod": "Static"
            }
        },
        {
            "type": "Microsoft.Network/networkSecurityGroups",
            "apiVersion": "2020-06-01",
            "name": "[variables('nsgName')]",
            "location": "[parameters('location')]",
            "properties": {
                "securityRules": [
                    {
                        "name": "AllowHTTPInbound",
                        "properties": {
                            "protocol": "*",
                            "sourcePortRange": "*",
                            "destinationPortRange": "80",
                            "sourceAddressPrefix": "Internet",
                            "destinationAddressPrefix": "*",
                            "access": "Allow",
                            "priority": 100,
                            "direction": "Inbound"
                        }
                    }
                ]
            }
        },
        {
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2020-06-01",
            "name": "[variables('vNetName')]",
            "location": "[parameters('location')]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "[variables('vNetAddressPrefix')]"
                    ]
                }
            }
        }
    ]
}

该模板中已定义了多个 Azure 资源:

重要

小时定价从部署 Bastion 的时刻开始计算,而无论出站数据使用情况如何。 有关详细信息,请参阅定价SKU。 如果要将 Bastion 部署为教程或测试的一部分,建议在使用完此资源后将其删除。

若要查找与 Azure 负载均衡器相关的更多模板,请参阅 Azure 快速入门模板

部署模板

  1. 使用 PowerShell。

    $projectName = Read-Host -Prompt "Enter a project name with 12 or less letters or numbers that is used to generate Azure resource names"
    $location = Read-Host -Prompt "Enter the location (i.e. chinaeast)"
    $adminUserName = Read-Host -Prompt "Enter the virtual machine administrator account name"
    $adminPassword = Read-Host -Prompt "Enter the virtual machine administrator password" -AsSecureString
    
    $resourceGroupName = "${projectName}rg"
    $templateUri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.network/load-balancer-standard-create/azuredeploy.json"
    
    New-AzResourceGroup -Name $resourceGroupName -Location $location
    New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri $templateUri -projectName $projectName -location $location -adminUsername $adminUsername -adminPassword $adminPassword
    
    Write-Host "Press [ENTER] to continue."
    

    等到控制台中显示提示。

  2. 从上一个代码块中选择“复制”,以复制 PowerShell 脚本。

  3. 右键单击 shell 控制台窗格,然后选择“粘贴”

  4. 输入相应的值。

    模板部署会创建三个可用性区域。 可用性区域仅在某些地区受到支持。 请使用受支持的地区之一。 如果不确定,请输入“chinaeast”。

    资源组名称是追加了 rg 的项目名称。 在下一部分中将用到资源组名称。

部署模板大约需要 10 分钟的时间。 完成后,输出类似于:

Azure 标准负载均衡器资源管理器模板 PowerShell 部署输出

使用 Azure PowerShell 部署模板。 还可以使用 Azure 门户、Azure CLI 和 REST API。 若要了解其他部署方法,请参阅部署模板

查看已部署的资源

  1. 登录 Azure 门户

  2. 从左侧窗格中选择“资源组”。

  3. 选择你在上一部分中创建的资源组。 默认资源组名称是追加了 -rg 的项目名称。

  4. 选择负载均衡器。 其默认名称是追加了 -lb 的项目名称。

  5. 仅复制公共 IP 地址的 IP 地址部分,然后将其粘贴到浏览器的地址栏中。

    Azure 标准负载均衡器资源管理器模板公共 IP

    浏览器将显示 Internet Information Services (IIS) Web 服务器的默认页。

    IIS Web 服务器

若要查看负载均衡器如何在所有 3 个 VM 之间分配流量,可从客户端计算机强制刷新 Web 浏览器。

清理资源

不再需要它们时,请删除:

  • 资源组
  • 负载均衡器
  • 相关资源

请访问 Azure 门户,选择包含负载均衡器的资源组,然后选择“删除资源组”。

后续步骤

在本快速入门中,请执行以下操作:

  • 为负载均衡器和虚拟机创建了虚拟网络。
  • 创建了用于管理的 Azure Bastion 主机。
  • 已创建标准负载均衡器并已将 VM 附加到它。
  • 配置了负载均衡器流量规则和运行状况探测。
  • 测试了负载均衡器。

若要了解详细信息,请继续学习与 Azure 负载均衡器相关的教程。