快速入門:使用 ARM 範本建立內部負載平衡器以平衡 VM 的負載Quickstart: Create an internal load balancer to load balance VMs by using an ARM template

本快速入門說明如何使用 Azure Resource Manager 範本 (ARM 範本) 建立內部 Azure 負載平衡器。This quickstart describes how to use an Azure Resource Manager template (ARM template) to create an internal Azure load balancer.

ARM 範本是一個 JavaScript 物件標記法 (JSON) 檔案,會定義專案的基礎結構和設定。An ARM template is a JavaScript Object Notation (JSON) file that defines the infrastructure and configuration for your project. 範本會使用宣告式語法。The template uses declarative syntax. 在宣告式語法中,您可以描述預期的部署,而不需要撰寫程式設計命令順序來建立部署。In declarative syntax, you describe your intended deployment without writing the sequence of programming commands to create the deployment.

如果您的環境符合必要條件,而且您很熟悉 ARM 範本,請選取 [部署至 Azure] 按鈕。If your environment meets the prerequisites and you're familiar with using ARM templates, select the Deploy to Azure button. 範本會在 Azure 入口網站中開啟。The template will open in the Azure portal.

部署至 AzureDeploy to Azure

必要條件Prerequisites

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶If you don't have an Azure subscription, create a free account before you begin.

檢閱範本Review the template

本快速入門中使用的範本來自 Azure 快速入門範本The template used in this quickstart is from the Azure Quickstart Templates.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "adminUsername": {
      "type": "string",
      "metadata": {
        "description": "Admin username"
      }
    },
    "adminPassword": {
      "type": "securestring",
      "metadata": {
        "description": "Admin password"
      }
    },
    "vmNamePrefix": {
      "type": "string",
      "defaultValue": "BackendVM",
      "metadata": {
        "description": "Prefix to use for VM names"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    },
    "vmSize": {
      "type": "string",
      "defaultValue": "Standard_DS1_V2",
      "metadata": {
        "description": "Size of the virtual machines"
      }
    }
  },
  "variables": {
    "availabilitySetName": "AvSet",
    "storageAccountType": "Standard_LRS",
    "storageAccountName": "[uniqueString(resourceGroup().id)]",
    "virtualNetworkName": "vNet",
    "subnetName": "backendSubnet",
    "loadBalancerName": "ilb",
    "networkInterfaceName": "nic",
    "subnetRef": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]",
    "numberOfInstances": 2
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-06-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[variables('storageAccountType')]"
      },
      "kind": "StorageV2"
    },
    {
      "type": "Microsoft.Compute/availabilitySets",
      "apiVersion": "2020-06-01",
      "name": "[variables('availabilitySetName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Aligned"
      },
      "properties": {
        "PlatformUpdateDomainCount": 2,
        "PlatformFaultDomainCount": 2
      }
    },
    {
      "type": "Microsoft.Network/virtualNetworks",
      "apiVersion": "2020-06-01",
      "name": "[variables('virtualNetworkName')]",
      "location": "[parameters('location')]",
      "properties": {
        "addressSpace": {
          "addressPrefixes": [
            "10.0.0.0/16"
          ]
        },
        "subnets": [
          {
            "name": "[variables('subnetName')]",
            "properties": {
              "addressPrefix": "10.0.2.0/24"
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Network/networkInterfaces",
      "apiVersion": "2020-06-01",
      "name": "[concat(variables('networkInterfaceName'), copyindex())]",
      "location": "[parameters('location')]",
      "copy": {
        "name": "nicLoop",
        "count": "[variables('numberOfInstances')]"
      },
      "dependsOn": [
        "[variables('virtualNetworkName')]",
        "[variables('loadBalancerName')]"
      ],
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "subnet": {
                "id": "[variables('subnetRef')]"
              },
              "loadBalancerBackendAddressPools": [
                {
                  "id": "[resourceId('Microsoft.Network/loadBalancers/backendAddressPools', variables('loadBalancerName'), 'BackendPool1')]"
                }
              ]
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Network/loadBalancers",
      "apiVersion": "2020-06-01",
      "name": "[variables('loadBalancerName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard"
      },
      "dependsOn": [
        "[variables('virtualNetworkName')]"
      ],
      "properties": {
        "frontendIPConfigurations": [
          {
            "properties": {
              "subnet": {
                "id": "[variables('subnetRef')]"
              },
              "privateIPAddress": "10.0.2.6",
              "privateIPAllocationMethod": "Static"
            },
            "name": "LoadBalancerFrontend"
          }
        ],
        "backendAddressPools": [
          {
            "name": "BackendPool1"
          }
        ],
        "loadBalancingRules": [
          {
            "properties": {
              "frontendIPConfiguration": {
                "id": "[resourceId('Microsoft.Network/loadBalancers/frontendIpConfigurations', variables('loadBalancerName'), 'LoadBalancerFrontend')]"
              },
              "backendAddressPool": {
                "id": "[resourceId('Microsoft.Network/loadBalancers/backendAddressPools', variables('loadBalancerName'), 'BackendPool1')]"
              },
              "probe": {
                "id": "[resourceId('Microsoft.Network/loadBalancers/probes', variables('loadBalancerName'), 'lbprobe')]"
              },
              "protocol": "Tcp",
              "frontendPort": 80,
              "backendPort": 80,
              "idleTimeoutInMinutes": 15
            },
            "Name": "lbrule"
          }
        ],
        "probes": [
          {
            "properties": {
              "protocol": "Tcp",
              "port": 80,
              "intervalInSeconds": 15,
              "numberOfProbes": 2
            },
            "name": "lbprobe"
          }
        ]
      }
    },
    {
      "type": "Microsoft.Compute/virtualMachines",
      "apiVersion": "2020-06-01",
      "name": "[concat(parameters('vmNamePrefix'), copyindex())]",
      "location": "[parameters('location')]",
      "copy": {
        "name": "virtualMachineLoop",
        "count": "[variables('numberOfInstances')]"
      },
      "dependsOn": [
        "[variables('storageAccountName')]",
        "nicLoop",
        "[variables('availabilitySetName')]"
      ],
      "properties": {
        "availabilitySet": {
          "id": "[resourceId('Microsoft.Compute/availabilitySets',variables('availabilitySetName'))]"
        },
        "hardwareProfile": {
          "vmSize": "[parameters('vmSize')]"
        },
        "osProfile": {
          "computerName": "[concat(parameters('vmNamePrefix'), copyIndex())]",
          "adminUsername": "[parameters('adminUsername')]",
          "adminPassword": "[parameters('adminPassword')]"
        },
        "storageProfile": {
          "imageReference": {
            "publisher": "MicrosoftWindowsServer",
            "offer": "WindowsServer",
            "sku": "2019-Datacenter",
            "version": "latest"
          },
          "osDisk": {
            "createOption": "FromImage"
          }
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('networkInterfaceName'), copyindex()))]"
            }
          ]
        },
        "diagnosticsProfile": {
          "bootDiagnostics": {
            "enabled": true,
            "storageUri": "[reference(variables('storageAccountName')).primaryEndpoints.blob]"
          }
        }
      }
    }
  ]
}

範本中已定義多個 Azure 資源:Multiple Azure resources have been defined in the template:

若要尋找更多有關 Azure Load Balancer 的範本,請參閱 Azure 快速入門範本To find more templates that are related to Azure Load Balancer, see Azure Quickstart Templates.

部署範本Deploy the template

Azure CLIAzure CLI

read -p "Enter the location (i.e. westcentralus): " location
resourceGroupName="myResourceGroupLB"
templateUri="https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/201-2-vms-internal-load-balancer/azuredeploy.json"

az group create \
--name $resourceGroupName \
--location $location

az deployment group create \
--resource-group $resourceGroupName \
--template-uri  $templateUri

檢閱已部署的資源Review deployed resources

  1. 登入 Azure 入口網站Sign in to the Azure portal.

  2. 選取左側面板中的 [資源群組]。Select Resource groups from the left pane.

  3. 選取您在上一節中建立的資源群組。Select the resource group that you created in the previous section. 預設的資源群組名稱為 myResourceGroupLBThe default resource group name is myResourceGroupLB

  4. 確認下列資源是在資源群組中建立的:Verify the following resources were created in the resource group:

使用者 Azure 入口網站以確認資源的建立。

清除資源Clean up resources

若不再需要,您可以使用 az group delete 命令來移除資源群組及其內含的所有資源。When no longer needed, you can use the az group delete command to remove the resource group and all resources contained within.

  az group delete \
    --name myResourceGroupLB

後續步驟Next steps

如需逐步教學課程,以引導您完成建立範本的流程,請參閱:For a step-by-step tutorial that guides you through the process of creating a template, see: