title | description | services | documentationcenter | author | manager | editor | tags | Customer intent | ms.assetid | ms.service | ms.devlang | ms.topic | ms.tgt_pltfrm | ms.workload | ms.date | ms.author | ms.custom |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Quickstart:Create a public load balancer - Azure CLI | Microsoft Docs |
This quickstart shows how to create a public load balancer using the Azure CLI |
load-balancer |
na |
KumudD |
jeconnoc |
azure-resource-manager |
I want to create a Basic Load balancer so that I can load balance internet traffic to VMs. |
load-balancer |
na |
get-started-article |
na |
infrastructure-services |
03/19/2018 |
kumud |
mvc |
This quickstart shows you how to create an Azure Load Balancer. To test the load balancer, you deploy two virtual machines (VMs) running Ubuntu server and load balance a web app between.
[!INCLUDE cloud-shell-try-it.md]
If you choose to install and use the CLI locally, this tutorial requires that you are running a version of the Azure CLI version 2.0.28 or later. To find the version, run az --version
. If you need to install or upgrade, see Install Azure CLI.
Create a resource group with az group create. An Azure resource group is a logical container into which Azure resources are deployed and managed.
The following example creates a resource group named myResourceGroupLB in the eastus location:
az group create \
--name myResourceGroupLB \
--location eastus
To access your web app on the Internet, you need a public IP address for the load balancer. Use az network public-ip create to create a public IP address named myPublicIP in myResourceGroupLB.
az network public-ip create --resource-group myResourceGroupLB --name myPublicIP
This section details how you can create and configure the following components of the load balancer:
- a frontend IP pool that receives the incoming network traffic on the load balancer.
- a backend IP pool where the frontend pool sends the load balanced network traffic.
- a health probe that determines health of the backend VM instances.
- a load balancer rule that defines how traffic is distributed to the VMs.
Create a public Azure Load Balancer with az network lb create named myLoadBalancer that includes a frontend pool named myFrontEndPool, a backend pool named myBackEndPool that is associated with the public IP address myPublicIP that you created in the preceding step.
az network lb create \
--resource-group myResourceGroupLB \
--name myLoadBalancer \
--public-ip-address myPublicIP \
--frontend-ip-name myFrontEndPool \
--backend-pool-name myBackEndPool
A health probe checks all virtual machine instances to make sure they can send network traffic. The virtual machine instance with failed probe checks is removed from the load balancer until it goes back online and a probe check determines that it's healthy. Create a health probe with az network lb probe create to monitor the health of the virtual machines.
az network lb probe create \
--resource-group myResourceGroupLB \
--lb-name myLoadBalancer \
--name myHealthProbe \
--protocol tcp \
--port 80
A load balancer rule defines the frontend IP configuration for the incoming traffic and the backend IP pool to receive the traffic, along with the required source and destination port. Create a load balancer rule myLoadBalancerRuleWeb with az network lb rule create for listening to port 80 in the frontend pool myFrontEndPool and sending load-balanced network traffic to the backend address pool myBackEndPool also using port 80.
az network lb rule create \
--resource-group myResourceGroupLB \
--lb-name myLoadBalancer \
--name myHTTPRule \
--protocol tcp \
--frontend-port 80 \
--backend-port 80 \
--frontend-ip-name myFrontEndPool \
--backend-pool-name myBackEndPool \
--probe-name myHealthProbe
Before you deploy some VMs and can test your load balancer, create the supporting virtual network resources.
Create a virtual network named myVnet with a subnet named mySubnet in the myResourceGroup using az network vnet create.
az network vnet create \
--resource-group myResourceGroupLB \
--location eastus \
--name myVnet \
--subnet-name mySubnet
Create network security group to define inbound connections to your virtual network.
az network nsg create \
--resource-group myResourceGroupLB \
--name myNetworkSecurityGroup
Create a network security group rule to allow inbound connections through port 80.
az network nsg rule create \
--resource-group myResourceGroupLB \
--nsg-name myNetworkSecurityGroup \
--name myNetworkSecurityGroupRuleHTTP \
--protocol tcp \
--direction inbound \
--source-address-prefix '*' \
--source-port-range '*' \
--destination-address-prefix '*' \
--destination-port-range 80 \
--access allow \
--priority 200
Create three network interfaces with az network nic create and associate them with the Public IP address and the network security group.
for i in `seq 1 2`; do
az network nic create \
--resource-group myResourceGroupLB \
--name myNic$i \
--vnet-name myVnet \
--subnet mySubnet \
--network-security-group myNetworkSecurityGroup \
--lb-name myLoadBalancer \
--lb-address-pools myBackEndPool
done
In this example, you create three virtual machines to be used as backend servers for the load balancer. To verify that the load balancer was successfully created, you also install NGINX on the virtual machines .
Create an availability set with az vm availabilityset create
az vm availability-set create \
--resource-group myResourceGroupLB \
--name myAvailabilitySet
You can use a cloud-init configuration file to install NGINX and run a 'Hello World' Node.js app on a Linux virtual machine. In your current shell, create a file named cloud-init.txt and copy and paste the following configuration into the shell. Make sure that you copy the whole cloud-init file correctly, especially the first line:
#cloud-config
package_upgrade: true
packages:
- nginx
- nodejs
- npm
write_files:
- owner: www-data:www-data
- path: /etc/nginx/sites-available/default
content: |
server {
listen 80;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
- owner: azureuser:azureuser
- path: /home/azureuser/myapp/index.js
content: |
var express = require('express')
var app = express()
var os = require('os');
app.get('/', function (req, res) {
res.send('Hello World from host ' + os.hostname() + '!')
})
app.listen(3000, function () {
console.log('Hello world app listening on port 3000!')
})
runcmd:
- service nginx restart
- cd "/home/azureuser/myapp"
- npm init
- npm install express -y
- nodejs index.js
Create the virtual machines with az vm create.
for i in `seq 1 2`; do
az vm create \
--resource-group myResourceGroupLB \
--name myVM$i \
--availability-set myAvailabilitySet \
--nics myNic$i \
--image UbuntuLTS \
--generate-ssh-keys \
--custom-data cloud-init.txt
--no-wait
done
It may take a few minutes for the VMs to get deployed.
To get the public IP address of the load balancer, use az network public-ip show. Copy the public IP address, and then paste it into the address bar of your browser.
az network public-ip show \
--resource-group myResourceGroupLB \
--name myPublicIP \
--query [ipAddress] \
--output tsv
When no longer needed, you can use the az group delete command to remove the resource group, load balancer, and all related resources.
az group delete --name myResourceGroupLB
In this quickstart, you created a Basic Load Balancer, attached VMs to it, configured the load balancer traffic rule, health probe, and then tested the load balancer. To learn more about Azure Load Balancer, continue to the tutorials for Azure Load Balancer.
[!div class="nextstepaction"] Azure Load Balancer tutorials