Tuesday, April 21, 2015

Managing your Azure infrastructure as code - part 1 (VM configuration)

Azure PowerShell is a powerful scripting environment that you can use to control and automate the deployment and management of your workloads in Azure. Using Azure PowerShell cmdlet architects and developers can automate every manual and repeated tasks on the Azure infrastructure. Nearly everything you can do manually in the Azure management portal can be done using Azure PowerShell in the Microsoft Azure Automation service. In this blog, we'll see how you can leverage the power of Azure PowerShell cmdlets to create Azure virtual machines with the platform images. To start with, the first step is to create a Azure virtual machine configuration object. The configuration object can be created using the New-AzureVMConfig cmdlets.

This object can then be used to perform a new deployment, as well as to add a new virtual machine to an existing deployment. To create a new virtual machine configuration, we’ll need a name for the virtual machine. We'll start the configuration object by specifying some initial characteristics. Later we'll modify the configuration object with the supporting cmdlets to create a VM. To start with, we'll need a valid name for the VM, the size for the virtual machine and the name of the image to use.
To list all the available worker role sizes you can use the cmdlets Get-AzureRoleSize as given below.

Get-AzureRoleSize | where { $_.SupportedByVirtualMachines -eq $true } | select instancesize

Choose a size from the listed options to use in the configuration.
Next, you’ll need a virtual image name, from the available options. You can use the cmdlet Get-AzureVMImage| Select imagename to list out the image names.
Using this information, you can create an initial configuration object like.

$vmName = “[YOUR_VM_NAME]”
$image = “[YOUR_VM_IMAGE_NAME]”
$azureVMConfig = New-AzureVMConfig -Name $vmName -InstanceSize ExtraSmall -ImageName $image

Next we'll use the Add-AzureProvisioningConfig cmdlet to add a provisioning configuration to the virtual machine. We'll use the Add-AzureProvisioningConfig cmdlet to modify the Azure VM configuration by setting the properties like admin credentials, data disk, endpoints, load balanced set etc. For e.g creating a Windows server, you can extend the command like.

$admin = "[ADMIN_USERNAME]"
$password = "[PASSWORD]"
$azureVM = New-AzureVMConfig -Name $vmName `
-InstanceSize ExtraSmall `
-ImageName $image `
| Add-AzureProvisioningConfig -Windows `
-AdminUsername $admin `
-Password $password

Next, we'll add a new data disk to the virtual machine object. The Add-AzureDataDisk cmdlet can be used to add a new data disk to a virtual machine object. We'll use the CreateNew parameter to create a new data disk with a specified size and label, and then attach it to the configuration. For e.g the below cmdlet Add-AzureDataDisk -CreateNew -DiskSizeInGB 200 -DiskLabel "datadisk1" -LUN 0 will create a new disk with name datadisk1 with an initial size of 200 GB with the logical unit number 0 in the data drive in the machine. We’ll use this details to extend our command like
$azureVM = New-AzureVMConfig -Name $vmName `
-InstanceSize ExtraSmall `
-ImageName $image | `
Add-AzureProvisioningConfig -Windows `
-AdminUsername $admin `
-Password $password | `
Add-AzureDataDisk -CreateNew `
-DiskSizeInGB 200 `
-DiskLabel "datadisk1" `
-LUN 0

Next we'll add a network endpoint to the virtual machine configuration using the Add-AzureEndpoint cmdlet. Endpoints in Windows Azure consist of a protocol, along with a public and private port. The private port is the port that the service is listening on the local computer. The public port is the port that the service is listening on externally. In some cases this is the same port, which is the case for PowerShell. The Add-AzureEndpoint cmdlet is used to add a new endpoint to an azure virtual machine or configuration. While adding an endpoint you can specify a new endpoint that is not load balanced, which will be used only by this virtual machine, or you can specify a load-balanced endpoint, which other virtual machines running in the same cloud service can share to load-balance traffic. You can also specify parameters for an endpoint probe. The probe will be used to check the health of endpoints and endpoint resources, probing for response every 15 seconds, and taking a resource out of rotation if no response is received within 31 seconds. For e.g the command Add-AzureEndpoint -Name "HTTP" -Protocol TCP -LocalPort 80 -PublicPort 80 -LBSetName "HttpLoadbalancedSet" –DefaultProbe will add an endpoint with name HTTP listening on public and private port 80 with default probe option. Let’s extend our command with this option too.

$azureVM = New-AzureVMConfig -Name $vmName `
-InstanceSize ExtraSmall `
-ImageName $image | `
Add-AzureProvisioningConfig -Windows `
-AdminUsername $admin `
-Password $password | `
Add-AzureDataDisk -CreateNew `
-DiskSizeInGB 200 `
-DiskLabel "datadisk1" `
-LUN 0 | `
Add-AzureEndpoint -Name "HTTP" `
-Protocol TCP `
-LocalPort 80 `
-PublicPort 80 -LBSetName "HttpLoadbalancedSet" `
–DefaultProbe

If you want to add more endpoints you can pipe more endpoints to the configurations based on your need. For e.g Add-AzureEndpoint -Protocol TCP -LocalPort 443 -PublicPort 443 -Name 'LBHTTPS' -LBSetName 'LBHTTPS' -ProbePort 443 -ProbeProtocol https -ProbePath '/'

Using this information, you can now create your VM by using a unique cloud service name like.

New-AzureVM -ServiceName [YOUR_CLOUD_SERVICE_NAME] -VMs $azureVM

No comments: