Saturday, July 26, 2014

Azure Virtual Machine using Powershell – common operations


This is very common scenario I have observed, many times the IT guys need to perform day to day tasks of Azure with the help of Powershell command and he finds dozens of ways to do it. In this post I am providing the list of all common and basic operation of Azure Virtual Machine using Powershell. If I have missed any operation on Azure virtual machine with powershell, then feel free to comment.
To perform any task in powershell there are numerous ways, I will be showing the easiest way to achieve it. As expected this post is also going to be big one!!!
Setting up Azure Account on Powershell window
This is mandatory step. Without this setup you cannot perform a single operation using Azure powershell. There are two ways to set up your credentials in Azure Powershell. One is to use Azure publish settings files and importing in powershell or you can directly login credentials by using Add-AzureAccount. Here I am depicting the second way –
#add azure account to powershell current session
Set-ExecutionPolicy RemoteSigned
Add-AzureAccount
Select-AzureSubscription -SubscriptionName  "Your Subscription name" 

Setting up Storage Account
Azure Virtual machine disks and images are stored in Azure Storage therefore we need to have storage account. If you have an existing storage account then definitely you can use that or create new. Following commands specify how you can create a new storage account and also depicts how can you turn ON or OFF the geo replication for account. I don’t wanted to have geo replication therefore to save on cost I am making locally redundant by specifying
-GeoReplicationEnabled  $false
Here are the complete commands –
#this section is required only if you don't have storage account available, if you have one ready then use that.
#set storage account details
$location = "West Europe"
#this can be any location of your choice
$storageAccountName = "YourStorageAccountName"
#set geo replication to locally redundant. If you wish to setup to Geo redundant then set below parameter to $true
Set-AzureStorageAccount  -StorageAccountName  $storageAccountName -GeoReplicationEnabled  $false

#set newly created storage account for current subscription
Set-Azuresubscription -SubscriptionName "YourSubscriptionName" -CurrentStorageAccountname $storageAccountName

Setting up Azure cloud Service (Optional)
Following command actually create a cloud service in your subscription, if you any cloud service existing then you can use the same to deploy your vm –
#create cloud service, if already present then you can use that
$cloudServiceName = "YourCloudServiceName"
New-AzureService -ServiceName $cloudServiceName -Location $locations


Selecting latest published Azure Virtual Machine Image using powershell
First run the command –
Get-AzureVMImage
This lists down all the VM images available. I will be taking the entire article with respect to Azure virtual Machine SQL Server with powershell therefore I searched for SQL Server family as shown below –
 
I used following command to list images based on published date in descending order so that I can select the latest image available in the location and here is the result screenshot.
#retrieve the latest image of your choice based on published date
$images = Get-AzureVMImage | where { $_.ImageFamily -eq "SQL Server 2012 SP1 Enterprise on Windows Server 2012" } | where { $_.Location.Split(";") -contains $location } | Sort-Object -Descending -Property PublishedDate 

$latestImage = $images[0]
$latestImage

 
Highlighted area shows the latest version available for SQL Server as on today in West Europe region and it got selected correctly.

Set up Credentials for Azure Virtual Machine using Powershell
In this step let’s setup the credentials for Azure virtual machine. Firs declare the variables with appropriate values –
#set virtual machine variables
$vmName = "YourVMName"
$adminLogin = "YourUserName"
$adminPasswd = "YourPassword"
Create Azure virtual Machine using Powershell
Let’s first define the VM specific variables as follows –
#set virtual machine variables
$vmName = "powershellvm"#should not be more that 13 characters as of now
$instanceSize = "Basic_A1"
#Possible values - #ExtraSmall,Small,Medium,Large,ExtraLarge,A5,A6,A7,A8,A9,Basic_A0,Basic_A1,Basic_A2,Basic_A3, Basic_A4
Below this point all commands uses variables declared above. So in case you are not able to understand from where the particular variable values come, do not hesitate to look above this point. J

Sunday, July 13, 2014

Upload large files to Azure block blob storage in parallel and Async using C#

Alright guys read carefully. This blog post explains the code by which you can UPLOAD MULTIPLE LARGE FILEs TO AZURE BLOB IN PARALLEL (PRECISELY BLOCK BLOB).
What I am doing and what I am not?
1.Ask user to provide list large files and blob names to upload in one go in parallel
2.The code will use TPL (Parallel.ForEach loop precisely) to perform simultaneous uploads of azure blobs.
3.Code will chop the large files into multiple blocks.
4.Every block of file is uploading in SYNC. I am not performing any ASYNC operation while uploading individual blocks to azure blob. (Precisely I will be using PutBlock and PutBlockList methods).
5.However, the UI project (In my case it is console application, also it can be either Worker Role) calls the method of upload blob in ASYNC way with the help of BeginInvoke and EndInvoke.
Applicable technology -
I am using VS2013, Azure SDK 2.3 and Storage library as “Windows Azure Storage 4.1.0” launched on 23rd of Jun 2014.
Implementation
In real world scenario, we always tend to perform large file uploads to azure blob using worker role. Hence I will depict code with respect to console application. Don’t worry it can be easily converted to worker role specific code. J
Alright so, let’s start with it!!
Again – This might also be long post due to heavy code blocks. So be prepared.
Reference – 70% of this code is based on solution provided by codeplex on this post - http://azurelargeblobupload.codeplex.com/SourceControl/latest#AzurePutBllockExample/
It is a great solution to upload large files to azure blob storage. Just awesome!! I will change a bit so that, we can perform parallel uploads of large files.
Let’s understand few important components.

Tuesday, July 8, 2014

Transient Fault Handling with SQL Azure and Entity Framework step by step guide


Communication errors are always a pain!! When you are working with cloud platform like Azure, communication errors makes you feel the pain even more!!
Thanks that Transient Fault Handling framework exists!!
Alright guys, so here I am going demonstrate how to configure transient fault handling application block of Enterprise Library 6 (and above hopefully, if they don’t change in next versionsJ) with SQL Azure and Entity framework. This going to be a long article that is what I think. So be ready!!
What are Transient Faults or Transient Errors?
These are mainly network issues. If you perform an operation in Azure and sometimes it does not work however if you try after some time, it works well. This odd and non-predictable behavior is nothing but transient faults.
When we talk about SQL Azure, transient faults are common. The article Azure SQL Database Resource Management - http://msdn.microsoft.com/library/azure/dn338083.aspx mentions the scenarios in which you may face transient fault of SQL Azure.  I highly recommend you to go through the all this documents and related links. Very informative!!
Now, very common transient error of SQL Azure is throttling.
What is throttling?
Throttle is very common term used in computing and networking. The dictionary meaning of throttle is to kill or stop something. Throttling can be of any type like, network throttling, bandwidth throttling, and email throttling and so on.  Throttling can be sometime good and sometime bad. When it is bad for you, it is your responsibility to handle it. Throttling is good when the database server engine is experiencing extreme load and it is well above the desired load. Then throttling brings down consumption of resource and frees some of the load of database engine. This is done via blocking the connections or denying the new connections to SQL Azure database engine. It is bad when you are trying to connect to SQL Azure and throttling occurs and you get connectivity errors with SQL Azure. To understand more about SQL Azure throttling refer to the link - http://msdn.microsoft.com/en-US/library/azure/dn338079.aspx.
Solution -
The solution is simple and you are advised to implement retry logic in your application. To implement retry logic in SQL Azure and entity framework, we use Transient Fault Handling block. It is simple and recommended to always use Transient Fault Handling framework to deal with SQL Azure connectivity errors and have retry logic in application.
Implementation –
Please note that, I am using Transient Fault Handling Block version 6.0.1304. This is the one available as of today (7th July 2014). Also I am using Azure SDK 2.3 and visual stiudio 2013 for this sample.
First let’s create a server in SQL Azure and add a database in it. I created simple database named as Company and added only one table in it named as Customer with 3 columns and sample data as shown in below screenshot.