Microsoft Cloud Account Purchase: Hands-on Teach You to Use Azure VMSS to Automate and Elastic Scaling of Applications
In the last tutorial, we successfully deployed the first virtual machine in Azure. But in reality, if your website suddenly encounters a "double eleven" type of traffic attack, a single virtual machine may be instantly paralyzed; and in the middle of the night, when the traffic is low, letting a bunch of high-performance servers idle is a huge waste of money.
In the cloud native era, we should not manually buy servers and match environments.
Azure Virtual Machine Scale Sets (Virtual Machine Scale Sets, or VMSS)
It was born to solve this pain point. It can be based on changes in business traffic,
Automatically help you increase (buy) or decrease (retire) the number of virtual machine instances
.
Today's in-depth tutorial will take you from scratch to build an ultra-high usable Web application architecture with "automatic elastic expansion and contraction" capability.
1. Core Concepts: What is VMSS and Elastic Scaling?
Before we start, we first use the vernacular to sort out two hard core concepts:
Virtual Machine Scale Set (VMSS): Simply put, it is a "typewriter template". You only need to configure the operating system and software environment of a virtual machine, and VMSS can help you clone 5, 50 or even 500 identical virtual machines in a few minutes.
Horizontal expansion (Scale Out) and horizontal contraction (Scale In): vertical expansion is to replace dual-core CPU with quad-core (need to restart, there is an upper limit); Horizontal expansion is "more people and more strength"-one cannot carry it, and two more will be automatically added. If the traffic is gone, it will be automatically returned. This is called elasticity (Elasticity).
2. Core Architecture Design
What we want to build is not a bare scale set, but a standard production environment architecture:
[Internet traffic (HTTP/80)]
│
▼
[Azure Load Balancer]
(Load Balancer)
│
-----------------------------------------------------------------------------
▼ ▼
[VMSS instance 1 ] [ VMSS instance 2 ] ... (increases/decreases automatically by CPU)
The traffic arrives at the load balancer first and is evenly distributed to the backend VMSS instances. VMSS will keep an eye on the CPU loss of these instances, and once the threshold is exceeded, it will automatically split a new instance to join the battle.
3. Phase 1: Creating and Configuring the VMSS (Infrastructure)
Log in to the Azure portal (Azure Portal) and enter
Enter
Virtual Machine Scale Sets"
(Virtual Machine Scale Sets), click Create ".
1. Basic information
Resource group: You are advised to create a new one, such as a MyVMSS-RG.
Virtual machine scale set name: for example, my-web-scale-set.
Region: Select the region closest to you (for example, East Asia).
Business Process: Select Unified (Uniform) ". This ensures that all cloned machines are identical and best suited for stateless web applications.
Image: Here we take Ubuntu Server 24.04 LTS as an example (the Windows operation logic is exactly the same).
2. Scale and specifications
Size: still recommend very cost-effective Standard_B1s or Standard_B2s (practice enough, save money first).
Authentication Type: Select Password or SSH Key ". In order to facilitate the teaching demonstration, we choose the password here, set the user name (such as azureuser) and strong password.
3. Network configuration (key step)
Pull to the network section and you will see the "Load Balancing" option.
Select Use a load balancer ".
Load balancer options: Select Azure Load Balancer ".
Choose Load Balancer/New: Name it my-load-balancer.
Choose Backend Pool/New: Name my-backend-pool.
💡In this way, in the future, no matter how many virtual machines are automatically created by VMSS, Azure will automatically stuff them into this "back-end pool", and the load balancer will automatically distribute the user's access traffic, without you having to manually configure the IP.
4. Phase 2: Out of the Box with Custom Scripts"
The scale assembly automatically helped us build the machine, but the new machine was empty. How can the newly cloned machine automatically install Web services (such as Nginx) and run our application?
We need to use
Advanced (Advanced)
option in
Custom Script Extension"
.
In the creation page's
"Advanced"
tab, find "Custom Data" and paste the following Linux initialization script in the text box:
Bash
#! /bin/bash
# Update the system and install Nginx
apt-get update -y
apt-get install nginx -y
# Start the Nginx service
systemctl start nginx
sys
temctl enable nginx
# Write a web page to dynamically display the name of the current host (convenient for us to test and see the effect)
echo "<h1>Hello from Azure VMSS! My Hostname is: $(hostname)</h1>" > /var/www/html/index.html
What this script does is:
Whenever VMSS finds a large traffic and automatically creates a new virtual machine, the new machine will automatically execute this code at the moment of startup-install Nginx, start the service, and write the web page. Truly unattended automation.
Phase 3 of the 5.: Configure the core-automatic elastic expansion and contraction strategy.
Tap to switch
Zoom (Scaling)"
tab. This is the soul of the whole tutorial.
The default option is "manual zoom", we need to change it
Custom Autoscale (Custom autoscale)"
.
Next, we need to configure a strict set of "scaling rules":
1. Set the instance scope
Minimum number of instances (Minimum): 1 (usually when no one visits, keep 1 machine to save money).
Maximum number of instances (Maximum): 3 (to prevent the code from being written incorrectly and falling into a dead loop, or in case of malicious attacks, the credit card will be maxed out due to unlimited opening of the machine, and an upper limit will be set to protect the wallet).
Default number of instances (Default): 1.
2. Add Scale Out Rule
Click "Add Rules" and we will tell the system when to add machines:
Measure Source: Current Resource (VMSS).
Time Aggregation: Average.
Measure name: Percentage CPU(CPU Usage).
Operator: Greater.
Threshold (Metric threshold): 70 (when the average CPU usage exceeds 70%).
Duration (minutes): 5 (this situation lasted for more than 5 minutes, indicating that it is not an occasional instantaneous fluctuation, it is really unable to hold it).
Action: Increase the count.
Number of instances: 1 (add 1 machine each time).
Cooling time (minutes): 5 (after adding a machine, let the bullet fly for a while, observe for 5 minutes, don't add too fast).
3. Add a Scale In Rule
If there is a loan, add another rule to tell the system when to return the machine to save money:
Measure name: Percentage CPU.
Operator: Less.
Threshold: 30 (when the average CPU is idle below 30%).
Action: Decrease the count.
Number of instances: 1 (1 machine per cut
until it is reduced to the lowest value of 1).
After the configuration is complete, click
“View Create”
and wait for the deployment to complete.
6. Verification Miracle: How do I test autoscaling?
After the deployment is complete, go to your resource group and find that
Load Balancer (my-load-balancer)
, copy its "front-end public IP address".
Normal access: Enter this IP in the browser and you should see Hello from Azure VMSS! My Hostname is: my-web-scale-set_0. Refresh several times, the page is as stable as Mount Tai.
Artificial Critical Strike (Pressure Measurement): We need to manually pull up the CPU of this machine. Connect to the current virtual machine instance through SSH, and run a classic CPU stress test command (or install stress tool) in the terminal: Bash# install stress test tool sudo apt-get install stress -y# to make CPU 4 core run at full load (even if you only have 1 core, it will be fully loaded) stress -- cpu 4 -- timeout 600
3. **Witness the miracle:**
Return to the VMSS page in the Azure portal and click **" Run history "** or **" Instance (Instances) "** on the left.
After about 5 minutes, you will find the CPU curve in the chart rising steeply. Then, a new instance is refreshed from the instance list: 'my-web-scale-set_1', with the status of "creating"-> "running".
4. **Visit again:**
Crazy refresh the load balancer IP in your browser. You will find that for a while it is' Hostname is: ..._0 ', and for a while it is' Hostname is: ..._1'.
**This indicates that the load balancer has successfully diverted part of the traffic to the newborn younger brother!**
5. **Automatic exit:**
After 10 minutes, the pressure test command ends and the CPU falls back. In another 5-10 minutes, the system triggers the shrink-in rule, and the newly created virtual machine will be automatically and gracefully shut down and deleted, and everything will be restored to its original state.
---
##7. summary and pit drainage guide
Through today's advanced learning, you have unlocked the core capabilities of cloud native architecture-high availability and elastic self-healing. In the actual production environment application, there are two tips to note:
* * * Stateless design (Stateless):* * The machine with elastic expansion and contraction capacity is "born with death. Absolutely do not
The user's uploaded files and databases are stored in the local hard disk of the VMSS virtual machine, otherwise the capacity will be completely lost. Files should be saved to Azure Blob Storage (object storage), and data should be saved to a separate database such as Azure SQL.
* * * Image Update: * * We are using scripts to install Nginx today. If your application is very complicated and depends on a lot, it is better to match all the environments in a common VM, then package it into a * * Custom Image (Custom Image)* *, and let VMSS clone directly based on your custom image.
With VMSS, you no longer have to stare at the server traffic billboard in the middle of the night, and you can safely hand over high availability to Microsoft Cloud's automated algorithms!

