Create a Standard Load Balancer with Azure CLI

1 hour
  • 2 Learning Objectives

About this Hands-on Lab

Your company has an application that is overloaded. Whenever there’s a spike in traffic, the application is unresponsive. You must implement a load balancer to save the application from crashing and improve response times.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create Virtual Machines
  1. Log in to the Azure Portal and open a Bash prompt in Cloud Shell.
  2. Create a network interface for your VMs.
  3. Create two VMs to add to the load balancing pool.
Create a Load Balancer
  1. Create a load balancer and load balancing rules for port 80.
  2. Test the connectivity to your load balancer by accessing the public IP address from a new browser tab.

Additional Resources

Note: When setting up the Azure CLI, if you have trouble using the existing storage account, choose to create a new storage account with a unique name.

You've been provided with one VM with this hands-on lab, which is the VM that hosts your application now. You will create a network interface for the two new VMs you are to create that will be load balanced. You will then create the VMs with the appropriate network interfaces attached. Use the following cloud-init script to install the packages during the deployment:

package_upgrade: true
  - nginx
  - nodejs
  - npm
  - 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!')
  - service nginx restart
  - cd "/home/azureuser/myapp"
  - npm init
  - npm install express -y
  - nodejs index.js

Once you've deployed the VMs, create a standard load balancer with a frontend and backend pool. The load balancer should also have rules for port 80 going to the frontend pool.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?