|
| 1 | +--- |
| 2 | +title: 'Quickstart: Create a basic public load balancer - Azure CLI' |
| 3 | +titleSuffix: Azure Load Balancer |
| 4 | +description: Learn how to create a public basic SKU Azure Load Balancer in this quickstart using the Azure CLI. |
| 5 | +author: asudbring |
| 6 | +ms.author: allensu |
| 7 | +ms.service: load-balancer |
| 8 | +ms.topic: quickstart |
| 9 | +ms.date: 03/16/2022 |
| 10 | +ms.custom: template-quickstart |
| 11 | +--- |
| 12 | + |
| 13 | +# Quickstart: Create a basic public load balancer using the Azure CLI |
| 14 | + |
| 15 | +Get started with Azure Load Balancer by using the Azure portal to create a basic public load balancer and two virtual machines. |
| 16 | + |
| 17 | +[!INCLUDE [quickstarts-free-trial-note](../../../includes/quickstarts-free-trial-note.md)] |
| 18 | + |
| 19 | +[!INCLUDE [azure-cli-prepare-your-environment.md](../../../includes/azure-cli-prepare-your-environment.md)] |
| 20 | + |
| 21 | +- This quickstart requires version 2.0.28 or later of the Azure CLI. If using Azure Cloud Shell, the latest version is already installed. |
| 22 | + |
| 23 | +>[!NOTE] |
| 24 | +>Standard SKU load balancer is recommended for production workloads. For more information about SKUs, see **[Azure Load Balancer SKUs](../skus.md)**. |
| 25 | +
|
| 26 | +## Create a resource group |
| 27 | + |
| 28 | +An Azure resource group is a logical container into which Azure resources are deployed and managed. |
| 29 | + |
| 30 | +Create a resource group with [az group create](/cli/azure/group#az_group_create): |
| 31 | + |
| 32 | +```azurecli |
| 33 | + az group create \ |
| 34 | + --name CreatePubLBQS-rg \ |
| 35 | + --location eastus |
| 36 | +``` |
| 37 | + |
| 38 | +## Create a virtual network |
| 39 | + |
| 40 | +Before you deploy VMs and test your load balancer, create the supporting virtual network and subnet. |
| 41 | + |
| 42 | +Create a virtual network using [az network vnet create](/cli/azure/network/vnet#az_network_vnet_create). The virtual network and subnet will contain the resources deployed later in this article. |
| 43 | + |
| 44 | +```azurecli |
| 45 | + az network vnet create \ |
| 46 | + --resource-group CreatePubLBQS-rg \ |
| 47 | + --location eastus \ |
| 48 | + --name myVNet \ |
| 49 | + --address-prefixes 10.1.0.0/16 \ |
| 50 | + --subnet-name myBackendSubnet \ |
| 51 | + --subnet-prefixes 10.1.0.0/24 |
| 52 | +``` |
| 53 | + |
| 54 | +## Create a public IP address |
| 55 | + |
| 56 | +To access your web app on the Internet, you need a public IP address for the load balancer. |
| 57 | + |
| 58 | +Use [az network public-ip create](/cli/azure/network/public-ip#az_network_public_ip_create) to create the public IP for the load balancer frontend. |
| 59 | + |
| 60 | +```azurecli |
| 61 | + az network public-ip create \ |
| 62 | + --resource-group CreatePubLBQS-rg \ |
| 63 | + --name myPublicIP \ |
| 64 | + --sku Basic |
| 65 | +``` |
| 66 | + |
| 67 | +## Create a load balancer |
| 68 | + |
| 69 | +This section details how you can create and configure the following components of the load balancer: |
| 70 | + |
| 71 | + * A frontend IP pool that receives the incoming network traffic on the load balancer |
| 72 | + |
| 73 | + * A backend IP pool where the frontend pool sends the load balanced network traffic |
| 74 | + |
| 75 | + * A health probe that determines health of the backend VM instances |
| 76 | + |
| 77 | + * A load balancer rule that defines how traffic is distributed to the VMs |
| 78 | + |
| 79 | +### Create the load balancer resource |
| 80 | + |
| 81 | +Create a public load balancer with [az network lb create](/cli/azure/network/lb#az_network_lb_create): |
| 82 | + |
| 83 | +```azurecli |
| 84 | + az network lb create \ |
| 85 | + --resource-group CreatePubLBQS-rg \ |
| 86 | + --name myLoadBalancer \ |
| 87 | + --sku Basic \ |
| 88 | + --public-ip-address myPublicIP \ |
| 89 | + --frontend-ip-name myFrontEnd \ |
| 90 | + --backend-pool-name myBackEndPool |
| 91 | +``` |
| 92 | + |
| 93 | +### Create the health probe |
| 94 | + |
| 95 | +A health probe checks all virtual machine instances to ensure they can send network traffic. |
| 96 | + |
| 97 | +A virtual machine with a failed probe check is removed from the load balancer. The virtual machine is added back into the load balancer when the failure is resolved. |
| 98 | + |
| 99 | +Create a health probe with [az network lb probe create](/cli/azure/network/lb/probe#az_network_lb_probe_create): |
| 100 | + |
| 101 | +```azurecli |
| 102 | + az network lb probe create \ |
| 103 | + --resource-group CreatePubLBQS-rg \ |
| 104 | + --lb-name myLoadBalancer \ |
| 105 | + --name myHealthProbe \ |
| 106 | + --protocol tcp \ |
| 107 | + --port 80 |
| 108 | +``` |
| 109 | + |
| 110 | +### Create the load balancer rule |
| 111 | + |
| 112 | +A load balancer rule defines: |
| 113 | + |
| 114 | +* Frontend IP configuration for the incoming traffic |
| 115 | + |
| 116 | +* The backend IP pool to receive the traffic |
| 117 | + |
| 118 | +* The required source and destination port |
| 119 | + |
| 120 | +Create a load balancer rule with [az network lb rule create](/cli/azure/network/lb/rule#az_network_lb_rule_create): |
| 121 | + |
| 122 | +```azurecli |
| 123 | + az network lb rule create \ |
| 124 | + --resource-group CreatePubLBQS-rg \ |
| 125 | + --lb-name myLoadBalancer \ |
| 126 | + --name myHTTPRule \ |
| 127 | + --protocol tcp \ |
| 128 | + --frontend-port 80 \ |
| 129 | + --backend-port 80 \ |
| 130 | + --frontend-ip-name myFrontEnd \ |
| 131 | + --backend-pool-name myBackEndPool \ |
| 132 | + --probe-name myHealthProbe \ |
| 133 | + --idle-timeout 15 |
| 134 | +``` |
| 135 | + |
| 136 | +## Create a network security group |
| 137 | + |
| 138 | +For a standard load balancer, the VMs in the backend address for are required to have network interfaces that belong to a network security group. |
| 139 | + |
| 140 | +Use [az network nsg create](/cli/azure/network/nsg#az_network_nsg_create) to create the network security group: |
| 141 | + |
| 142 | +```azurecli |
| 143 | + az network nsg create \ |
| 144 | + --resource-group CreatePubLBQS-rg \ |
| 145 | + --name myNSG |
| 146 | +``` |
| 147 | + |
| 148 | +### Create a network security group rule |
| 149 | + |
| 150 | +Create a network security group rule using [az network nsg rule create](/cli/azure/network/nsg/rule#az_network_nsg_rule_create): |
| 151 | + |
| 152 | +```azurecli |
| 153 | + az network nsg rule create \ |
| 154 | + --resource-group CreatePubLBQS-rg \ |
| 155 | + --nsg-name myNSG \ |
| 156 | + --name myNSGRuleHTTP \ |
| 157 | + --protocol '*' \ |
| 158 | + --direction inbound \ |
| 159 | + --source-address-prefix '*' \ |
| 160 | + --source-port-range '*' \ |
| 161 | + --destination-address-prefix '*' \ |
| 162 | + --destination-port-range 80 \ |
| 163 | + --access allow \ |
| 164 | + --priority 200 |
| 165 | +``` |
| 166 | + |
| 167 | +## Create a bastion host |
| 168 | + |
| 169 | +In this section, you'll create te resources for Azure Bastion. Azure Bastion is used to securely manage the virtual machines in the backend pool of the load balancer. |
| 170 | + |
| 171 | +### Create a public IP address |
| 172 | + |
| 173 | +Use [az network public-ip create](/cli/azure/network/public-ip#az_network_public_ip_create) to create a public ip address for the bastion host. The public IP is used by the bastion host for secure access to the virtual machine resources. |
| 174 | + |
| 175 | +```azurecli |
| 176 | + az network public-ip create \ |
| 177 | + --resource-group CreatePubLBQS-rg \ |
| 178 | + --name myBastionIP \ |
| 179 | + --sku Standard \ |
| 180 | + --zone 1 2 3 |
| 181 | +``` |
| 182 | +### Create a bastion subnet |
| 183 | + |
| 184 | +Use [az network vnet subnet create](/cli/azure/network/vnet/subnet#az_network_vnet_subnet_create) to create a bastion subnet. The bastion subnet is used by the bastion host to access the virtual network. |
| 185 | + |
| 186 | +```azurecli |
| 187 | + az network vnet subnet create \ |
| 188 | + --resource-group CreatePubLBQS-rg \ |
| 189 | + --name AzureBastionSubnet \ |
| 190 | + --vnet-name myVNet \ |
| 191 | + --address-prefixes 10.1.1.0/27 |
| 192 | +``` |
| 193 | + |
| 194 | +### Create bastion host |
| 195 | + |
| 196 | +Use [az network bastion create](/cli/azure/network/bastion#az_network_bastion_create) to create a bastion host. The bastion host is used to connect securely to the virtual machine resources created later in this article. |
| 197 | + |
| 198 | +```azurecli |
| 199 | + az network bastion create \ |
| 200 | + --resource-group CreatePubLBQS-rg \ |
| 201 | + --name myBastionHost \ |
| 202 | + --public-ip-address myBastionIP \ |
| 203 | + --vnet-name myVNet \ |
| 204 | + --location eastus |
| 205 | +``` |
| 206 | + |
| 207 | +It can take a few minutes for the Azure Bastion host to deploy. |
| 208 | + |
| 209 | +## Create backend servers |
| 210 | + |
| 211 | +In this section, you create: |
| 212 | + |
| 213 | +* Two network interfaces for the virtual machines |
| 214 | + |
| 215 | +* Two virtual machines to be used as backend servers for the load balancer |
| 216 | + |
| 217 | +### Create network interfaces for the virtual machines |
| 218 | + |
| 219 | +Create two network interfaces with [az network nic create](/cli/azure/network/nic#az_network_nic_create): |
| 220 | + |
| 221 | +```azurecli |
| 222 | + array=(myNicVM1 myNicVM2) |
| 223 | + for vmnic in "${array[@]}" |
| 224 | + do |
| 225 | + az network nic create \ |
| 226 | + --resource-group CreatePubLBQS-rg \ |
| 227 | + --name $vmnic \ |
| 228 | + --vnet-name myVNet \ |
| 229 | + --subnet myBackEndSubnet \ |
| 230 | + --network-security-group myNSG |
| 231 | + done |
| 232 | +``` |
| 233 | + |
| 234 | +### Create availability set for virtual machines |
| 235 | + |
| 236 | +Create the availability set with [az vm availability-set create](/cli/azure/vm/availability-set#az_vm_availability_set_create): |
| 237 | + |
| 238 | +```azurecli |
| 239 | + az vm availability-set create \ |
| 240 | + --name myAvSet \ |
| 241 | + --resource-group CreatePubLBQS-rg \ |
| 242 | + --location eastus |
| 243 | + |
| 244 | +``` |
| 245 | + |
| 246 | +### Create virtual machines |
| 247 | + |
| 248 | +Create the virtual machines with [az vm create](/cli/azure/vm#az_vm_create): |
| 249 | + |
| 250 | +```azurecli |
| 251 | + az vm create \ |
| 252 | + --resource-group CreatePubLBQS-rg \ |
| 253 | + --name myVM1 \ |
| 254 | + --nics myNicVM1 \ |
| 255 | + --image win2019datacenter \ |
| 256 | + --admin-username azureuser \ |
| 257 | + --availability-set myAvSet \ |
| 258 | + --no-wait |
| 259 | +``` |
| 260 | + |
| 261 | +```azurecli |
| 262 | + az vm create \ |
| 263 | + --resource-group CreatePubLBQS-rg \ |
| 264 | + --name myVM2 \ |
| 265 | + --nics myNicVM2 \ |
| 266 | + --image win2019datacenter \ |
| 267 | + --admin-username azureuser \ |
| 268 | + --availability-set myAvSet \ |
| 269 | + --no-wait |
| 270 | +``` |
| 271 | + |
| 272 | +It may take a few minutes for the VMs to deploy. You can continue to the next steps while the VMs are creating. |
| 273 | + |
| 274 | +[!INCLUDE [ephemeral-ip-note.md](../../../includes/ephemeral-ip-note.md)] |
| 275 | + |
| 276 | +### Add virtual machines to load balancer backend pool |
| 277 | + |
| 278 | +Add the virtual machines to the backend pool with [az network nic ip-config address-pool add](/cli/azure/network/nic/ip-config/address-pool#az_network_nic_ip_config_address_pool_add): |
| 279 | + |
| 280 | +```azurecli |
| 281 | + array=(myNicVM1 myNicVM2) |
| 282 | + for vmnic in "${array[@]}" |
| 283 | + do |
| 284 | + az network nic ip-config address-pool add \ |
| 285 | + --address-pool myBackendPool \ |
| 286 | + --ip-config-name ipconfig1 \ |
| 287 | + --nic-name $vmnic \ |
| 288 | + --resource-group CreatePubLBQS-rg \ |
| 289 | + --lb-name myLoadBalancer |
| 290 | + done |
| 291 | +``` |
| 292 | + |
| 293 | +## Install IIS |
| 294 | + |
| 295 | +Use [az vm extension set](/cli/azure/vm/extension#az_vm_extension_set) to install IIS on the virtual machines and set the default website to the computer name. |
| 296 | + |
| 297 | +```azurecli |
| 298 | + array=(myVM1 myVM2) |
| 299 | + for vm in "${array[@]}" |
| 300 | + do |
| 301 | + az vm extension set \ |
| 302 | + --publisher Microsoft.Compute \ |
| 303 | + --version 1.8 \ |
| 304 | + --name CustomScriptExtension \ |
| 305 | + --vm-name $vm \ |
| 306 | + --resource-group CreatePubLBQS-rg \ |
| 307 | + --settings '{"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $($env:computername)"}' |
| 308 | + done |
| 309 | +``` |
| 310 | + |
| 311 | +## Test the load balancer |
| 312 | + |
| 313 | +To get the public IP address of the load balancer, use [az network public-ip show](/cli/azure/network/public-ip#az_network_public_ip_show). |
| 314 | + |
| 315 | +Copy the public IP address, and then paste it into the address bar of your browser. |
| 316 | + |
| 317 | +```azurecli |
| 318 | + az network public-ip show \ |
| 319 | + --resource-group CreatePubLBQS-rg \ |
| 320 | + --name myPublicIP \ |
| 321 | + --query ipAddress \ |
| 322 | + --output tsv |
| 323 | +``` |
| 324 | + |
| 325 | +## Clean up resources |
| 326 | + |
| 327 | +When no longer needed, use the [az group delete](/cli/azure/group#az_group_delete) command to remove the resource group, load balancer, and all related resources. |
| 328 | + |
| 329 | +```azurecli |
| 330 | + az group delete \ |
| 331 | + --name CreatePubLBQS-rg |
| 332 | +``` |
| 333 | + |
| 334 | +## Next steps |
| 335 | + |
| 336 | +In this quickstart: |
| 337 | + |
| 338 | +* You created a basic public load balancer |
| 339 | + |
| 340 | +* Attached two virtual machines |
| 341 | + |
| 342 | +* Configured the load balancer traffic rule and health probe |
| 343 | + |
| 344 | +* Tested the load balancer |
| 345 | + |
| 346 | +To learn more about Azure Load Balancer, continue to: |
| 347 | +> [!div class="nextstepaction"] |
| 348 | +> [What is Azure Load Balancer?](../load-balancer-overview.md) |
0 commit comments