Categories
Cloud

Azure Virtual Machines

Virtual Machines (VMs) are software emulations of physical computers hosted on Microsoft infrastructure, including virtual processors, memory, storage, and networking.

Virtual machines are Infrastructure as a Service that provides you complete control over operating systems running on virtualized servers. They can be deployed and managed in three ways:

  • The Azure portal
  • A script using Azure CLI or Powershell
  • Azure Resource Manager templates

Below you will learn how to create an Azure virtual machine and the different steps involved.

QUICK START

In the Azure portal, create a virtual machine by selecting Create a resource, select Compute, and choose your image. You can also search for Virtual machines and click +Add.

Azure Virtual Machines Cheatsheet

Azure VM Images

Virtual machine images are files that contain a preconfigured operating system that can be deployed to a virtual machine.

You can download images of preconfigured OSs on the Azure marketplace or create one from scratch. There is a large selection of images including Windows and various Linux distributions.

Specialized virtual images can be used to restore a virtual machine with its accounts, databases, settings, and other data intact.

To create an image in the Azure portal, go to the page for the virtual machine and select Capture. You will have to create a new VM from the image.

You can also build your own images from scratch with Microsoft Hyper-V and upload them to Azure. To generalize your images from scratch use Sysprep for Windows or waagnet for Linux.

Azure VM Sizes

Virtual machines are assigned virtual processors, memory, and storage according to their size and incur different levels of pricing.

Select a VM size depending on your use case:

Use CaseSize
General computing or web, low cost, low workloadsB-Series
General purpose, enterprise applicationsD-Series
General purpose, enterprise applications, Premium SSDDs-Series
General purpose, enterprise applications, ConfidentialDC-Series
Memory intensive, high RAME-Series
Computational intensive, high CPUF-Series
Memory and storage optimized databasesG-Series
High Performance ComputingH-Series
Storage optimized databasesL-Series
Heavy graphics, GPU optimizedN-Series
Learn more

Azure VM Storage

There are 4 types of storage available for virtual machines:

  • Standard HDD
  • Standard SSD
  • Premium SSD
  • Ultra Disk
Max Size (GB)Max Throughput (MB/s)Max IOPS
Standard HDD32,7675002,000
Standard SSD32,7677506,000
Premium SSD32,76790020,000
Ultra Disk65,5362,000160,000

IOPS: Input/output Operations Per Second that measures amount of read and write instructions per second.

Throughput or bandwidth: measures amount of data applications can send or receive from the disk per second.

IOPS x I/O size = throughput

Latency: amount of time for an application to send request to disk and get response. Measures how long it takes to process a request.

Standard HDDs offer storage for a lower price but are much slower. Standard SSD disks are best for normal workloads with better performance. Premium SSD disks are for I/O intensive workloads or mission-critical systems that need to process data very quickly. Ultra disks are the highest disk performance available.

Disks can also be managed or unmanaged.

Managed diskUnmanaged disk
Azure Backup supportSelf managed storage accounts
Better security: role-based access control (RBAC)Maximum 20,000 I/O per storage account and 40 VHDs
Better scalability: Azure auto manages disk and storage accounts
Better reliability: disk managed by Azure

Azure strongly recommends managed disks and may discontinue unmanaged disks in the future.

Virtual Hard Disks (VHDs) are similar to physical hard disks except it stores virtual files for the operating system and hosts the virtual machine.

By default, two VHDs will be created for your virtual machine:

  1. The Operating System disk. This is your primary or C: drive and has a maximum capacity of 2048 GB.
    • Linux OS size is often around 30 GB, Windows about 127 GB
  2. A Temporary disk. This provides temporary storage for the OS or any apps. It is configured as the D: drive by default and is sized based on the VM size, making it an ideal location for the Windows paging file.

Create a Data disk for data storage outside the OS and temporary disk. Each data disk can hold up to 32,767 gibibytes (GiB) of data, with the maximum amount of storage determined by the VM size you select.

You will have to initialize and format disks after you attach them, similar to physical drives:

  1. Launch disk management from the VM’s Start menu or type diskmgmt.msc into the search bar and press enter.
  2. Verify the uninitialized disk and press OK to initialize it.
  3. Right click the disk and select New simple volume to format it. Continue through the wizard.
Automate this

Automate the initializing and formatting of drives according to your OS:

Windows (Powershell):

$disks = Get-Disk | Where partitionstyle -eq 'raw' | sort number

    $letters = 70..89 | ForEach-Object { [char]$_ }
    $count = 0
    $labels = "data1","data2"

    foreach ($disk in $disks) {
        $driveLetter = $letters[$count].ToString()
        $disk | 
        Initialize-Disk -PartitionStyle MBR -PassThru |
        New-Partition -UseMaximumSize -DriveLetter $driveLetter |
        Format-Volume -FileSystem NTFS -NewFileSystemLabel $labels[$count] -Confirm:$false -Force
	$count++
    }

Linux (Azure CLI):

az vm extension set \
 --vm-name [Vm_Name] \
 --name customScript \
 --publisher Microsoft.Azure.Extension
 --settings '{"fileUris":["https://raw.githubusercontent.com/MicrosoftDocs/mslearn-add-and-size-disks-in-azure-virtual-machines/master/add-data-disk.sh"]}' \
 --protected-settings '{"commandToExecute": "./add-data-disk.sh"}'

This uses the custom script extension to download a bash script that modifies etc/fstab. Review the full script here.

Azure also offers Ephemeral OS disks.

Ephemeral disks store OS data on local storage instead of remotely on Azure storage. It works well for stateless workloads, where applications are tolerant of individual VM failures, but are more affected by VM deployment time or reimaging of the individual VM instances. It has lower read/write latency to the OS disk and faster VM reimaging.

By default a Windows VM will have a Premium SSD, 1023 GB, and None (empty disk), managed disk.

Azure VM Networking

Each virtual machine has at least one network interface that provides IP address connectivity to the machine and sends/receives data from the network.

The network interface has network security groups applied to prevent unauthorized access to your virtual machine.

In order to connect to your virtual machine you will have to open a port:

For Windows:

  1. Under Inbound port rules, choose Allow selected ports and then select RDP (3389) and HTTP (80) from the drop-down.
  2. Once the VM is created you can view it in the Azure portal, click Connect to Download RDP file, enter in your login information, click OK, and Continue.

For Linux:

  1. Under Administrator account, select SSH public key and make sure Generate new key pair is selected. Then create a name for the Key pair name.
  2. Under Inbound port rules > Public inbound ports, choose Allow selected ports and then select SSH (22) and HTTP (80).
  3. When the Generate new key pair window opens, select Download private key and create resource. Your key file will be download as keyName.pem. Note where the file was downloaded.
  4. On the page for your new VM, select the public IP address and copy it.
  5. To connect from the console:
ssh -i .\Downloads\keyName.pem [user]@[Public IP]

To learn more about networking, read Azure Networking Basics.

Azure CLI

Create a resource group:

az group create --name [Resource_Group]--location [westus]

Create a Windows VM from image:

az vm create -n [VM_Name] -g [Resource_Group] --image Win2016Datacenter

Create a Debian Linux VM from image and generate SSH keys:

az vm create -g [Resource_Group] -n [VM_Name] --image debian --generate-ssh-keys

View all popular VM images:

az vm image list --output table

List all created VMs:

az vm list

View detailed information on a VM:

az vm show --resource-group [Resource_Group] --name [VM_Name]

Start a VM:

az vm start \
   -n [VM_Name] \
   -g [Resource_Group]

Restart a VM:

az vm restart -n MyVm -g [Resource_Group]
--no-wait

Stop a VM:

az vm stop \
   -n [VM_Name] \
   -g [Resource_Group]

Deallocate a VM:

az vm deallocate -g [Resource_Group] -n [VM_Name]

View a VM’s size:

az vm show \
   --resource-group [Resource_Group] \
   --name [VM_Name] \
   --query hardwareProfile.vmSize

Resize a VM:

az vm resize \
   --resource-group [Resource_Group] \
   --name [VM_Name] \
   --size [Standard_D2s_v3]

Attach a new disk to the VM:

az vm disk attach -g [Resource_Group] --vm-name [VM_Name] --name [Disk_Name] --size-gb [Size] --new

Detach a disk from the VM:

az vm disk detach -g [Resource_Group] --vm-name [VM_Name] --name [Disk_Name]

View a VM’s IP address information:

az vm list-ip-addresses -n [VM_name] -o table

View all VM’s IP address information:

az vm list \
   --resource-group [Resource_Group] \
   --query "[*].{Name:name, PrivateIP:privateIps, PublicIP:publicIps}" \
   --show-details \
   --output table

Open port 80:

az vm open-port \
   --port 80 \
   --resource-group [Resource_Group] \
   --name [VM_Name]

Enable boot diagnostics (monitoring):

az vm boot-diagnostics enable