Setting-Up Scale set and Load balancer On Azure
Published: 2022-10-15 09:56:15

In this project ,I will show how to do the following :

- Create a virtual machine to be syspreped and added to the Azure compute gallery.

- How to Sysprep an image on Azure and add it to the Azure Compute gallery.

- Create a scale set from the image in the Azure compute gallery.

- Place a public load balancer in front of the scale set.

- Simulate a load intensive process on the scale set to confirm the scaling policy of the scale set.


**- Create A Virtual Machine :**

From all resources, select create a virtual machine

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ixrovz6stwue4u5b68mr.png)_**(IMAGE 1)**_

You can set up the virtual machine in any region, I will be using a Windows Server 2022 Datacenter Server Core-Gen 1 for this project. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/07atairi67y3fgzyc067.png)_**(IMAGE 2)**_

Allow the Port 80- HTTP (so that the users can access the webpage over the internet), and Port 3389-(to RDP into the virtual machine)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fkdfqqxhgzc6jf7xqve0.png)_**(IMAGE 3)**_

In the network tab, allow the application wizard to create a virtual network and subnet for the virtual machine.

There is no further configuration to be added or completed; simply click the other tabs to review and create the virtual machine. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3c0br37fm2chhrne872.png) _**(IMAGE 4)**_


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9v2mrc9dlww3vvv113bl.png) **(IMAGE 5)**

**- Setting UP IIS (Internet Information Service) Web Server On The Virtual Machine**

In the networking tab, we configure the RDP and map it to our IP address ( a security measure to keep access to the virtual machine restricted to the allowed IP address and from potential hackers).

In the source drop-down menu, select "My IP address." But if you have multiple IP addresses that you want to also give access to, use the (IP Address) option as shown in image 6, and input the IP addresses, each separated by a semicolon.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v97ag154p9bumhd9msbc.png) _**(IMAGE 6)**_


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4s2yuq85bhvqndfza9z8.png) _**(IMAGE 7)**_

In the connect session , download the RDP file and launch the executable file, and connect to the virtual machine.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d2nv179c53ipftz1odc9.png) _**(IMAGE 8)**_

For Windows users, you already have the RDP client pre-installed on the system.

For MAC (Apple Computers), you will need to download the Remote Desktop Client to use the RDP file executable.

For some devices that cannot run an RDP client, such as the Google Chromebook, Azure has made available the option, Azure Bastion, to log in to the virtual machine. For more information, see: https://learn.microsoft.com/en-us/azure/bastion/bastion-overview

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wj0mjme7g3kpg03jp0xu.png) _**(IMAGE 9)**_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pem40gu617xxtbajt893.png) _**(IMAGE 10)**_

When you have logged into the virtual machine, launch the server manager from the start menu. If the server does not launch automatically, from the dashboard of the server manager, click Add roles and features so we can install IIS on the server.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92panbntxer7uoqwa5nk.png) _**(IMAGE 11)**_

Check the role-based or feature-based installation.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48cw8g6t1c5pkbroz6im.png) _**(IMAGE 12)**_

Click your server name from the server pool.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqhg6uy4vylybjutrsyz.png) _**(IMAGE 13)**_

In the select roles section, tick the web server IIS and click the option to add the feature and complete the installation process.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ja78wn16an2ek6wfl58.png) 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x7pym2lsjtkudy4x7hpt.png) _**(IMAGE 14)**_

Go to the web root folder path, located in - windows-C:\inetpub\wwwroot\ 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sfl8kc6dyxrxcxn800fl.png) _**(IMAGE 15)**_

From Azure Portal, copy the public IP attached to the virtual machine and enter it into your browser. The default IIS webpage should be displayed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gdllo47chj0cprizpucf.png) _**(IMAGE 16)**_

Replacing the default IIS webpage with my Portfolio page, http://dgns.southafricanorth.cloudapp.azure.com/

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yboh7a8jir8md6ddcw3s.png) _**(IMAGE 17)**_


**- Sysprep an Image on Azure and add it to Azure Compute gallery**

Sysprep (System Preparation) prepares a Windows client or Windows Server installation for imaging 

Sysprep helps you Manage multiple PCs by creating a generic image that can be used across multiple hardware designs. For more information : https://learn.microsoft.com/en-us/windows-hardware/manufacture/desktop/sysprep--system-preparation--overview?view=windows-11#sysprep-features

Since we want to spin up a scale set , we want the image to be replicated across multiple servers when users from different locations try to access the content of our web server. which is a good use case for the design for business availability and redundancy when there is a large web traffic spike to a single server infrastructure. For more information, see: https://learn.microsoft.com/en-us/azure/virtual-machines/availability

To launch the sysprep program located in the directory %windir%\system32\sysprep, tick the generalize option and also shutdown 
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7emwwta39q7g1aph0pn8.png) 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/syi7wilr1mj1esgej8md.png) _**(IMAGE 18)**_


**-Azure Compute gallery**
An Azure Compute Gallery helps you build structure and organization around your Azure resources, like images and applications. For more information see: https://learn.microsoft.com/en-us/azure/virtual-machines/azure-compute-gallery

From the Azure portal, when the virtual machine shuts down, we use the capture button to create an image to be added to the Azure compute gallery. We select the option to generalise the image and create an image definition in the Azure compute gallery.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ejpkpb42tjvo8f36a3tw.png) _**(IMAGE 19)**_

Then click the review and create button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/db5vsjyzma7fhct2stbo.png) _**(IMAGE 20)**_

Go to the resource after it has completed the set-up. You can also locate Azure compute galleries from the Azure portal global search. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x5u12t2j1fuv5oefq9ai.png) _**(IMAGE 21)**_


**- Create a scale set from the image in Azure compute gallery**
Azure virtual machine scale sets let you create and manage a group of load-balanced VMs. The number of VM instances can automatically increase or decrease in response to demand or a defined schedule.

For more information : https://learn.microsoft.com/en-us/azure/virtual-machine-scale-sets/overview#why-use-virtual-machine-scale-sets

Click the Create the VMSS button after selecting the image definition we created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u37ckh2h58n0h3monzio.png) _**(IMAGE 22)**_

Creating the VMSS with availability options (depending on the business design needs, it is not a must) and orchestration mode as uniform , for more information : https://learn.microsoft.com/en-us/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-orchestration-modes

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jq6aezw7mawp24jtvr4b.png) _**(IMAGE 23)**_

We create the VMSS virtual machines (1 or more). In the networking tab of the virtual creation process, we will edit and disable Public IP , since we will put a load balancer in front of the VMSS.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3s3cevkt98dw1zxq7qny.png) 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m5wlhzoyk9toctuyo5jq.png) _**(IMAGE 24)**_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a43vr8qf6lomqi64yfpp.png) _**(IMAGE 25)**_

We won't add a load balancer from this VMSS creation wizard. We will create it as a standalone resource and attach it to the VMSS.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rfzj25578gx71ettz53y.png) _**(IMAGE 26)**_

Create the virtual machine scale set, for this project:

- initial count for the virtual machine to be one, 

- use a custom scaling policy, 

- scale out (adding more instances when these conditions are met) -CPU threshold >= 45 for a duration of five minutes and scale in policy (remove instance when CPU threshold is <= 40,

- Maximum number of instances set to 8.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ve45i0cpdg26ri3qkz5o.png) _**(IMAGE 27)**_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2o97k590mc5no2cgubx.png) _**(IMAGE 28)**_

We tick the option to allow Azure to over provision for us in the case of critical virtual machine health and traffic spikes, when there is a delay in removing faulty virtual machines.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dm4ssnufn4veboeql9fo.png) _**(IMAGE 29)**_

Click the review and create button. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcdjjj2p8v0xr35zjsda.png) _**(IMAGE 30)**_

The created virtual machine has no public IP attached to it, and the instance count is one.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v243g1hw95r0jb802liv.png) _**(IMAGE 31)**_

In the monitoring section of the VMSS, the CPU average is under 20%.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g2ia9vfreeg491ccwg4m.png) _**(IMAGE 32)**_

**- Place a public load balancer in front of the scale set**

Create the load balancer with the following parameters: since we are attaching the load balancer to a VMSS, we can't use a Basic SKU load balancer, and since we want users to access the webpage of the VMSS, we create the load balancer as a Public , Tier Regional.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rbu1syqkk0kkt5banpi.png) _**(IMAGE 33)**_

Frontend IP configuration , where we set up the public IP address for the backend (VMSS).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wqybkgoy8a9kbn2j3ei9.png) _**(IMAGE 34)**_

Backend pool, select the virtual network where the VMSS is located, with backend pool configuration (NIC).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o62te7xxvgjiga81y1ok.png) _**(IMAGE 35)**_

Because in my environment , I have two virtual machines in the same VNET. It shows all the available virtual machines in the VNET that can be added to the backend pool.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dbp6pvteaz6o5qzgh460.png) _**(IMAGE 36)**_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/85ghfz9m9u720a1iip8b.png) _**(IMAGE 37)**_

In the inbound rules section, we add the load balancing rules, using the configurations highlighted below :

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kcmqpbgoua0nk1sf13on.png) _**(IMAGE 38)**_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zdc7jp7nx63jb6nsc82r.png) _**(IMAGE 39)**_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/emdjyeiqpeva7ugjuwgq.png) _**(IMAGE 40)**_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqzo4ot3x5kfvu3egjqm.png) _**(IMAGE 41)**_

We won't be adding outbound rules to the load balancer; we'll review and create them.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ulgawdnbk0qsiw22r5sb.png) _**(IMAGE 42)**_

We confirm the frontendIP configuration that will be attached to the VMSS is available (23.164.39.101).
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fifp7xgmukkpndfeuzpo.png) _**(IMAGE 43)**_

Also confirm from the Azure portal for the Virtual Machine Scale Set that the frontend-IP configuration from the load balancer has been attached to the Virtual Machine Scale Set (enlarged(23.3164.39.101)).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e56z70jnmtrgbhknbo00.png) _**(IMAGE 44)**_


Let's see if the IP still redirects to our page, and sure enough, it still loads my portfolio page. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vm9x2v9pxl7mi6k2o2bo.png) _**(IMAGE 45)**_

Under the monitoring tab of the VMSS, the CPU average should be stable since the workload the webserver is receiving is stable. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hjr5121wqpx5ol54c17d.png) _**(IMAGE 46)**_

Also confirming from the instance itself, the CPU average is also stable
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2bagdacdo1wstuvwotue.png) _**(IMAGE 47)**_


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jzbplb3vccv7x82cg9we.png) _**(IMAGE 48)**_


**- Simulate a load intensive process on the scale set to confirm the scaling policy of the scale set.**

In this section, we will attempt to simulate a CPU intensive workload on the webserver, to confirm if the scale set , scale-out (adding more servers to cope with the increased workload when the CPU threshold is exceeded), and scale-in (removing extra servers when the CPU intensive process reduces and within the threshold set for CPU average) are correct.

We will use the Azure serial console, which will give us access to the guest OS operation of the webserver. We provisioned a Windows server. For more information, see: https://learn.microsoft.com/en-us/troubleshoot/azure/virtual-machines/serial-console-overview

When the prompts appear in the command prompt after launching the serial console, do the following :

- in the SAC prompt,input : cmd

- in the channel prompt, input : ch -si 1

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/13trscyai700zafrka9j.png) _**(IMAGE 49)**_


Input your username, then press enter for domain (the server is not domain joined), and your password.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vy53xruh5b5sqwxeou1n.png) _**(IMAGE 50)**_


Then, in the windows prompts to switch to PowerShell , type PowerShell. The script to help simulate is a PowerShell script.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xkbuihuvild2pcqpirbu.png) _**(IMAGE 51)**_

Copy and paste the PowerShell script into the prompt and press enter when it prompts for the enter key. 
The PowerShell script is in the code snippet below. Sourced from this site : https://www.robvit.com/windows_server/generate-cpu-load-with-powershell/

**NOTE: ** Please don't use this script in a production or critical server environment. This a test environment and project, and the servers will be deleted after this exercise.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ir7pxsymy3iit5qr7fsh.png) _**(IMAGE 52)**_

```

$NumberOfLogicalProcessors = Get-WmiObject win32_processor | Select-Object -ExpandProperty NumberOfLogicalProcessors
 
ForEach ($core in 1..$NumberOfLogicalProcessors){ 
 
start-job -ScriptBlock{
 
    $result = 1;
    foreach ($loopnumber in 1..2147483647){
        $result=1;
        
        foreach ($loopnumber1 in 1..2147483647){
        $result=1;
            
            foreach($number in 1..2147483647){
                $result = $result * $number
            }
        }
 
            $result
        }
    }
}
 
Read-Host "Press any key to exit..."
Stop-Job * 
```

After some minutes of rechecking the CPU average metrics of the virtual machine, there is a sudden spike in the CPU usage.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/57molmh5aqe7muykyvzo.png) _**(IMAGE 53)**_


Checking the instance count of the virtual machines, there is a scale-out ( more instances are being added).
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5njjrxx6cke06cqtgg7.png) _**(IMAGE 54)**_

From the monitoring tab of the virtual machine scale set, we can see the spike in the CPU average (Note : this was taken after the CPU average had gone back to the normal level, to show the peak and low periods).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ymvf24qq8wsmsmm7d2z.png) _**(IMAGE 55)**_

The instance count confirms that the total number of virtual machines has been added and deleted. In the scaling rule, the instance maximum limit was pegged at 8 instances, as shown in _**image 57**_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zith60i4z4y0u457joz3.png) _**(IMAGE 56)**_


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0acyx24zav9eb1ptu5dd.png) _**(IMAGE 57)**_


The instance been automatically deleted, when scale-in rule kicked in.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gt7ruo3il95dw2pc423r.png) _**(IMAGE 58)**_


Another look at the CPU average when it has gone back to its normal state after the spike.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x9dryryjgtxc0lfvyvn1.png) _**(IMAGE 59)**_

And we are back to one virtual machine (named enlargedscale_3 in my environment, I started with enlargedscale_1).
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0w8xh7o4mpbywkbxfd3.png) _**(IMAGE 60)**_

The next projects will be working on the Arm template to spin up virtual machines and different resources.

Links to other Azure projects :
Setting-Up A Basic Azure Load Balancer & Availability Sets https://dev.to/cli_tayo/setting-up-a-basic-azure-load-balancer-availability-sets-14m1

Adding A Virtual Machine To An Existing Availability sets On Azure
https://dev.to/cli_tayo/adding-a-virtual-machine-to-an-existing-availability-sets-on-azure-ahh