# Lab 08: Manage Azure Virtual Machines

For complete lab instructions, see the [full guide on GitHub](https://github.com/MicrosoftLearning/AZ-104-MicrosoftAzureAdministrator/blob/master/Instructions/Labs/LAB_08-Manage_Virtual_Machines.md).

**Objective:** This lab explores Azure Virtual Machines (VMs) and Virtual Machine Scale Sets (VMSS). You'll create and manage individual VMs, resize them, and then create a VMSS with autoscaling.

**Key Concepts:**

* **Availability Zones:** Physically separate locations within an Azure region, providing high availability and fault tolerance. Deploying VMs across zones protects against single-point-of-failure within a region.
    
* **VM Size (SKU):** Determines the compute resources (CPU, memory) allocated to a VM. Resizing a VM changes its SKU.
    
* **Managed Disks:** Azure-managed storage for VMs. You can change the disk type (Standard HDD, Standard SSD, Premium SSD) and size.
    
* **Virtual Machine Scale Set (VMSS):** A group of identical, load-balanced VMs that can automatically scale in or out based on demand or a schedule.
    
* **Autoscaling:** Automatically adjusting the number of VM instances in a VMSS based on metrics like CPU utilization.
    
* **Orchestration Modes**: *Uniform* (identical instances, suitable for stateless applications, the focus of this lab) and *Flexible* (allows more heterogeneity, suitable for a wider range of workloads, including stateful applications).
    

## Job skills

* Task 1: Deploy zone-resilient Azure virtual machines by using the Azure portal.
    
* Task 2: Manage compute and storage scaling for virtual machines.
    
* Task 3: Create and configure Azure Virtual Machine Scale Sets.
    
* Task 4: Scale Azure Virtual Machine Scale Sets.
    
* Task 5: Create a virtual machine using Azure PowerShell (optional 1).
    
* Task 6: Create a virtual machine using the CLI (optional 2).
    

**Task 1: Deploy Zone-Resilient VMs**

1. **Sign In:** Log in to the Azure portal ([https://portal.azure.com](https://portal.azure.com/)).
    
2. **Create VMs:** Search for and select "Virtual machines" and click "+ Create" -&gt; "Azure virtual machine."
    
    * **Basics:**
        
        * Virtual machine names:  *After* selecting both Availability Zones (next step), select **Edit Names** and use `az104-vm1` and `az104-vm2`.
            
        * Availability options: `Availability zone`
            
        * Availability zone: Check *both* `Zone 1` and `Zone 2`. This creates *two* VMs, one in each zone.
            
        * Azure Spot instance: `Unchecked`
            
        * Size: `Standard_D2s_v3`
            
        * Password: (Provide a strong password)
            
        * Public inbound ports: `None`
            
    * **Disks:**
        
        * OS disk type: `Premium SSD`
            
        * Delete with VM: `Checked` (default)
            
        * Enable Ultra Disk compatibility: `Unchecked`
            
    * **Networking:** Accept the defaults, but set Load Balancing Options to `None` and <mark>Delete public IP and NIC when VM is deleted</mark> to `Checked`.
        
    * **Management:** Patch orchestration options:  `Azure orchestrated`
        
    * **Monitoring:**Boot diagnostics: `Disable`
        
    * **Advanced:** Accept the defaults.
        
    * **Review + create:** Verify and click "Create."
        

**Task 2: Manage VM Scaling**

1. **Resize VM (Vertical Scaling):**
    
    * Go to the `az104-vm1` VM resource.
        
    * Under "Settings," select "Size."
        
    * Choose `Standard_DS1_v2` 
        
    * Click "Resize." Confirm the change if prompted.
        
2. **Manage Data Disk:**
    
    * Under "Settings," select "Disks."
        
    * Click "+ Create and attach a new disk."
        
        * Disk name: `vm1-disk1`
            
        * Storage type: `Standard HDD`
            
        * Size (GiB): `32`
            
    * Click "Save."
        
    * After creation, click the "Detach" icon (far right) for `vm1-disk1`.
        
    * Click Apply. This **detaches** the disk but keeps it in storage.
        
3. **Change Disk Type:**
    
    * Search for and select "Disks."
        
    * Select `vm1-disk1`.
        
    * Under "Settings," select "Size + performance."
        
    * Change the "Storage type" to `Standard SSD`.
        
    * Click "Save."
        
4. **Reattach Disk:**
    
    * Go back to the `az104-vm1` VM.
        
    * Select "Disks."
        
    * Under "Data disks," click "Attach existing disks."
        
    * Disk name: Select `vm1-disk1`.
        
    * Verify the disk type is now `Standard SSD`.
        
    * Click Apply.
        

**<mark>Task 3: Create Virtual Machine Scale Set</mark>**

1. **Create VMSS:** Search for and select "Virtual machine scale sets." and click "+ Create."
    
    * **Basics:**
        
        * Virtual machine scale set name: `vmss1`
            
        * Region: `(US) East US`
            
        * Availability zone: Select `Zone 1`, `Zone 2`, and `Zone 3`.
            
        * Orchestration mode: `Uniform`
            
        * Scaling Options: Review and take the defaults. We will change this in the next task.
            
        * Size: `Standard_D2s_v3`
            
    * **Disks:** Accept the defaults.
        
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1741025759337/c2c3a4f1-8718-4fb4-9a02-a9c047523776.png align="center")
    
    * **Networking:** Click "Create virtual network" (below the Virtual network textbox).
        
        * * Name: `vmss-vnet`
                
            * Address range: `10.82.0.0/20` (delete the existing default range)
                
            * Subnet name: `subnet0`
                
            * Subnet range: `10.82.0.0/24`
                
            * Click "OK."
                
        * Click the "Edit network interface" icon (pencil) to the right of the NIC entry.
            
            * NIC network security group: `Advanced`
                
            * Configure network security group: Click "Create new."
                
                * Name: `vmss1-nsg`
                    
                * Click "Add an inbound rule."
                    
                    * Source: `Any`
                        
                    * Source port ranges: `*`
                        
                    * Destination: `Any`
                        
                    * Service: `HTTP`
                        
                    * Action: `Allow`
                        
                    * Priority: `1010`
                        
                    * Name: `allow-http`
                        
                * Click "Add."
                    
                * Click "OK."
                    
            
            ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1741025558547/113c623f-6cad-405d-b230-c77b539f590f.png align="center")
            
            * Public IP address: `Enabled`
                
            * Click "OK."
                
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1741025619292/8969f924-5aeb-4f2c-a071-f91d94bb8f48.png align="center")
        
        * **Load Balancing:**
            
            * Load balancing options: `Azure load balancer`
                
            * Select a load balancer: Click **Create new**
                
                * Enter the Load Balancer Name: `vmss-lb`
                    
                * Take the defaults, and select **Create**.
                    
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1741026033375/9a14e63b-488e-4822-8c1f-066e1e832233.png align="center")
    
    * **Management:** Disable Boot Diagnostics.
        
    * **Health:** Review the defaults (no changes).
        
    * **Advanced:** Review the defaults (no changes).
        
    * **Review + create:** Verify and click "Create."
        
2. **Wait for Deployment:** This will take about 5 minutes.
    

**<mark>Task 4: Scale VMSS (Autoscaling)</mark>**

1. **Configure Autoscaling:**
    
    * Go to the `vmss1` resource.
        
    * Under Settings, choose `Availability + Scale`, and then `Scaling`.
        
    * Select `Custom autoscale`.
        
    * Scale mode: `Scale based on a metric`
        
    
    * Click "+ Add a rule."
        
        * **Scale Out Rule (Increase Instances):**
            
            * Metric source: `Current resource (vmss1)`
                
            * Metric namespace: `Virtual Machine Host`
                
            * Metric name: `Percentage CPU`
                
            * Operator: `Greater than`
                
            * Metric threshold to trigger scale action: `70`
                
            * Duration (minutes): `10`
                
            * Time grain statistic: `Average`
                
            * Operation: `Increase percent by`
                
            * Cool down (minutes): `5`
                
            * Percentage: `50`
                
            * Select Add.
                
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1741029261924/f65e78e6-1abd-4a4d-91fb-cb1c9369e29b.png align="center")
        
    * **Scale In Rule (Decrease Instances):**
        
        * Select **Add a rule**
            
        * Metric Source: `Current Resource`
            
        * Metric namespace: `Virtual Machine Host`
            
        * Metric name: `Percentage CPU`
            
        * Operator: `Less than`
            
        * Threshold: `30`
            
        * Operation: `Decrease percent by`
            
        * Percentage: `50`
            
        * Select Add.
            
        * **Instance Limits:**
            
            * Minimum: `2`
                
            * Maximum: `10`
                
            * Default: `2`
                
    * Click "Save."
        

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1741029434470/da6d88cb-fa8a-40ba-961a-de82134a4ec9.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1741029494067/b5e83fb4-6f53-4d0d-a836-3edcc3b12d25.png align="center")

2. **Monitor Instances:**
    

1. * On the `vmss1` page, select "Instances." This is where you'd monitor the number of running VM instances as the scale set scales in and out.
        

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1741029550467/fd95ff50-6571-4468-afb6-79a14374cd99.png align="center")

**Task 5: Create VM with PowerShell (Optional)**

1. **Launch Cloud Shell (PowerShell):** Click the Cloud Shell icon in the portal (or go to [https://shell.azure.com](https://shell.azure.com/)). Choose PowerShell.
    
2. **Create VM:**
    
    PowerShell
    
    ```plaintext
    New-AzVm `
    -ResourceGroupName 'az104-rg8' `
    -Name 'myPSVM' `
    -Location 'East US' `
    -Image 'Win2019Datacenter' `
    -Zone '1' `
    -Size 'Standard_D2s_v3' `
    -Credential (Get-Credential)
    ```
    
    Enter a username and password when prompted.
    
3. **Verify and Deallocate:**
    
    PowerShell
    
    ```plaintext
    Get-AzVM -ResourceGroupName 'az104-rg8' -Status
    Stop-AzVM -ResourceGroupName 'az104-rg8' -Name 'myPSVM' -Force
    Get-AzVM -ResourceGroupName 'az104-rg8' -Status
    ```
    

**Task 6: Create VM with Azure CLI (Optional)**

1. **Launch Cloud Shell (Bash):** Click the Cloud Shell icon, and choose Bash.
    
2. **Create VM:**
    
    Bash
    
    ```plaintext
    az vm create --name myCLIVM --resource-group az104-rg8 --image Ubuntu2204 --admin-username localadmin --generate-ssh-keys
    ```
    
3. **Verify and Deallocate:**
    
    Bash
    
    ```plaintext
    az vm show --name myCLIVM --resource-group az104-rg8 --show-details
    az vm deallocate --resource-group az104-rg8 --name myCLIVM
    az vm show --name myCLIVM --resource-group az104-rg8 --show-details
    ```
    

For complete lab instructions, see the [full guide on GitHub](https://github.com/MicrosoftLearning/AZ-104-MicrosoftAzureAdministrator/blob/master/Instructions/Labs/LAB_08-Manage_Virtual_Machines.md).
