Monday, August 11, 2014

Creating Cloud Service

Today we are going to create windows Azure cloud service. Not like my previous post about creating Azure mobile service today i'm going to totally working with visual studio and not going to the azure portal

before start ,
  1. you need to have Azure subscription
  2. Visual Studio ( http://www.visualstudio.com/ )
  3. Azure SDK (http://azure.microsoft.com/en-us/downloads/) -.NET

Unlike most of other cloud service providers azure provide SDK's for developers not only for .NET it supports java, Node.js , php, Python and Ruby . more will come later . Today im demonstrate codes with .NET SDK.

If you are installed and ready lets begin our task

First open Server explorer and if you configured your cloud SDK correctly it will display  Windows Azure menu. click on it and login to your Azure subscription

1. Open Visual Studio -> New -> Cloud Service  and give name to solution















2. Then Visual Studio will prompt you New Windows Azure Cloud Service Prompt.
Today I'm going to host website as a cloud service therefore I need to add WebRole (ASP .NET Web role ) to my cloud service.

 I select it and then rename it using edit button in the Azure cloud service solution list as PrabathslWebRole














**I'll explain what is web role in azure cloud service in late in this post.

3. Then it will prompt New ASP.NET web project window it is familiar one to all ASP.NET developers. I'm selecting MVC project to work on. You can create any site as your wish.


















My project is now created and you can see that in solution explorer there is two separate projects in the same solution.













One is our cloud service and next one is our ASP.NET MVC project. You can working on ASP.NET project as same as you previously worked with ASP.NET. I'll explain some additional things in regarding cloud service project


If you expand Cloud service project you can see that in roles folder there is my WebRole











What is WebRole ? 
 In Windows Azure it introduced concepts called Roles. There is two main roles in azure 
  1. Web Role
  2. Worker Role
 Actually each and every role will represent Virtual Machine (VM)  in Azure IaaS (Infranstructure As A Service). Wen we working with Cloud services it provide as PaaS (Platform As A Service) that means it automatically create required VM for us. To optimize resource consumption within VM's Azure IaaS contains predefined VM's for common tasks.

 If we working with Web Site we need IIS (Internet Information Service) within our hosted VM. Therefore Azure Web Role will contain VM that contain configured IIS wit it. If our Service not working with HTTP we don't need IIS
.Therefore It contains predefined VM without IIS it is Worker Role. (**I'll go through Worker role and its capabilities with separate post)

You can have multiple worker roles and Web roles within same Service.

If you have two worker roles within your Service it means when your application is up there is two different VM's is running on Azure fabric

let's go back to project

4. Lets run our code and see (* I made a mistake here. If i need to run application locally it need to start Azure Compute Emulator //feature with in azure SDK ad its allow to run and debus cloud applications locally// for that i need to run visual Studio as administrator)

Hopefully it is up and running.. 

Then R-Click on Azure  Emulator and go to Compute Emulator UI











Then you can see that my AzureServiceDemo Project is running and it contains PrabathSlWebRole  in VM. It has only one instance of VM. 





To gain efficiency and 99.5% up time that grantees by Azure you need to have at least two instances of each VM (in this case web role) 

to do that go back to the project and open PrabathSlWebRole .  In select configuration and make instance count at least 2 . and you can select VM sizes for each instance according to your requirement and traffic that expecting from clients. in my demo i don't need to maintain lot more traffic  and i select Small












Azure Load Balance

When we are working with two separate instance instance of VM's they have separate IP's as well. how we utilize it ? 

Answer is azure do every thing for you . Azure load balancer get the external (pubic) traffic and redirect it in to available VM's automatically according to resource availble and round robin like scheduling algorithms . 

to monitor that lets change our code slightly.  

Go to HomeController.cs and 

edit About() as follows

public ActionResult About()
       {
           //Lets add current instance Id to this message
           //need to reference using Microsoft.WindowsAzure.ServiceRuntime;
           ViewBag.Message = "current instance Id = " + RoleEnvironment.CurrentRoleInstance.Id.ToString() + "  Role " + RoleEnvironment.CurrentRoleInstance.Role.ToString();
           return View();
       }


Then run it and see the results .. click the about and check the value with several refreshes.. isn't it amazing :)  

And here Compute Emulator ,









Now it runs two instances of same PrabathSlWebRole. 


With in hosted environment these VM's are represent in separate Windows Server 2012 R2  VM's.

Lets meet with another post.


0 comments: