Skip to content

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.
Diagram: visitors from the internet reach two web servers in one virtual network through a load balancer and through an Application Gateway that routes by URL path.
Two web servers behind a load balancer (Task 2) and an Application Gateway (Task 3). Select the diagram to open it full size.

Before you start

  • You are signed in to the lab directory (Getting started).
  • Your lab resource group is rg-CLOUD-LAB-GRP-N. Replace N with 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.

  1. Search for Deploy a custom template and select it.
  2. Select Build your own template in the editor.
  3. 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 } }
          }
        }
      ]
    }
    
  4. Fill in the form:

    Setting Value
    Resource group rg-CLOUD-LAB-GRP-N
    Admin Password the VM password
    Vm Size Standard_B1s
    Location southeastasia3
  5. Select Review + create, then Create. Wait for the deployment to complete (about 5 minutes).

  6. 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.

  1. Search for Load balancers and select + Create.
  2. On Basics:

    Setting Value
    Resource group rg-CLOUD-LAB-GRP-N
    Name lab-lb
    Region Southeast Asia 3
    SKU Standard
    Type Public
    Tier Regional
  3. Select Next: Frontend IP configuration and + Add a frontend IP configuration:

    Setting Value
    Name lab-fe
    IP type IP address
    Public IP address Create new
  4. 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].

  5. Select Next: Backend pools and + Add a backend pool:

    Setting Value
    Name lab-be
    Virtual network lab-06-vnet1
    Backend pool configuration NIC

    Select + Add, check both lab-06-vm0 and lab-06-vm1, and select Add, then Save.

  6. Select Review + create, then Create. Go to the resource.

Add the load balancing rule

  1. Under Settings, select Load balancing rules → + Add:

    Setting Value
    Name lab-lbrule
    IP version IPv4
    Frontend IP address lab-fe
    Backend pool lab-be
    Protocol TCP
    Port 80
    Backend port 80
  2. Under Health probe, select Create new: name lab-hp, protocol TCP, port 80, interval 5. Select Save.

  3. Leave the other settings as they are and select Save.

Test the load balancer

  1. On the load balancer, open Frontend IP configuration and copy the public IP address.
  2. Open http://<the IP address> in a browser. You should see Hello World from lab-06-vm0 or Hello World from lab-06-vm1.
  3. 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

  1. Open lab-06-vnet1. Under Settings, select Subnets → + Subnet.
  2. Name subnet-appgw, starting address 10.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

  1. Search for Application gateways and select + Create.
  2. On Basics:

    Setting Value
    Resource group rg-CLOUD-LAB-GRP-N
    Name lab-appgw
    Region Southeast Asia 3
    Tier Standard V2
    Enable autoscaling No
    Instance count 1
    IP address type IPv4 only
    HTTP2 Disabled
    Virtual network lab-06-vnet1
    Subnet subnet-appgw

    Standard V2 only

    WAF V2 isn't allowed and is blocked with [LAB-P08C-APP-GATEWAY].

  3. On Frontends:

    • Frontend IP address type: Public.
    • Public IP address: Add new, name lab-gwpip. If you see an Availability zone option, select None.
  4. 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-appgwbe both VMs: the NICs of lab-06-vm0 (10.60.0.4) and lab-06-vm1 (10.60.1.4)
    lab-imagebe lab-06-vm0 only
    lab-videobe lab-06-vm1 only
  5. On Configuration, select + Add a routing rule:

    Setting Value
    Rule name lab-gwrule
    Priority 10
    Listener name lab-listener
    Frontend IP Public IPv4
    Protocol HTTP
    Port 80
    Listener type Basic
  6. On the Backend targets tab of the rule:

    • Target type: Backend pool, target lab-appgwbe.
    • Backend settings: Add new, name lab-http, protocol HTTP, port 80. 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/* images lab-http lab-imagebe
    /video/* videos lab-http lab-videobe
  7. Select Add twice. Then Review + create and Create. This takes 5 to 10 minutes.

Test the gateway

  1. Open lab-appgw. Under Monitoring, select Backend health. Both servers should show Healthy.
  2. On the Overview page, copy the Frontend public IP address.
  3. Open http://<the IP address>/image/. You should see Images served by lab-06-vm0.
  4. Open http://<the IP address>/video/. You should see Videos 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:

  1. Delete lab-appgw (the Application Gateway). This is the one that costs the most.
  2. Delete lab-lb (the load balancer).
  3. Delete the two public IP addresses, lab-gwpip and lab-lbpip. They stay behind after the gateway and load balancer are deleted.
  4. Delete the VMs lab-06-vm0 and lab-06-vm1. The template set the disks and network cards to be deleted with them.
  5. Delete lab-06-vnet1, then lab-06-nsg1.
  6. Check that your resource group has none of these resources left.

Check your work

  • The load balancer address showed Hello World from both VMs.
  • Backend health of the Application Gateway said Healthy for both VMs.
  • /image/ came from lab-06-vm0 and /video/ from lab-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.