Lab 06: Share the load across web servers¶
At a glance
- Time: about 75 minutes (the Application Gateway takes 5 to 10 minutes to create)
- Region: Southeast Asia 3
- You create: 2 small Ubuntu web servers, 1 load balancer and 1 Application Gateway. You use the same two VMs for both.
- Cost: the Application Gateway is the most expensive item in the lab. Delete it as soon as you finish Task 3.
What you will learn¶
- How a load balancer (layer 4) spreads traffic across VMs.
- How an Application Gateway (layer 7) routes by the URL path.
- How both use a backend pool and a health probe.
What is different in this environment
- Two VMs, not three. The two Ubuntu VMs from Task 1 serve both the load balancer (Task 2) and the Application Gateway (Task 3). You don't create more VMs.
- The VMs run nginx and are set up automatically by the template. You don't install anything.
- Southeast Asia 3, size Standard_B1s, your own resource group.
- The Application Gateway uses 1 instance and no autoscaling, to keep the cost down.
Before you start¶
- You are signed in to the lab directory (Getting started).
- Your lab resource group is
rg-CLOUD-LAB-GRP-N. ReplaceNwith your group number. - Delete the VMs from Lab 05 first. Each group may hold only 2 VMs.
- Have the VM password from your trainer ready (the one for user
azureuser).
The plan¶
| Resource | Name | Notes |
|---|---|---|
| Virtual network | lab-06-vnet1 |
10.60.0.0/16 |
| Subnets | subnet0, subnet1 |
10.60.0.0/24, 10.60.1.0/24 |
| Network security group | lab-06-nsg1 |
allows HTTP (port 80) from the internet |
| Virtual machines | lab-06-vm0, lab-06-vm1 |
Ubuntu 24.04, nginx, 10.60.0.4 and 10.60.1.4 |
| Load balancer | lab-lb |
Task 2 |
| Application Gateway | lab-appgw |
Task 3 |
Task 1: Deploy the web servers from a template¶
The template creates the network, the NSG and both VMs. Each VM shows a web page with its own name.
- Search for Deploy a custom template and select it.
- Select Build your own template in the editor.
-
Delete everything in the editor and paste this template. Select Save.
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "adminPassword": { "type": "securestring" }, "vmSize": { "type": "string", "defaultValue": "Standard_B1s" }, "location": { "type": "string", "defaultValue": "southeastasia3" } }, "variables": { "vnetName": "lab-06-vnet1", "nsgName": "lab-06-nsg1" }, "resources": [ { "type": "Microsoft.Network/networkSecurityGroups", "apiVersion": "2023-09-01", "name": "[variables('nsgName')]", "location": "[parameters('location')]", "properties": { "securityRules": [ { "name": "AllowHTTP", "properties": { "priority": 100, "direction": "Inbound", "access": "Allow", "protocol": "Tcp", "sourceAddressPrefix": "Internet", "sourcePortRange": "*", "destinationAddressPrefix": "*", "destinationPortRange": "80" } } ] } }, { "type": "Microsoft.Network/virtualNetworks", "apiVersion": "2023-09-01", "name": "[variables('vnetName')]", "location": "[parameters('location')]", "dependsOn": [ "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsgName'))]" ], "properties": { "addressSpace": { "addressPrefixes": [ "10.60.0.0/16" ] }, "subnets": [ { "name": "subnet0", "properties": { "addressPrefix": "10.60.0.0/24", "networkSecurityGroup": { "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsgName'))]" } } }, { "name": "subnet1", "properties": { "addressPrefix": "10.60.1.0/24", "networkSecurityGroup": { "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsgName'))]" } } } ] } }, { "type": "Microsoft.Network/networkInterfaces", "apiVersion": "2023-09-01", "name": "[concat('lab-06-nic', copyIndex())]", "location": "[parameters('location')]", "copy": { "name": "nicLoop", "count": 2 }, "dependsOn": [ "[resourceId('Microsoft.Network/virtualNetworks', variables('vnetName'))]" ], "properties": { "ipConfigurations": [ { "name": "ipconfig1", "properties": { "privateIPAllocationMethod": "Static", "privateIPAddress": "[concat('10.60.', copyIndex(), '.4')]", "subnet": { "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vnetName'), concat('subnet', copyIndex()))]" } } } ] } }, { "type": "Microsoft.Compute/virtualMachines", "apiVersion": "2024-03-01", "name": "[concat('lab-06-vm', copyIndex())]", "location": "[parameters('location')]", "copy": { "name": "vmLoop", "count": 2 }, "dependsOn": [ "[resourceId('Microsoft.Network/networkInterfaces', concat('lab-06-nic', copyIndex()))]" ], "properties": { "hardwareProfile": { "vmSize": "[parameters('vmSize')]" }, "osProfile": { "computerName": "[concat('lab-06-vm', copyIndex())]", "adminUsername": "azureuser", "adminPassword": "[parameters('adminPassword')]", "customData": "[base64(format('#cloud-config\npackage_update: true\npackages:\n - nginx\nwrite_files:\n - path: /var/www/html/index.html\n content: |\n Hello World from {0}\n - path: /var/www/html/image/index.html\n content: |\n Images served by {0}\n - path: /var/www/html/video/index.html\n content: |\n Videos served by {0}\nruncmd:\n - systemctl restart nginx\n', concat('lab-06-vm', copyIndex())))]", "linuxConfiguration": { "disablePasswordAuthentication": false } }, "storageProfile": { "imageReference": { "publisher": "Canonical", "offer": "ubuntu-24_04-lts", "sku": "server", "version": "latest" }, "osDisk": { "createOption": "FromImage", "managedDisk": { "storageAccountType": "StandardSSD_LRS" }, "deleteOption": "Delete" } }, "networkProfile": { "networkInterfaces": [ { "id": "[resourceId('Microsoft.Network/networkInterfaces', concat('lab-06-nic', copyIndex()))]", "properties": { "deleteOption": "Delete" } } ] }, "diagnosticsProfile": { "bootDiagnostics": { "enabled": false } } } } ] } -
Fill in the form:
Setting Value Resource group rg-CLOUD-LAB-GRP-NAdmin Password the VM password Vm Size Standard_B1sLocation southeastasia3 -
Select Review + create, then Create. Wait for the deployment to complete (about 5 minutes).
- Open your resource group. You should see two VMs, two network interfaces, two disks, a virtual network and an NSG. There is no public IP address on the VMs.
Wait a few more minutes
After the VMs show Running, give them about 3 minutes to install nginx. The web pages don't work before that.
Think about it
Find the AllowHTTP rule in the template. Which traffic does it allow, and why is it needed even though the VMs have no public IP?
Task 2: Load balance the web servers¶
A public load balancer gives your VMs one public address and spreads the connections across them.
- Search for Load balancers and select + Create.
-
On Basics:
Setting Value Resource group rg-CLOUD-LAB-GRP-NName lab-lbRegion Southeast Asia 3 SKU Standard Type Public Tier Regional -
Select Next: Frontend IP configuration and + Add a frontend IP configuration:
Setting Value Name lab-feIP type IP address Public IP address Create new -
In the new public IP: name
lab-lbpip, SKU Standard, tier Regional, assignment Static. Save, then save again.Standard SKU only
A Basic public IP or a Global tier is blocked with
[LAB-P08A-PUBLIC-IP]. -
Select Next: Backend pools and + Add a backend pool:
Setting Value Name lab-beVirtual network lab-06-vnet1Backend pool configuration NIC Select + Add, check both
lab-06-vm0andlab-06-vm1, and select Add, then Save. -
Select Review + create, then Create. Go to the resource.
Add the load balancing rule¶
-
Under Settings, select Load balancing rules → + Add:
Setting Value Name lab-lbruleIP version IPv4 Frontend IP address lab-feBackend pool lab-beProtocol TCP Port 80Backend port 80 -
Under Health probe, select Create new: name
lab-hp, protocol TCP, port80, interval5. Select Save. - Leave the other settings as they are and select Save.
Test the load balancer¶
- On the load balancer, open Frontend IP configuration and copy the public IP address.
- Open
http://<the IP address>in a browser. You should seeHello World from lab-06-vm0orHello World from lab-06-vm1. - Refresh a few times, or use a private browser window. The name may change.
A browser reuses connections
A browser often keeps the same connection, so it may stay on one VM. To see the sharing clearly, use Cloud Shell (PowerShell, No storage account required), replace the address, and run:
1..10 | ForEach-Object { (Invoke-WebRequest -UseBasicParsing "http://<the IP address>").Content.Trim() }
You should see both VM names in the output.
Think about it
The load balancer works at layer 4: it sees only IP addresses and ports. What does that mean for requests to /image/ and /video/?
Task 3: Route by URL with an Application Gateway¶
An Application Gateway works at layer 7. It reads the web request, so it can send /image/ to one server and /video/ to another. It uses the same two VMs as the load balancer.
Add the gateway subnet¶
- Open
lab-06-vnet1. Under Settings, select Subnets → + Subnet. - Name
subnet-appgw, starting address10.60.3.224, size/27. Select Add.
A subnet of its own
An Application Gateway needs a dedicated subnet of /27 or larger. Nothing else can be in it.
Create the gateway¶
- Search for Application gateways and select + Create.
-
On Basics:
Setting Value Resource group rg-CLOUD-LAB-GRP-NName lab-appgwRegion Southeast Asia 3 Tier Standard V2 Enable autoscaling No Instance count 1IP address type IPv4 only HTTP2 Disabled Virtual network lab-06-vnet1Subnet subnet-appgwStandard V2 only
WAF V2 isn't allowed and is blocked with
[LAB-P08C-APP-GATEWAY]. -
On Frontends:
- Frontend IP address type: Public.
- Public IP address: Add new, name
lab-gwpip. If you see an Availability zone option, select None.
-
On Backends, add three backend pools. For each, set Add backend pool without targets to No, and choose Virtual machine as the target type:
Pool name Target lab-appgwbeboth VMs: the NICs of lab-06-vm0(10.60.0.4) andlab-06-vm1(10.60.1.4)lab-imagebelab-06-vm0onlylab-videobelab-06-vm1only -
On Configuration, select + Add a routing rule:
Setting Value Rule name lab-gwrulePriority 10Listener name lab-listenerFrontend IP Public IPv4 Protocol HTTP Port 80Listener type Basic -
On the Backend targets tab of the rule:
- Target type: Backend pool, target
lab-appgwbe. - Backend settings: Add new, name
lab-http, protocol HTTP, port80. Select Add. - Under Path-based routing, select Add multiple targets to create a path-based rule and add two rules:
Path Target name Backend settings Backend target /image/*imageslab-httplab-imagebe/video/*videoslab-httplab-videobe - Target type: Backend pool, target
-
Select Add twice. Then Review + create and Create. This takes 5 to 10 minutes.
Test the gateway¶
- Open
lab-appgw. Under Monitoring, select Backend health. Both servers should show Healthy. - On the Overview page, copy the Frontend public IP address.
- Open
http://<the IP address>/image/. You should seeImages served by lab-06-vm0. - Open
http://<the IP address>/video/. You should seeVideos served by lab-06-vm1.
If you get an error
A 502 Bad Gateway usually means the backend isn't healthy yet. Check Backend health, wait a minute and refresh. If both VMs say Unhealthy, check that nginx has finished installing (wait 3 more minutes).
Think about it
Compare Task 2 and Task 3. Which tool can send /image/ and /video/ to different servers, and which one is better for non-web traffic?
Task 4: Clean up¶
Start with the most expensive resource. Delete in this order:
- Delete
lab-appgw(the Application Gateway). This is the one that costs the most. - Delete
lab-lb(the load balancer). - Delete the two public IP addresses,
lab-gwpipandlab-lbpip. They stay behind after the gateway and load balancer are deleted. - Delete the VMs
lab-06-vm0andlab-06-vm1. The template set the disks and network cards to be deleted with them. - Delete
lab-06-vnet1, thenlab-06-nsg1. - Check that your resource group has none of these resources left.
Check your work¶
- The load balancer address showed
Hello Worldfrom both VMs. - Backend health of the Application Gateway said Healthy for both VMs.
-
/image/came fromlab-06-vm0and/video/fromlab-06-vm1. - Your resource group is empty again, including public IP addresses.
Key points¶
- A load balancer works on IP addresses and ports (layer 4). An Application Gateway reads the web request (layer 7), so it can route by URL.
- Both use a backend pool and a health probe. Traffic goes only to healthy servers.
- The same VMs can sit behind both at the same time.
- An Application Gateway needs its own subnet.
- Delete expensive resources as soon as you finish with them.