Windows Azure - Load balancing a website running on different Virtual Machines
In this post, we shall create two Windows Virtual Machines in Azure, set up the end points for communication, deploy a web application and finally enable the load balancing mechanism to distribute the load.
To start with, we shall first create a virtual machine on azure. If you do not have a Windows Azure account yet, subscribe for a free trial here.
Once you create a trial, just log on to manage.windowsazure.com and create a new Virtual Machine.
Here are the steps to create. Select NEW -> COMPUTE > VIRTUAL MACHINE > QUICK CREATE. Provide a DNS Name, Select OS as Windows Server 2008, provide a windows password and you are good to go. Click the check-mark arrow.
You will see something like this. Wait for some time.
And finally, your VM Should be up and running in a few minutes.
Open the virtual machine you just created by clicking on the name.
Here are the steps to create. Select NEW -> COMPUTE > VIRTUAL MACHINE > FROM GALLERY. (Click on it)
A popup will be opened
Select Windows Server 2008 image from the list. click Next. In the next screen, provide a VM name (VM 2 for example), provide a new windows password and click next.
You are done with your deployment now. Close all the remote desktop connections. Return back to the Azure Management Portal.
You have the Virtual Machines up and running but there is no way for the rest of the world to access your webpages. We need to create endpoints for both the Virtual Machines so that the webpages inside can be accessed by public. (ie. to make your website Public Facing). To do that,
We have now added an endpoint so that anyone can talk to VM1 using the port 80.
From the Azure portal, select VM2, Navigate to END POINTS tab, click on the Add new End point button at the bottom. But this time, select the option "Load Balance Traffic on an existing Endpoint".
Click Next. Provide the details as mentioned below. (Name & Port)
Yes. We are done. We have 2 VM's connected. We also have created End Points for both. We also have enabled the Load Balancing option for the VM2. Now, just find out the IP of your VM1 or VM2. To do that, go to the Azure Portal Dashboard, open your VM. Just note down the Public Virtual IP Address. In my case it is 168.62.36.130.
That is all you had to do. Thanks for following it up. Now, from any pc (having internet access) launch your favorite browser and hit http://<Your Public Virtual IP Address>/first/index.html
In my case it will be http://168.62.36.130/first/index.html . You may see any one of the pages.
"This page is served from Virtual Machine 2" or "This page is served from Virtual Machine 1"
Just switch your browser, access the same URL again and again from different browsers or after clearing browser cache, randomly you will either see a page from VM1 or may be from VM2.
So, Windows Azure did the Magical job of Load Balancing your websites. Some times, the request is sent to VM1 and some times to VM2 sharing the load.
However, this is just for illustration. In real scenarios, you will deploy the same code in both VM's. If you are even smart, you will deploy your code on 1 VM and clone/copy it as second VM.
There are about 20 screenshots in the article making it really big. But the concept is too small and simple. Isn't it?
To start with, we shall first create a virtual machine on azure. If you do not have a Windows Azure account yet, subscribe for a free trial here.
Once you create a trial, just log on to manage.windowsazure.com and create a new Virtual Machine.
You will see something like this. Wait for some time.
Open the virtual machine you just created by clicking on the name.
Click on the CONNECT button at the bottom. You will be prompted to download the RDP File. Just save it and open it.
Provide the password and you should be able to get inside your brand new virtual machine in no time. Install the IIS in your Virtual Machine (By Adding a New Role, Selecting IIS Web Server to Install).
You are almost done with your First VM. Now, let us create a second VM. But this time, we shall create the second one in a different way.
A popup will be opened
Select Windows Server 2008 image from the list. click Next. In the next screen, provide a VM name (VM 2 for example), provide a new windows password and click next.
You will see the below screen once you click on the "Next" arrow from the above screen. Here, you have to select the option "Connect to an existing virtual machine" and from the dropdown, select the name of the First VM you just created.
Click next, do not modify anything else and click Complete.
After a few minutes, even this VM will be up and running. Now, in the same way, connect to 2nd VM through RDC and login. Install IIS (like we did for first VM).
So, finally we have two VM's connected and with IIS installed. Close the remote desktop client for now.
The next step is to create two html files locally on your machine.
Just open up notepad/visual studio and create a new index.html file with the below content.
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Welcome</title> </head> <body> <h1> This page is served from Virtual Machine 1</h1> </body> </html>
Create a directory VM1 and save the above index.html file inside.
Now, let us create one more index.html file with a different heading
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Welcome</title> </head> <body> <h1> This page is served from Virtual Machine 2</h1> </body> </html>
Create a directory VM2 and save the above index.html file inside.
So, now we have two folders VM1 & VM2 and each has a different index.html file. We are almost done. Here are the steps now to deploy this code on your VM's
- Login to VM1 using RDC
- Just copy the VM1 folder from your local desktop. and paste it inside your d drive of VM1
- Open IIS, Create a new web application by name "first" and point it to the VM1 folder (From Step.2)
- Browser the website http://localhost/first/index.html . You will see the below kind of output
And now,
- Login to VM2 using RDC
- Just copy the VM2 folder from your local desktop. and paste it inside your d drive of VM2.
- Open IIS, Create a new web application by name "first" and point it to the VM2 folder.
- Launch http://localhost/first/index.html . You will see the below screen.
You are done with your deployment now. Close all the remote desktop connections. Return back to the Azure Management Portal.
You have the Virtual Machines up and running but there is no way for the rest of the world to access your webpages. We need to create endpoints for both the Virtual Machines so that the webpages inside can be accessed by public. (ie. to make your website Public Facing). To do that,
- Open the VM1 in Azure Portal
- Navigate to ENDPOINTS tab (tabs will be on top)
- Click on the ADD ENDPOINT button, provide the name as http, select TCP protocol and enter 80 for public and private ports. It should look like below.
We have now added an endpoint so that anyone can talk to VM1 using the port 80.
From the Azure portal, select VM2, Navigate to END POINTS tab, click on the Add new End point button at the bottom. But this time, select the option "Load Balance Traffic on an existing Endpoint".
Click Next. Provide the details as mentioned below. (Name & Port)
Yes. We are done. We have 2 VM's connected. We also have created End Points for both. We also have enabled the Load Balancing option for the VM2. Now, just find out the IP of your VM1 or VM2. To do that, go to the Azure Portal Dashboard, open your VM. Just note down the Public Virtual IP Address. In my case it is 168.62.36.130.
That is all you had to do. Thanks for following it up. Now, from any pc (having internet access) launch your favorite browser and hit http://<Your Public Virtual IP Address>/first/index.html
In my case it will be http://168.62.36.130/first/index.html . You may see any one of the pages.
"This page is served from Virtual Machine 2" or "This page is served from Virtual Machine 1"
Just switch your browser, access the same URL again and again from different browsers or after clearing browser cache, randomly you will either see a page from VM1 or may be from VM2.
So, Windows Azure did the Magical job of Load Balancing your websites. Some times, the request is sent to VM1 and some times to VM2 sharing the load.
However, this is just for illustration. In real scenarios, you will deploy the same code in both VM's. If you are even smart, you will deploy your code on 1 VM and clone/copy it as second VM.
There are about 20 screenshots in the article making it really big. But the concept is too small and simple. Isn't it?