Tutorial: Load balance VMs within an availability zone by using the Azure portal

This tutorial creates a public load balancer with a zonal IP. In the tutorial, you specify a zone for your frontend and backend instances.

In this tutorial, you learn how to:

  • Create a virtual network with an Azure Bastion host for management.
  • Create a NAT gateway for outbound internet access of the resources in the virtual network.
  • Create a load balancer with a health probe and traffic rules.
  • Create zonal virtual machines (VMs) and attach them to a load balancer.
  • Create a basic Internet Information Services (IIS) site.
  • Test the load balancer.

For more information about availability zones and a standard load balancer, see Standard load balancer and availability zones.

Prerequisites

  • An Azure subscription

Sign in to Azure

Sign in to the Azure portal at https://portal.azure.com.

Create the virtual network

In this section, you'll create a virtual network and subnet.

  1. In the search box at the top of the portal, enter Virtual network. Select Virtual Networks in the search results.

  2. In Virtual networks, select + Create.

  3. In Create virtual network, enter or select this information in the Basics tab:

    Setting Value
    Project Details
    Subscription Select your Azure subscription
    Resource Group Select Create new.
    In Name enter CreateZonalLBTutorial-rg.
    Select OK.
    Instance details
    Name Enter myVNet
    Region Select (Europe) West Europe
  4. Select the IP Addresses tab or select the Next: IP Addresses button at the bottom of the page.

  5. In the IP Addresses tab, enter this information:

    Setting Value
    IPv4 address space Enter 10.1.0.0/16
  6. Select + Add subnet.

  7. On the Add subnet page, enter this information:

    Setting Value
    Subnet name Enter myBackendSubnet
    Subnet address range Enter 10.1.0.0/24
  8. Select Add.

  9. Select the Security tab.

  10. Under BastionHost, select Enable. Enter this information:

    Setting Value
    Bastion name Enter myBastionHost
    AzureBastionSubnet address space Enter 10.1.1.0/26
    Public IP Address Select Create new.
    For Name, enter myBastionIP.
    Select OK.
  11. Select the Review + create tab or select the Review + create button.

  12. Select Create.

Create NAT gateway

In this section, you'll create a NAT gateway for outbound internet access for resources in the virtual network.

  1. In the search box at the top of the portal, enter NAT gateway. Select NAT gateways in the search results.

  2. In NAT gateways, select + Create.

  3. In Create network address translation (NAT) gateway, enter or select the following information:

    Setting Value
    Project details
    Subscription Select your subscription.
    Resource group Select CreateZonalLBTutorial-rg.
    Instance details
    NAT gateway name Enter myNATgateway.
    Availability zone Select 1.
    Idle timeout (minutes) Enter 15.
  4. Select the Outbound IP tab or select the Next: Outbound IP button at the bottom of the page.

  5. In Outbound IP, for Public IP addresses, select Create a new public IP address.

  6. On the Add a public IP address page, for Name, enter myNATGatewayIP.

  7. Select OK.

  8. Select the Subnet tab or select the Next: Subnet button at the bottom of the page.

  9. On the Subnet page, for Virtual network, select myVNet from the dropdown.

  10. For Subnet name, select myBackendSubnet.

  11. Select the Review + create button at the bottom of the page, or select the Review + create tab.

  12. Select Create.

Create load balancer

In this section, you'll create a zonal load balancer that load balances virtual machines.

During the creation of the load balancer, you'll configure:

  • Frontend IP address
  • Backend pool
  • Inbound load-balancing rules
  1. In the search box at the top of the portal, enter Load balancer. Select Load balancers in the search results.

  2. In the Load balancer page, select Create.

  3. In the Basics tab of the Create load balancer page, enter or select the following information:

    Setting Value
    Project details
    Subscription Select your subscription.
    Resource group Select CreateZonalLBTutorial-rg.
    Instance details
    Name Enter myLoadBalancer
    Region Select (Europe) West Europe.
    SKU Leave the default Standard.
    Type Select Public.
    Tier Leave the default Regional.
  4. Select Next: Frontend IP configuration at the bottom of the page.

  5. In Frontend IP configuration, select + Add a frontend IP configuration.

  6. For Name, type LoadBalancerFrontend.

  7. For IP version, select either IPv4 or IPv6.

    Note

    IPv6 isn't currently supported with Routing Preference or Cross-region load-balancing (Global Tier).

  8. For IP type, select IP address.

    Note

    For more information on IP prefixes, see Azure Public IP address prefix.

  9. For Public IP address, select Create new.

  10. On the Add a public IP address page, for Name, enter myPublicIP.

  11. For Availability zone, select 1 from the dropdown, then click OK to close the Add a public IP address page.

    Note

    In regions with Availability Zones, you have the option to select no-zone (default option), a specific zone, or zone-redundant. The choice will depend on your specific domain failure requirements. In regions without Availability Zones, this field won't appear.
    For more information on availability zones, see Availability zones overview.

  12. If you see Routing preference settings, leave the default of Microsoft Network for Routing preference.

  13. Select OK.

  14. Select Add.

  15. At the bottom of the page, select Next: Backend pools.

  16. On the Backend pools page, select + Add a backend pool.

  17. On the Add backend pool page, for Name, type myBackendPool.

  18. For Virtual network, select myVNet from the dropdown.

  19. For Backend Pool Configuration, select either NIC or IP Address.

  20. Select Save.

  21. At the bottom of the page, select the Next: Inbound rules button.

  22. On the Inbound rules page, for Load balancing rule, select + Add a load balancing rule.

  23. On the Add load balancing rule page, enter or select the following information:

    Setting Value
    Name Enter myHTTPRule
    IP Version Select IPv4 or IPv6 depending on your requirements.
    Frontend IP address Select LoadBalancerFrontend.
    Backend pool Select myBackendPool.
    Protocol Select TCP.
    Port Enter 80.
    Backend port Enter 80.
    Health probe Select Create new.
    In Name, enter myHealthProbe.
    Select HTTP in Protocol.
    Leave the rest of the defaults, and select OK.
    Session persistence Select None.
    Idle timeout (minutes) Enter or select 15.
    TCP reset Select Enabled.
    Floating IP Select Disabled.
    Outbound source network address translation (SNAT) Leave the default of (Recommended) Use outbound rules to provide backend pool members access to the internet.
  24. Select Add.

  25. At the bottom of the page, select the Review + create button.

  26. Select Create.

    Note

    In this example we created a NAT gateway to provide outbound Internet access. The outbound rules tab in the configuration is bypassed as it's optional isn't needed with the NAT gateway. For more information on Azure NAT gateway, see What is Azure Virtual Network NAT? For more information about outbound connections in Azure, see Source Network Address Translation (SNAT) for outbound connections

Create virtual machines

In this section, you'll create three VMs (myVM1, myVM2, and myVM3) in one zone (Zone 1).

These VMs are added to the backend pool of the load balancer that was created earlier.

  1. On the upper-left side of the portal, select Create a resource > Compute > Virtual machine.

  2. In Create a virtual machine, type or select the values in the Basics tab:

    Setting Value
    Project Details
    Subscription Select your Azure subscription
    Resource Group Select CreateZonalLBTutorial-rg
    Instance details
    Virtual machine name Enter myVM1
    Region Select (Europe) West Europe
    Availability Options Select Availability zone
    Availability zone Select 1
    Image Select Windows Server 2019 Datacenter - Gen1
    Azure Spot instance Leave the default of unchecked.
    Size Choose VM size or take default setting
    Administrator account
    Username Enter a username
    Password Enter a password
    Confirm password Reenter password
    Inbound port rules
    Public inbound ports Select None
  3. Select the Networking tab, or select Next: Disks, then Next: Networking.

  4. In the Networking tab, select or enter:

    Setting Value
    Network interface
    Virtual network myVNet
    Subnet myBackendSubnet
    Public IP Select None.
    NIC network security group Select Advanced
    Configure network security group Select Create new.
    In the Create network security group, enter myNSG in Name.
    Under Inbound rules, select +Add an inbound rule.
    Under Service, select HTTP.
    Under Priority, enter 100.
    In Name, enter myNSGRule
    Select Add
    Select OK
    Load balancing
    Place this virtual machine behind an existing load-balancing solution? Select the check box.
    Load balancing settings
    Load-balancing options Select Azure load balancing
    Select a load balancer Select myLoadBalancer
    Select a backend pool Select myBackendPool
  5. Select Review + create.

  6. Review the settings, and then select Create.

  7. Follow the steps 1 to 8 to create two more VMs with the following values and all the other settings the same as myVM1:

    Setting VM 2 VM 3
    Name myVM2 myVM3
    Availability zone 1 1
    Network security group Select the existing myNSG Select the existing myNSG

Note

Azure provides a default outbound access IP for VMs that either aren't assigned a public IP address or are in the back-end pool of an internal basic Azure load balancer. The default outbound access IP mechanism provides an outbound IP address that isn't configurable.

The default outbound access IP is disabled when a public IP address is assigned to the VM, the VM is placed in the back-end pool of a standard load balancer, with or without outbound rules, or if an Azure Virtual Network NAT gateway resource is assigned to the subnet of the VM.

VMs that are created by virtual machine scale sets in flexible orchestration mode don't have default outbound access.

For more information about outbound connections in Azure, see Default outbound access in Azure and Use source network address translation (SNAT) for outbound connections.

Install IIS

  1. Select All services in the left-hand menu, select All resources, and then from the resources list, select myVM1 that is located in the CreateZonalLBTutorial-rg resource group.

  2. On the Overview page, select Connect, then Bastion.

  3. Select Use Bastion.

  4. Enter the username and password entered during VM creation.

  5. Select Connect.

  6. On the server desktop, navigate to Windows Administrative Tools > Windows PowerShell.

  7. In the PowerShell Window, run the following commands to:

    • Install the IIS server
    • Remove the default iisstart.htm file
    • Add a new iisstart.htm file that displays the name of the VM:
     # Install IIS server role
     Install-WindowsFeature -name Web-Server -IncludeManagementTools
    
     # Remove default htm file
     Remove-Item  C:\inetpub\wwwroot\iisstart.htm
    
     # Add a new htm file that displays server name
     Add-Content -Path "C:\inetpub\wwwroot\iisstart.htm" -Value $("Hello World from " + $env:computername)
    
  8. Close the Bastion session with myVM1.

  9. Repeat steps 1 to 8 to install IIS and the updated iisstart.htm file on myVM2 and myVM3.

Test the load balancer

  1. In the search box at the top of the page, enter Load balancer. Select Load balancers in the search results.

  2. Click the load balancer you created, myLoadBalancer. On the Frontend IP configuration page for your load balancer, locate the public IP address.

  3. Copy the public IP address, and then paste it into the address bar of your browser. The custom VM page of the IIS Web server is displayed in the browser.

    Screenshot of load balancer test

Clean up resources

When no longer needed, delete the resource group, load balancer, and all related resources. To do so, select the resource group CreateZonalLBTutorial-rg that contains the resources and then select Delete.

Next steps

Advance to the next article to learn how to load balance VMs across availability zones: