Step-by-Step: Quickly Build IaaS Server Farms in the Cloud
Join the DZone community and get the full member experience.
Join For FreeLast week, I wrote an article about using the new Azure Preview Portal and new IaaS Resource Group Gallery templates to provision an entire cloud-based server farm in as little as eight mouse-clicks – complete with storage, virtual networking and multiple VM tiers.
Since publishing that article, I’ve been asked by a number of people how the same Resource Group model could be used to automate the provisioning of server farms using PowerShell. Beginning in version 0.8.0 of the Azure PowerShell modules, the Azure PowerShell installation actually installs 3 modules:
- The Azure module, for traditional Azure service management. This is what we’ve used in the past for scripting Azure environments using PowerShell.
- The AzureResourceManager module, for using Resource Groups to work with multiple related cloud resources as a grouped entity.
- The AzureProfile module, for managing and selecting Azure subscriptions.
Azure Resource Manager can greatly simplify the management of multi-VM applications by allowing the related resources to be provisioned and managed as a Resource Group, rather than as a discrete set of individual resources. In this article, we’ll step through the process for leveraging the AzureResourceManager module to script the process for quickly building a new server farm in the cloud.
Task 1: Setup your Cloud Scripting Environment
Before using the AzureResourceManager module, you’ll first need to ensure that you’re overall scripting environment is setup with an Azure subscription and the Azure PowerShell modules.
- Activate a FREE Microsoft Azure subscription, if you don’t yet have an activate subscription.
SIGN-UP: Free Microsoft Azure subscription - Download and install the latest version of the Azure PowerShell modules.
DOWNLOAD: Azure PowerShell modules
These modules are intended to work with Windows PowerShell v3.0 or later. - From within your Windows PowerShell ISE scripting environment, import and check the version of the installed Azure modules.
# Import Azure Module Import-Module "C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\ServiceManagement\Azure\Azure.psd1" # Check Azure Module version - needs to be V0.8.0 or later Get-Module -Name Azure
The version number returned from the Get-Module cmdlet above should be version 0.8.0 or later. If an earlier version is reported, go back to step 2 above and be sure to download and install the latest version of these modules. - Sign-in to your Microsoft Azure subscription.
# Authenticate to Azure Add-AzureAccount
When prompted to sign-in, enter the same username and password used when activating your Microsoft Azure subscription in Step 1 above. - Select your current Microsoft Azure subscription,
if you have more than one active subscription. You can identify the
name of each Azure subscription associated with your user account by
using the Get-AzureSubscription cmdlet.
# List active Azure subscriptions (Get-AzureSubscription).SubscriptionName # Select Azure Subscription $subscriptionName = "ENTER_YOUR_SUBSCRIPTION_NAME" Select-AzureSubscription ` -SubscriptionName $subscriptionName
- Switch to Azure Resource Manager mode. This is the mode in which you’ll be working to provision new resources using Azure Resource Groups.
# Switch mode from Azure Service Management to Azure Resource Manager Switch-AzureMode ` -Name AzureResourceManager
Your scripting environment is now prepared for provisioning resources using Azure Resource Manager.
Task 2: Selecting an Azure Resource Group Gallery Template
Generally speaking, the Azure Resource Group Gallery Templates that are currently available via the Azure Preview Portal are also available for use in PowerShell scripts that can automate the provisioning process. These templates combine storage, networking, cloud services and virtual machine resources into a single template that can be quickly provisioned. In fact, of the templates that are displayed in the Preview Portal today, the only template that’s not currently available for provisioning via PowerShell is the SharePoint Server Farm template. All of the other templates are available for use within PowerShell.
Before provisioning a new Azure Resource Group, we’ll want to select the appropriate Resource Group Gallery Template to use in our script. We can display the available Azure Resource Group Gallery templates using the Get-AzureResourceGroupGalleryTemplate cmdlet. By combining this cmdlet with the Out-GridView cmdlet, you can interactively prompt users to select a particular template from the list of all available templates.
# Select Azure Resource Group Gallery Template $templateIdentity = (Get-AzureResourceGroupGalleryTemplate | Out-GridView -OutputMode Single).IdentityPrompting users for a Resource Group Gallery Template using Out-GridView
For this article, we’ll start with the Resource Group Gallery Template for Windows Server 2012 R2, which we can also specify directly with the associated Template Identity value.
# Select Windows Server 2012 R2 Template $templateIdentity = "Microsoft.WindowsServer2012R2Datacenter.0.2.0-preview"
Task 3: Define variable values for new Resource Group
Since Resource Groups can be used to quickly provision a number of related cloud resources, such as storage, virtual networks, cloud services and virtual machines, we’ll need to specify the particular names and values to be used for each resource.
# Define unique name for new Resource Group # Replace XXX with your initials $resourceGroupName = "XXXlab01" # Specify Azure Datacenter region $location = "East US" # Define names & value for Storage and vNet $storageAccountName = $resourceGroupName + "stor" $vNetName = $resourceGroupName + "net" $vNetAddressSpace = "10.1.0.0/16" # Define VM Size to provision $vmSize = "Small" # 1 core, 1.75 GB RAM # Define Cloud Service and VM Names $cloudServiceName = $resourceGroupName + "web" $vmHostName = $resourceGroupName + "web01" # Enter local Admin user & pwd for new VMs $creds = Get-Credential ` -Message "Enter new Admin credentials for VM"
Task 4: Provision new Resource Group
Now that we’ve defined the names and values we wish to use for provisioning our related set of cloud resources, we can build a new Resource Group with just a single PowerShell cmdlet – complete with cloud storage, a virtual network, cloud service and virtual machine!
After the new Resource Group has been provisioned, you can confirm the list of resources associated with the Resource Group by using the Get-AzureResourceGroup cmdlet.# Provision new Resource GroupNew-AzureResourceGroup ` -Name $resourceGroupName ` -Location $location ` -GalleryTemplateIdentity $templateIdentity ` -newStorageAccountName $storageAccountName ` -newDomainName $cloudServiceName ` -newVirtualNetworkName $vNetName ` -vnetAddressSpace $vNetAddressSpace ` -hostName $vmHostName ` -userName $creds.UserName ` -password $creds.GetNetworkCredential().SecurePassword ` -hardwareSize $vmSize ` -locationFromTemplate $location ` -Force
# Get the resources for a Resource Group Get-AzureResourceGroup ` -Name $resourceGroupName
Task 5: Add more VM’s to a Resource Group
Now that the new Resource Group is provisioned, it’s easy to add more VM’s to the same Resource Group. Just specify the unique property values for a new VM and run the New-AzureResourceGroup cmdlet again. For example, to add a SQL Server VM to the existing Resource Group, you can use the code snippet below.After the new VM is provisioned, run the Get-AzureResourceGroup cmdlet again to see the new resources.# Add a second VM to an existing Resource Group $templateIdentity = "Microsoft.SQLServer2012SP1Enterprise.0.2.0-preview" $cloudServiceName = $resourceGroupName + "db" $vmHostName = $resourceGroupName + "db01" New-AzureResourceGroup ` -Name $resourceGroupName ` -Location $location ` -GalleryTemplateIdentity $templateIdentity ` -newStorageAccountName $storageAccountName ` -newDomainName $cloudServiceName ` -newVirtualNetworkName $vNetName ` -vnetAddressSpace $vNetAddressSpace ` -hostName $vmHostName ` -userName $creds.UserName ` -password $creds.GetNetworkCredential().SecurePassword ` -hardwareSize $vmSize ` -locationFromTemplate $location ` -Force
# Get the resources for a Resource Group Get-AzureResourceGroup ` -Name $resourceGroupName
Continue your Hybrid Cloud learning!
In this article, we’ve used the new AzureResourceManager module for PowerShell to quickly provision a multi-VM server farm environment, including storage, virtual networking and multiple VMs. In future articles, we’ll look at leveraging Azure Resource Groups from a management perspective as well. To continue your learning on Microsoft Azure and Hybrid Cloud, be sure to join our FREE Hybrid Cloud study track in our online Early Experts study group!
Published at DZone with permission of Keith Mayer, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Merge GraphQL Schemas Using Apollo Server and Koa
-
Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
-
Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations
-
Competing Consumers With Spring Boot and Hazelcast
Comments