Azure 虚拟机规模集 #
什么是虚拟机规模集? #
虚拟机规模集(Virtual Machine Scale Sets,VMSS)是 Azure 提供的自动扩展解决方案,可以自动增加或减少 VM 实例数量。
text
┌─────────────────────────────────────────────────────────────┐
│ VMSS 概览 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 特点 │
│ ├── 自动扩展:根据负载自动调整实例数量 │
│ ├── 负载均衡:自动分配流量 │
│ ├── 高可用:跨可用区分布 │
│ ├── 统一管理:相同配置的 VM │
│ └── 自动修复:自动替换不健康实例 │
│ │
│ 适用场景 │
│ ├── Web 前端集群 │
│ ├── 计算集群 │
│ ├── 批处理工作负载 │
│ └── 微服务 │
│ │
└─────────────────────────────────────────────────────────────┘
编排模式 #
统一编排 vs 灵活编排 #
text
┌─────────────────────────────────────────────────────────────┐
│ 编排模式对比 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 统一编排 (Uniform) │
│ ├── 所有 VM 相同配置 │
│ ├── 自动扩展 │
│ ├── 适合大规模相同工作负载 │
│ └── 传统模式 │
│ │
│ 灵活编排 (Flexible) │
│ ├── 独立 VM 管理 │
│ ├── 支持 Spot 实例 │
│ ├── 更灵活的扩展策略 │
│ └── 推荐模式 │
│ │
└─────────────────────────────────────────────────────────────┘
创建规模集 #
使用 Azure CLI #
bash
# 创建资源组
az group create --name myResourceGroup --location eastus
# 创建虚拟机规模集
az vmss create \
--resource-group myResourceGroup \
--name myScaleSet \
--image UbuntuLTS \
--upgrade-policy-mode automatic \
--instance-count 2 \
--admin-username azureuser \
--generate-ssh-keys \
--lb myLoadBalancer
# 查看规模集
az vmss list --resource-group myResourceGroup --output table
创建灵活编排规模集 #
bash
# 创建灵活编排规模集
az vmss create \
--resource-group myResourceGroup \
--name myFlexibleScaleSet \
--image UbuntuLTS \
--orchestration-mode flexible \
--instance-count 2 \
--admin-username azureuser \
--generate-ssh-keys
自动扩展 #
自动扩展规则 #
text
┌─────────────────────────────────────────────────────────────┐
│ 自动扩展规则 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 扩展条件 │
│ ├── CPU 使用率 > 70% │
│ ├── 内存使用率 > 80% │
│ ├── 请求队列长度 > 100 │
│ └── 自定义指标 │
│ │
│ 缩减条件 │
│ ├── CPU 使用率 < 30% │
│ ├── 内存使用率 < 40% │
│ └── 冷却时间:5-10 分钟 │
│ │
│ 实例范围 │
│ ├── 最小实例数: 2 │
│ ├── 最大实例数: 10 │
│ └── 默认实例数: 2 │
│ │
└─────────────────────────────────────────────────────────────┘
配置自动扩展 #
bash
# 定义自动扩展设置
az monitor autoscale create \
--resource-group myResourceGroup \
--resource myScaleSet \
--resource-type Microsoft.Compute/virtualMachineScaleSets \
--name autoscale \
--min-count 2 \
--max-count 10 \
--count 2
# 添加扩展规则
az monitor autoscale rule create \
--resource-group myResourceGroup \
--autoscale-name autoscale \
--scale out 1 \
--condition "Percentage CPU > 70 avg 5m"
# 添加缩减规则
az monitor autoscale rule create \
--resource-group myResourceGroup \
--autoscale-name autoscale \
--scale in 1 \
--condition "Percentage CPU < 30 avg 5m"
基于计划的扩展 #
bash
# 创建计划扩展规则
az monitor autoscale profile create \
--resource-group myResourceGroup \
--autoscale-name autoscale \
--name weekend-profile \
--min-count 1 \
--max-count 5 \
--count 1 \
--recurrence week sat sun
实例管理 #
手动扩展 #
bash
# 手动扩展实例数
az vmss scale \
--resource-group myResourceGroup \
--name myScaleSet \
--new-capacity 5
# 查看实例状态
az vmss list-instances \
--resource-group myResourceGroup \
--name myScaleSet \
--output table
实例保护 #
bash
# 保护实例不被删除
az vmss update \
--resource-group myResourceGroup \
--name myScaleSet \
--instance-id 0 \
--protect-from-scale-set-actions true
# 取消保护
az vmss update \
--resource-group myResourceGroup \
--name myScaleSet \
--instance-id 0 \
--protect-from-scale-set-actions false
升级策略 #
升级模式 #
text
┌─────────────────────────────────────────────────────────────┐
│ 升级模式 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 自动 (Automatic) │
│ ├── 自动更新所有实例 │
│ ├── 滚动更新 │
│ └── 适合无状态应用 │
│ │
│ 滚动 (Rolling) │
│ ├── 分批更新 │
│ ├── 可配置批次大小 │
│ └── 适合需要控制更新速度 │
│ │
│ 手动 (Manual) │
│ ├── 手动触发更新 │
│ ├── 完全控制 │
│ └── 适合关键应用 │
│ │
└─────────────────────────────────────────────────────────────┘
手动升级实例 #
bash
# 更新规模集镜像
az vmss update \
--resource-group myResourceGroup \
--name myScaleSet \
--set virtualMachineProfile.storageProfile.imageReference.version=latest
# 手动升级所有实例
az vmss update-instances \
--resource-group myResourceGroup \
--name myScaleSet \
--instance-ids "*"
# 升级特定实例
az vmss update-instances \
--resource-group myResourceGroup \
--name myScaleSet \
--instance-ids 0 1 2
自定义脚本扩展 #
bash
# 添加自定义脚本扩展
az vmss extension set \
--resource-group myResourceGroup \
--vmss-name myScaleSet \
--name customScript \
--publisher Microsoft.Azure.Extensions \
--settings '{"fileUris": ["https://example.com/install.sh"]}' \
--protected-settings '{"commandToExecute": "sh install.sh"}'
健康监控 #
应用健康扩展 #
bash
# 添加应用健康扩展
az vmss extension set \
--resource-group myResourceGroup \
--vmss-name myScaleSet \
--name ApplicationHealth \
--publisher Microsoft.ManagedServices \
--settings '{"protocol": "http", "port": 80, "requestPath": "/health"}'
自动修复策略 #
bash
# 启用自动修复
az vmss update \
--resource-group myResourceGroup \
--name myScaleSet \
--set automaticRepairsPolicy.enabled=true \
--set automaticRepairsPolicy.gracePeriod=PT10M
负载均衡 #
集成负载均衡器 #
text
┌─────────────────────────────────────────────────────────────┐
│ 负载均衡架构 │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ │
│ │ 用户请求 │ │
│ └──────┬──────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ 公网 IP │ │
│ └──────┬──────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ 负载均衡器 │ │
│ └──────┬──────┘ │
│ │ │
│ ┌───────────────┼───────────────┐ │
│ ▼ ▼ ▼ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ VM-1 │ │ VM-2 │ │ VM-3 │ │
│ └──────────┘ └──────────┘ └──────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
配置 NAT 规则 #
bash
# 创建入站 NAT 池
az network lb inbound-nat-pool create \
--resource-group myResourceGroup \
--lb-name myLoadBalancer \
--name myNatPool \
--protocol Tcp \
--frontend-port-range-start 50000 \
--frontend-port-range-end 50100 \
--backend-port 22
高可用配置 #
可用性区域 #
bash
# 创建跨区域规模集
az vmss create \
--resource-group myResourceGroup \
--name myZonalScaleSet \
--image UbuntuLTS \
--zones 1 2 3 \
--instance-count 3 \
--admin-username azureuser \
--generate-ssh-keys
容错域 #
bash
# 查看容错域
az vmss show \
--resource-group myResourceGroup \
--name myScaleSet \
--query "properties.platformFaultDomainCount"
最佳实践 #
设计建议 #
text
┌─────────────────────────────────────────────────────────────┐
│ 最佳实践 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. 无状态设计 │
│ ├── 使用外部存储 │
│ ├── 避免本地状态 │
│ └── 会话外部化 │
│ │
│ 2. 健康检查 │
│ ├── 配置健康探针 │
│ ├── 启用自动修复 │
│ └── 监控实例状态 │
│ │
│ 3. 扩展策略 │
│ ├── 设置合理的冷却时间 │
│ ├── 避免频繁扩展 │
│ └── 监控扩展事件 │
│ │
│ 4. 镜像管理 │
│ ├── 使用自定义镜像 │
│ ├── 版本控制 │
│ └── 快速部署 │
│ │
└─────────────────────────────────────────────────────────────┘
下一步 #
现在你已经掌握了虚拟机规模集的使用,接下来学习 App Service 了解 PaaS 计算服务!
最后更新:2026-03-29