Jaa


Step-by-Step: Templating VMs in the Cloud with Windows Azure and PowerShell - 31 Days of Servers in the Cloud - Part 17 of 31

In previous articles in our 31 Days of Servers in the Cloud series, we’ve stepped through the process of building out various lab environments in the cloud, such as Windows Server 2012, SQL Server 2012, SharePoint 2013, Windows 8 App Dev and even Linux,  on the Windows Azure cloud platform.

Note: If you’ve missed some of the articles in this series, you can catch up with the entire series at 31 Days of Servers in the Cloud – Getting Started.

As you begin provisioning these virtual machine environments, I’ve received a number inquiries from IT Pros on how to create your own template VMs with Windows Azure so that you can quickly deploy, de-provision and re-provision custom lab environments on-the-fly.  In this article, I’ll step through this process for both Windows Server VMs and Linux VMs by using Windows Azure and PowerShell together.

Note: Service Pack 1 for System Center 2012 also introduces the ability to manage virtual machine and cloud service templates for Private Clouds and Windows Azure from a single enterprise management toolset.  If you are interested in enterprise management for Windows Azure virtual machines, you can download Service Pack 1 for free to begin exploring these capabilities.
 
DO IT: Download Service Pack 1 for System Center 2012 for enterprise management of Windows Azure and private cloud environments.

Prerequisites

The following is required to complete this step-by-step guide:

  • A Windows Azure subscription with the Virtual Machines Preview enabled.
     
    DO IT: Sign up for a FREE Trial of Windows Azure
     
    NOTE: When activating your FREE Trial for Windows Azure, you will be prompted for credit card information.  This information is used only to validate your identity and your credit card will not be charged, unless you explicitly convert your FREE Trial account to a paid subscription at a later point in time. 
     
  • Completion of the Getting Started tasks in the following article:
     
    DO IT: Getting Started with Servers in the Cloud

Creating Template VMs in the Cloud with Windows Azure

To create and use your own template VMs in the cloud with Windows Azure, we’ll be working through the following exercises in this guide:

  • Exercise 1: Provision a new Base VM in Windows Azure
  • Exercise 2: Customize, Prepare and Capture the base VM as an Image
  • Exercise 3: Deploy a new VM from an Image
  • Exercise 4: Automating Lab Provisioning with PowerShell

As we work through these exercises, I’ll provide screenshots of the steps that I use so that you can easily follow along.  Feel free to leave any questions that you may encounter in the comments section at the end of this article.

Exercise 1: Provision a new Base VM in Windows Azure

In this exercise, we’ll be creating a new base virtual machine in the Windows Azure cloud that we’ll later use to capture a new virtual machine template image.

  1. Sign in at the Windows Azure Management Portal with the logon credentials used when you signed up for your Free 90-Day Windows Azure Trial.
     
  2. Select Virtual Machines located on the side navigation panel on the Windows Azure Management Portal page.
     
    image
    Virtual Machines page in Windows Azure Management Portal
     
  3. Click the +NEW button located on the bottom navigation bar and select Compute | Virtual Machines | From Gallery.
     
    image
    Creating a New Virtual Machine From Gallery
     
  4. In the Virtual Machine Operating System Selection list, select one of the base platform images and click the Next button. In this step-by-step guide, I’ll be selecting Windows Server 2012, December 2012 as my platform image, but the steps in this guide will work well with any of the base operating system images.
     
    image
    Virtual Machine Operating System Selection
     
    Note: Do not select a platform image that is already pre-installed with an application, such as SQL Server or BizTalk server, as these platform images have already been customized with installed applications.  To prepare your own customized image that include an application, select one of the base operating system images and install the application during Exercise 2 in this step-by-step guide..
  5. On the Virtual Machine Configuration page, complete the fields as follows:
     
    - Virtual Machine Name: XXXbasevm01 ( where XXX represents your initials ) 
     
    - New Password and Confirm Password fields: Choose and confirm a new local Administrator password.
     
    - Size: Small (1 core, 1.75GB Memory) 
     
    image
    Virtual Machine Configuration
     
    Click the Next button to continue.
     
    Note: It is suggested to use secure passwords for Administrator users and service accounts, as Windows Azure virtual machines could be accessible from the Internet knowing just their DNS.  You can also read this document on the Microsoft Security website that will help you select a secure password: https://www.microsoft.com/security/online-privacy/passwords-create.aspx.
     
  6. On the Virtual Machine Mode page, complete the fields as follows:
     
    - Standalone Virtual Machine: Selected
     
    - DNS Name: XXXbasevm01.cloudapp.net
     
    - Storage Account: Select the Storage Account defined in the Getting Started steps from the Prerequisites section above.
     
    - Region/Affinity Group/Virtual Network: Select the Affinity Group defined in the Getting Started steps from the Prerequisites section above. 
     
    image
    Virtual Machine Mode
     
    Click the Next button to continue.
     
  7. On the Virtual Machine Options page, click the Checkmark button to begin provisioning the new virtual machine. 
     
    image
    Provisioning a New Base Virtual Machine
     
    As the new virtual machine is being provisioned, you will see the Status column on the Virtual Machines page of the Windows Azure Management Portal cycle through several values including Stopped, Stopped (Provisioning), and Running (Provisioning)
     
    When provisioning for this new Virtual Machine is completed, the Status column will display a value of Running and you may continue with the next exercise in this guide.

Exercise 2: Customize, Prepare and Capture the base VM as an Image

In this exercise, we’ll be customizing our new base VM with any configuration changes or additional software that we intend to save as part of our image.  Then, we’ll prepare our base VM for imaging and capture it as new VM image that will appear in the My Images list of the Virtual Machine Gallery when creating new VMs.

  1. After the new virtual machine has finished provisioning, click on the name ( XXXbasevm01 ) of the new Virtual Machine displayed on the Virtual Machines page of the Windows Azure Management Portal.  This will display the Virtual Machine Details Page for XXXbasevm01.     
     
    image
    Virtual Machine Details Page
     

  2. On the Virtual Machines Details Page for XXXbasevm01, make a note of the DNS hostname listed for this VM on the right-side of the page.  This is the hostname we’ll connect to for customizing and preparing this base VM for imaging. 

    If XXXbasevm01 was created from a Windows Server platform image, connect to this virtual machine by clicking the Connect button located on the bottom navigation toolbar and click the Open button to launch a Remote Desktop Connection to the console of this virtual machine. 
     
    image
    Launching a Remote Desktop Connection
     
    If XXXbasevm01 was created from a Linux platform image, connect to this virtual machine by using a Secure Shell (SSH) client, such as Putty, to establish a new SSH session to the DNS hostname for the base virtual machine recorded above. 
     

  3. Login to the base VM remotely via either the Remote Desktop session or SSH session.  Customize this virtual machine with additional configuration settings and software packages that you wish to include in the image that will be captured later in this exercise.
     
    Note: Specific guidance for preparing SQL Server VMs and SharePoint VMs for imaging is also available.  Some products, such as Exchange, are not compatible with imaging tools due to changes made during the application installation process, so be sure to confirm compatibility with imaging for any applications that are being installed as part of this step.
     

  4. After completing the customizations to be included in the image, prepare the base VM for imaging.
     
    If XXXbasevm01 was created from a Windows Server platform image, use the System Preparation Tool (Sysprep) to prepare the base VM for imaging.  Run sysprep by executing the following commands:
     
       cd %windir%\system32\sysprep
    sysprep /generalize /shutdown /oobe
     
    If XXXbasevm01 was created from a Linux platform image, use the Windows Azure Linux Agent.  Prepare the virtual machine for imaging by executing the following commands:
     
       sudo su -
    waagent –force –deprovision
    export HISTSIZE=0
    shutdown –h now

    Wait for the virtual machine status in the Windows Azure Management Portal to show as Stopped and then continue with the next step.  
     

  5. After the base VM is reported on the Windows Azure Management Portal with a status of Stopped, capture the base VM to a new image by clicking the Capture button located on the bottom toolbar on the Virtual Machine Details page for XXXbasevm01.
     
    image
    Capturing a New VM Image
     
    Note: When capturing a VM to an image, the original VM that is captured will be deleted from your Windows Azure subscription account.  The original VM is replaced by the newly captured image.
     

  6. In the Capture an image from a virtual machine dialog box, specify a name for the new image and check the checkbox option for I have run Sysprep on this virtual machine.
     
    image
    Capture an Image from a Virtual Machine.
     
    Note: If you are capturing an image from a base LinuxVM, you should instead check the checkbox option for I have run the de-provision command on the Virtual Machine.
    Click the Checkmark button to capture the new Image.  After this process has completed, the new Image will be listed on the Virtual Machines | Images page with a green checkmark displayed to the left of it, and you may continue with the next exercise.
     
    image
    New Image Capture Completed

Exercise 3: Deploy a new VM from an Image

In this exercise, you will deploy a new instance of a virtual machine using the virtual machine image captured in the previous exercise.  As you will see, this process is very similar to the steps used in Exercise 1 to create the original base VM.

  1. Sign in at the Windows Azure Management Portal with the logon credentials used when you signed up for your Free 90-Day Windows Azure Trial.
     
  2. Select Virtual Machines located on the side navigation panel on the Windows Azure Management Portal page.
     
    image
    Virtual Machines page in Windows Azure Management Portal
     
  3. Click the +NEW button located on the bottom navigation bar and select Compute | Virtual Machines | From Gallery.
     
    image
    Creating a New Virtual Machine From Gallery
     
  4. In the Virtual Machine Operating System Selection list, select My Images and then select the image captured in the previous exercise.  Click the Next button. 
      
        image
    Creating a VM from a Custom Image
     
  5. On the Virtual Machine Configuration page, complete the fields as follows:
     
    - Virtual Machine Name: XXXlabvm01 ( where XXX represents your initials ) 
     
    - New Password and Confirm Password fields: Choose and confirm a new local Administrator password.
     
    - Size: Small (1 core, 1.75GB Memory) 
      
    image    
    Virtual Machine Configuration
     
    Click the Next button to continue.      
     
  6. On the Virtual Machine Mode page, complete the fields as follows:
     
    - Standalone Virtual Machine: Selected
     
    - DNS Name: XXXlabvm01.cloudapp.net
       
    - Region/Affinity Group/Virtual Network: Select the Affinity Group defined in the Getting Started steps from the Prerequisites section above. 
      
     image   
    Virtual Machine Mode
     
    Note: Unlike the original process used in Exercise 1 to create the base VM, you are not prompted for a Storage Account when creating a new VM from a custom image.  This is because this new VM will use the same storage account as the custom image from which it is created.
        
    Click the Next button to continue.
     
  7. On the Virtual Machine Options page, click the Checkmark button to begin provisioning the new virtual machine. 
      
    image    
    Provisioning a New Virtual Machine from a Custom Image
         
    As the new virtual machine is being provisioned, you will see the Status column on the Virtual Machines page of the Windows Azure Management Portal cycle through several values including Stopped, Stopped (Provisioning), and Running (Provisioning)
     
    When provisioning for this new Virtual Machine is completed, the Status column will display a value of Running and you may continue with the next exercise in this guide.

Exercise 4: Automating Lab Provisioning with PowerShell

Our VM template image is now functional for spinning up new lab VMs, but if you’re like me, you may need to spin up several VMs at the same time from a common image.  In addition, you may not be using these lab VMs 24x7 around-the-clock.  As long as a virtual machine is provisioned, it will continue to accumulate compute hours against your Free 90-Day Windows Azure Trial account regardless of virtual machine state – even in a shutdown state!

We can leverage the Windows Azure PowerShell module to automate these tasks.  In this exercise, we’ll step through using Windows PowerShell to automate:

  • Deploying new VMs from a common image
  • De-provisioning lab virtual machines when not in use
  • Re-provisioning lab virtual machines when needed again. 

Once you’ve configured the PowerShell snippets below, you’ll be able to spin up your cloud-based lab environment when needed in just a few minutes!

Note: Prior to beginning this exercise, please ensure that you’ve downloaded, installed and configured the Windows Azure PowerShell module as outlined in the Getting Started article listed in the Prerequisite section of this step-by-step guide.  For a step-by-step walkthrough of configuring PowerShell support for Azure, see Setting Up Management by Brian Lewis, one of my peer IT Pro Technical Evangelists.

  1. Deploy new VMs.   Use the New-AzureQuickVM cmdlet in the PowerShell snippet below to automate the process we performed in Exercise 3 for deploying a new VM from an image. 
     

    # Specify a name for the new VM
    $myVM = "XXXlabvm02"

    # Set the Affinity Group in which the new VM will run
    $myAffinityGroup = "XXXlab01"

    # Specify the Image from which to build the new VM
    $myImage = Get-AzureVMImage XXXimage01 # Specify the Administrator password to provision in the new VM
    $myPwd = “P@ssw0rd”

    # Deploy a new Windows VM using the parameter values specified above.
    New-AzureQuickVM -Windows -name $myVM -ImageName $myImage.ImageName -ServiceName $myVM -AffinityGroup $myAffinityGroup -Password $myPwd

    # Deploy a new Linux VM using the parameter values specified above.
    New-AzureQuickVM –Linux -name $myVM -ImageName $myImage.ImageName -ServiceName $myVM -AffinityGroup $myAffinityGroup –LinuxUser “azureuser” -Password $myPwd

     

  2. De-provision the lab. Use the Stop-AzureVM and Export-AzureVM cmdlets in the PowerShell snippet below to shutdown and export lab VMs when they are not being used.  
     

    # Specify the Name of the VM to Export
    $myVM = "XXXlabvm02"

    # Stop the VM prior to exporting it
    Stop-AzureVM -ServiceName $myVM -Name $myVM


    # Set the Export folder path for the VM configuration file. Make sure this folder exists!
    $ExportPath = "C:\ExportVMs\ExportAzureVM-$myVM.xml"

    # Export the VM to a file
    Export-AzureVM -ServiceName $myVM -name $myVM -Path $ExportPath

    # After you've confirmed that the Export file exists, delete the VM
    Remove-AzureVM -ServiceName $myVM -name $myVM

     

  3. Re-provision the lab. Use the Import-AzureVM and Start-AzureVM cmdlets in the PowerShell snippet below to import and start lab VMs when needed again.
     

    # Specify the Name of the VM to Import $myVM = “XXXlabvm02" # Specify the Import Path of the VM’s exported configuration file.
    $ImportPath = "C:\ExportVMs\ExportAzureVM-$myVM.xml"

    # Import the VM to Windows Azure
    Import-AzureVM -Path $ImportPath | New-AzureVM -ServiceName $myVM

    # Start the VM
    Start-AzureVM -ServiceName $myVM -name $myVM

What’s next? Keep learning!

To learn more about Windows Azure and Windows Server 2012, be sure to check out the additional resources below!

How are you using Windows Azure?

Do you have a unique or interesting scenario for leveraging the Windows Azure cloud platform?  If so, be sure to share with us in the Comments section below.

Keith

Comments

  • Anonymous
    May 14, 2014
    Pingback from Why new image is available from Portal, but not from SDK listing? - Windows Azure Blog