Introduction
Azure Virtual Machines (VMs) are one of the core services provided by Microsoft Azure. They allow you to create and manage virtualized computing environments in the cloud. This module will cover the basics of Azure VMs, including their setup, management, and common use cases.
Key Concepts
What is an Azure Virtual Machine?
- Definition: An Azure VM is an on-demand, scalable computing resource that provides the flexibility of virtualization without the need to buy and maintain the physical hardware.
- Use Cases: Development and testing, running applications, extending your data center, disaster recovery, etc.
Benefits of Using Azure VMs
- Scalability: Easily scale up or down based on your needs.
- Cost-Effective: Pay only for what you use.
- Flexibility: Choose from a wide range of VM sizes and configurations.
- Global Reach: Deploy VMs in data centers around the world.
Setting Up an Azure Virtual Machine
Step-by-Step Guide
-
Log in to Azure Portal
- Navigate to Azure Portal and log in with your Azure account.
-
Create a New Virtual Machine
- Click on "Create a resource" in the left-hand menu.
- Select "Compute" and then "Virtual Machine".
-
Configure Basic Settings
- Subscription: Choose your Azure subscription.
- Resource Group: Create a new resource group or select an existing one.
- VM Name: Enter a name for your VM.
- Region: Select the region where you want to deploy the VM.
- Availability Options: Choose availability set or availability zone if needed.
- Image: Select the operating system image (e.g., Windows Server, Ubuntu).
- Size: Choose the VM size based on your requirements.
-
Configure Administrator Account
- Username: Enter a username for the VM.
- Password: Enter a strong password or use an SSH public key for Linux VMs.
-
Configure Networking
- Virtual Network: Select an existing virtual network or create a new one.
- Subnet: Choose a subnet within the virtual network.
- Public IP: Assign a public IP address if needed.
- Network Security Group (NSG): Configure inbound and outbound rules.
-
Configure Management Options
- Enable or disable monitoring, backup, and other management options as needed.
-
Review and Create
- Review all the settings and click "Create" to deploy the VM.
Example Code: Creating a VM using Azure CLI
# Create a resource group az group create --name myResourceGroup --location eastus # Create a virtual machine az vm create \ --resource-group myResourceGroup \ --name myVM \ --image UbuntuLTS \ --admin-username azureuser \ --generate-ssh-keys
Managing Azure Virtual Machines
Starting and Stopping VMs
- Start a VM: You can start a VM from the Azure Portal or using the Azure CLI.
az vm start --resource-group myResourceGroup --name myVM
- Stop a VM: Similarly, you can stop a VM.
az vm stop --resource-group myResourceGroup --name myVM
Monitoring and Diagnostics
- Azure Monitor: Use Azure Monitor to track the performance and health of your VMs.
- Diagnostics Extension: Enable the diagnostics extension to collect detailed metrics and logs.
Scaling VMs
- Vertical Scaling: Change the size of the VM to a larger or smaller instance.
- Horizontal Scaling: Add more VMs to a load balancer to distribute the load.
Practical Exercises
Exercise 1: Create a Windows VM
- Log in to the Azure Portal.
- Create a new resource group named
WindowsVMGroup
. - Create a new Windows Server VM in the
WindowsVMGroup
resource group. - Configure the VM with the following settings:
- Name:
WindowsVM
- Region:
East US
- Size:
Standard_B2s
- Username:
adminuser
- Password:
P@ssw0rd!
- Name:
Exercise 2: Monitor VM Performance
- Enable Azure Monitor for the
WindowsVM
. - Set up an alert to notify you if the CPU usage exceeds 80%.
Solutions
Solution to Exercise 1
- Log in to the Azure Portal: Navigate to Azure Portal and log in.
- Create a new resource group:
- Click on "Resource groups" in the left-hand menu.
- Click "Add" and enter
WindowsVMGroup
as the name and selectEast US
as the region.
- Create a new Windows Server VM:
- Click on "Create a resource" and select "Compute" > "Virtual Machine".
- Enter the following details:
- Name:
WindowsVM
- Region:
East US
- Size:
Standard_B2s
- Username:
adminuser
- Password:
P@ssw0rd!
- Name:
- Click "Review + create" and then "Create".
Solution to Exercise 2
- Enable Azure Monitor:
- Navigate to the
WindowsVM
in the Azure Portal. - Click on "Monitoring" > "Insights" and enable Azure Monitor.
- Navigate to the
- Set up an alert:
- In the
WindowsVM
blade, click on "Alerts" > "New alert rule". - Define the condition: CPU usage > 80%.
- Set up the action group to notify you via email.
- In the
Conclusion
In this module, you learned about Azure Virtual Machines, their benefits, and how to set them up and manage them. You also practiced creating and monitoring VMs through practical exercises. In the next module, we will explore Azure App Services, another core service in Azure.