快速入門:使用 ARM 範本建立內部負載平衡器以平衡 VM 的負載
在本快速入門中,您將瞭解如何使用 Azure Resource Manager 範本 (ARM 範本) 來建立內部 Azure 負載平衡器。 內部負載平衡器會將流量分散到位於負載平衡器後端集區之虛擬網路中的虛擬機。 除了內部負載平衡器,此範本也會建立虛擬網路、網路介面、NAT 閘道和 Azure Bastion 實例。
相較於其他部署方法,使用 ARM 範本所需的步驟比較少。
Azure Resource Manager 範本是一個 JavaScript 物件標記法 (JSON) 檔案,會定義專案的基礎結構和設定。 範本使用宣告式語法。 您可以描述預期的部署,而不需要撰寫程式設計命令順序來建立部署。
如果您的環境符合必要條件,而且您很熟悉 ARM 範本,請選取 [部署至 Azure] 按鈕。 範本會在 Azure 入口網站中開啟。
必要條件
如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶。
檢閱範本
本快速入門中使用的範本來自 Azure 快速入門範本。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.27.1.19265",
"templateHash": "14348520895393914284"
}
},
"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_D2s_v3",
"metadata": {
"description": "Size of VM"
}
},
"vNetAddressPrefix": {
"type": "string",
"defaultValue": "10.0.0.0/16",
"metadata": {
"description": "Virtual network address prefix"
}
},
"vNetSubnetAddressPrefix": {
"type": "string",
"defaultValue": "10.0.0.0/24",
"metadata": {
"description": "Backend subnet address prefix"
}
},
"vNetBastionSubnetAddressPrefix": {
"type": "string",
"defaultValue": "10.0.2.0/24",
"metadata": {
"description": "Bastion subnet address prefix"
}
},
"lbPublicIPAddress": {
"type": "string",
"defaultValue": "10.0.0.6",
"metadata": {
"description": "Public IP address of load balancer"
}
}
},
"variables": {
"natGatewayName": "lb-nat-gateway",
"natGatewayPublicIPAddressName": "lb-nat-gateway-ip",
"vNetName": "lb-vnet",
"vNetSubnetName": "backend-subnet",
"storageAccountType": "Standard_LRS",
"storageAccountName": "[uniqueString(resourceGroup().id)]",
"loadBalancerName": "internal-lb",
"networkInterfaceName": "lb-nic",
"numberOfInstances": 2,
"lbSkuName": "Standard",
"bastionName": "lb-bastion",
"bastionSubnetName": "AzureBastionSubnet",
"bastionPublicIPAddressName": "lb-bastion-ip"
},
"resources": [
{
"type": "Microsoft.Network/natGateways",
"apiVersion": "2023-09-01",
"name": "[variables('natGatewayName')]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard"
},
"properties": {
"idleTimeoutInMinutes": 4,
"publicIpAddresses": [
{
"id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('natGatewayPublicIPAddressName'))]"
}
]
},
"dependsOn": [
"[resourceId('Microsoft.Network/publicIPAddresses', variables('natGatewayPublicIPAddressName'))]"
]
},
{
"type": "Microsoft.Network/publicIPAddresses",
"apiVersion": "2023-09-01",
"name": "[variables('natGatewayPublicIPAddressName')]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard"
},
"properties": {
"publicIPAddressVersion": "IPv4",
"publicIPAllocationMethod": "Static",
"idleTimeoutInMinutes": 4
}
},
{
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2023-09-01",
"name": "[variables('vNetName')]",
"location": "[parameters('location')]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"[parameters('vNetAddressPrefix')]"
]
}
}
},
{
"type": "Microsoft.Network/virtualNetworks/subnets",
"apiVersion": "2023-09-01",
"name": "[format('{0}/{1}', variables('vNetName'), variables('bastionSubnetName'))]",
"properties": {
"addressPrefix": "[parameters('vNetBastionSubnetAddressPrefix')]"
},
"dependsOn": [
"[resourceId('Microsoft.Network/virtualNetworks', variables('vNetName'))]"
]
},
{
"type": "Microsoft.Network/virtualNetworks/subnets",
"apiVersion": "2023-09-01",
"name": "[format('{0}/{1}', variables('vNetName'), variables('vNetSubnetName'))]",
"properties": {
"addressPrefix": "[parameters('vNetSubnetAddressPrefix')]",
"natGateway": {
"id": "[resourceId('Microsoft.Network/natGateways', variables('natGatewayName'))]"
}
},
"dependsOn": [
"[resourceId('Microsoft.Network/natGateways', variables('natGatewayName'))]",
"[resourceId('Microsoft.Network/virtualNetworks', variables('vNetName'))]"
]
},
{
"type": "Microsoft.Network/bastionHosts",
"apiVersion": "2023-09-01",
"name": "[variables('bastionName')]",
"location": "[parameters('location')]",
"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'))]"
}
}
}
]
},
"dependsOn": [
"[resourceId('Microsoft.Network/publicIPAddresses', variables('bastionPublicIPAddressName'))]",
"[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vNetName'), variables('bastionSubnetName'))]"
]
},
{
"type": "Microsoft.Network/publicIPAddresses",
"apiVersion": "2023-09-01",
"name": "[variables('bastionPublicIPAddressName')]",
"location": "[parameters('location')]",
"sku": {
"name": "[variables('lbSkuName')]"
},
"properties": {
"publicIPAddressVersion": "IPv4",
"publicIPAllocationMethod": "Static"
}
},
{
"copy": {
"name": "networkInterface",
"count": "[length(range(0, variables('numberOfInstances')))]"
},
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2023-09-01",
"name": "[format('{0}{1}', variables('networkInterfaceName'), range(0, variables('numberOfInstances'))[copyIndex()])]",
"location": "[parameters('location')]",
"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('loadBalancerName'), 'BackendPool1')]"
}
]
}
}
]
},
"dependsOn": [
"[resourceId('Microsoft.Network/loadBalancers', variables('loadBalancerName'))]",
"[resourceId('Microsoft.Network/virtualNetworks', variables('vNetName'))]",
"[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vNetName'), variables('vNetSubnetName'))]"
]
},
{
"type": "Microsoft.Network/loadBalancers",
"apiVersion": "2023-09-01",
"name": "[variables('loadBalancerName')]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard"
},
"properties": {
"frontendIPConfigurations": [
{
"properties": {
"subnet": {
"id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vNetName'), variables('vNetSubnetName'))]"
},
"privateIPAddress": "[parameters('lbPublicIPAddress')]",
"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"
}
]
},
"dependsOn": [
"[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vNetName'), variables('vNetSubnetName'))]"
]
},
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2023-01-01",
"name": "[variables('storageAccountName')]",
"location": "[parameters('location')]",
"sku": {
"name": "[variables('storageAccountType')]"
},
"kind": "StorageV2"
},
{
"copy": {
"name": "vm",
"count": "[length(range(0, variables('numberOfInstances')))]"
},
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2023-09-01",
"name": "[format('{0}{1}', parameters('vmNamePrefix'), range(0, variables('numberOfInstances'))[copyIndex()])]",
"location": "[parameters('location')]",
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('vmSize')]"
},
"osProfile": {
"computerName": "[format('{0}{1}', parameters('vmNamePrefix'), range(0, variables('numberOfInstances'))[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', format('{0}{1}', variables('networkInterfaceName'), range(0, variables('numberOfInstances'))[range(0, variables('numberOfInstances'))[copyIndex()]]))]"
}
]
},
"diagnosticsProfile": {
"bootDiagnostics": {
"enabled": true,
"storageUri": "[reference(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2023-01-01').primaryEndpoints.blob]"
}
}
},
"dependsOn": [
"[resourceId('Microsoft.Network/networkInterfaces', format('{0}{1}', variables('networkInterfaceName'), range(0, variables('numberOfInstances'))[range(0, variables('numberOfInstances'))[copyIndex()]]))]",
"[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
]
}
],
"outputs": {
"location": {
"type": "string",
"value": "[parameters('location')]"
},
"name": {
"type": "string",
"value": "[variables('loadBalancerName')]"
},
"resourceGroupName": {
"type": "string",
"value": "[resourceGroup().name]"
},
"resourceId": {
"type": "string",
"value": "[resourceId('Microsoft.Network/loadBalancers', variables('loadBalancerName'))]"
}
}
}
範本中已定義多個 Azure 資源:
- Microsoft.Network/virtualNetworks:負載平衡器和虛擬機器的虛擬網路。
- Microsoft.Network/networkInterfaces:虛擬機器的網路介面。
- Microsoft.Network/loadBalancers:內部負載平衡器。
- Microsoft.Network/natGateways
- Microsoft.Network/publicIPAddresses:NAT 閘道和 Azure Bastion 的公用 IP 位址。
- Microsoft.Compute/virtualMachines:後端集區中的虛擬機。
- Microsoft.Network/bastionHosts:Azure Bastion 實例。
- Microsoft.Network/virtualNetworks/subnets:虛擬網路的子網。
- Microsoft.Storage/storageAccounts:虛擬機的記憶體帳戶。
若要尋找更多有關 Azure Load Balancer 的範本,請參閱 Azure 快速入門範本。
部署範本
在此步驟中,您會使用 Azure PowerShell 搭配 [New-AzResourceGroupDeployment](/powershell/module/az.resources/new-azresourcegroupdeployment)
命令來部署範本。
選取以下程式碼區塊的 [試用] 以開啟 Azure Cloud Shell,然後遵循指示登入 Azure。
使用 Azure CLI 或 Azure PowerShell 部署 Bicep 檔案。
echo "Enter a project name with 12 or less letters or numbers that is used to generate Azure resource names" read projectName echo "Enter the location (i.e. centralus)" read location resourceGroupName="${projectName}rg" templateUri="https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.network/internal-loadbalancer-create/azuredeploy.json" az group create --name $resourceGroupName --location $location az deployment group create --resource-group $resourceGroupName --template-uri $templateUri --name $projectName --parameters location=$location read -p "Press [ENTER] to continue."
系統會提示您輸入下列值:
- projectName:用於產生資源名稱。
- adminUsername:虛擬機器系統管理員使用者名稱。
- adminPassword:虛擬機器系統管理員密碼。
部署範本需要約 10 分鐘。
Azure PowerShell 或 Azure CLI 可用來部署範本。 您也可以使用 Azure 入口網站 和 REST API。 若要了解其他部署方法,請參閱部署範本。
檢閱已部署的資源
使用 Azure CLI 或 Azure PowerShell,使用下列命令列出資源群組中已部署的資源:
az resource list --resource-group $resourceGroupName
清除資源
若不再需要,請使用 Azure CLI 或 Azure PowerShell,使用下列命令刪除資源群組及其資源:
Remove-AzResourceGroup -Name "${projectName}rg"
下一步
如需逐步教學課程,以引導您完成建立範本的流程,請參閱: