Image and Replicate Your Virtual Machine (Azure)

This is step 6 of 7 of Hands-On Project 1 for Azure. In this step, you learn how to create an image of a fully-loaded virtual machine and create new virtual machines from the image, with pre-installed software. With these skills in hand, you can also confidently delete entire virtual machines and re-create them to save on usage costs.

Back to Project

To get an overview of the process of imaging and replicating an Azure virtual machine, let’s begin with a short 7 minute tutorial:

About Azure VM Images

Within Azure, there are several options for creating virtual machine images. We will be creating and using an Image Gallery. An Image Gallery is a location where VM images can be stored and replicated across Azure regions.

When using an Image Gallery, there are two types of images allowed: generalized and specialized images. A generalized image acts as a template for creating new virtual machines. Before a generalized image is created, you must run commands on the VM to generalize it. This strips off machine-specific configurations and generally results in the original VM becoming unusable. The software installed on the original machine is however retained on a generalized image. When you create new virtual machines from a generalized image, you must provide all of the information needed to create a unique VM, such as the SSH key pair, main login account, and so on. We will not be creating a generalized image in this exercise.

With a specialized image, you are effectively creating a “golden version” of your original system. All of the machine-specific information is retained, including user accounts, key pair, and of course all of the software that was installed on the original VM. Retaining the machine-specific information is consistent with the idea of deleting the original VM and then being able to re-create it with everything intact. When you create a new virtual machine from a specialized image, everything is already there and you can take up where you left off. For this exercise, we will be creating a specialized image.

Create a Specialized Image of an Existing Virtual Machine

Assuming this virtual machine is the one you created for the earlier steps, it will have software on it that you installed, such as the net-tools package, the python-is-python3 package, and the apache2 package. It will also have a user with the name you provided as the management account. All of this will be included in the specialized image when we create it.

To create a specialized image, perform the following steps:

  1. From the Azure Portal, complete the following steps:
  2. Navigate: Portal Home –> Virtual Machines –> select virtual machine –> Capture
  3. For Share image to Azure compute gallery, leave the default to share the image to a gallery.
  4. For Target Azure compute gallery, select New Gallery, enter a gallery name, and click OK.
  5. For Operating System state, select Specialized.
  6. For Target VM image definition, click Create New, enter an image definition name, and click OK. The image definition name is the name you will use to identify the image when you are creating a new VM from the image.
  7. Leave the remaining options as defaults and click OK.
  8. For Version number, enter a version number for the image, such as 0.0.1.
  9. Allow the remaining options to default, and click Review and Create.
  10. After reviewing the specifications, click Create. This will start the image creation process. By default, this will stop the original virtual machine, so if you are connected, you will be disconnected. Stopping the original virtual machine is necessary to guarantee that all uncommitted updates are written out and that the file system is in a consistent state when the image is created.
  11. The image creation process will take a while. Azure will notify you when the process is complete.

Create a New Virtual Machine from the Saved Image

Once the image creation process is complete, you can create a new virtual machine from it. This will have the effect of cloning your original instance in the exact state it was in when you created the image.

To create a new virtual machine from the saved image, perform the following steps:

  1. From the Azure Portal, complete the following steps:
  2. Navigate: Portal Home –> Search for ‘gallery’ –> select Azure Compute Galleries –> select your gallery –> select your image –> + Create VM
  3. Enter a virtual machine name.
  4. Scroll to SSH public key source and select Use existing key. You will not be able to paste in a public key – it will use the public key that is in the saved image.
  5. Click Review and Create, then Create. This will start the deployment process. When deployment is complete, the cloned VM will be running.
  6. Connect to the new instance with Bitvise SSH client. The private key to access the new instance should already be stored as one of your Bitvise client keys.

Once logged in, you can confirm whether the software you installed is there by running a few commands from the terminal window, such as:

sudo systemctl status apache2
python -V

These commands should work the same way they do on the original instance.