Spot Instances 完全指南

折扣幅度
60-91%
vs On-Demand
中断风险
2%平均
每小时
预警告警
2分钟
提前通知
最佳使用
批处理
容错工作负载

$ aws ec2 request-spot-instances --instance-count 10 --type persistent

想象一下,你的渲染农场或者大数据批处理任务能便宜 90% 运行。Spot Instances 就是这个"折扣天堂"——AWS 用空闲容量给你打折,但代价是随时可能被回收。今天我们来看看怎么安全地用好 Spot Instances。

Spot Instances 工作原理

AWS 将闲置的 EC2 容量拿出来拍卖:

Spot vs On-Demand vs RI 对比

特性On-DemandReservedSpot
价格全价6折1-4折
中断风险有(2分钟告警)
容量保障无(随时可能失败)
适用场景关键业务稳定基线批处理/容错
灵活性最高

Spot 适用场景

最佳场景

不适合场景

最佳实践:用 On-Demand/RI 覆盖核心业务基线,用 Spot 处理峰值和批处理任务。

Spot 竞价策略

1. 手动竞价(One-Time)

设置你的心理价位:

aws ec2 request-spot-instances \
  --instance-count 5 \
  --type "one-time" \
  --spot-price "0.05" \
  --launch-specification file://spec.json

2. 持续请求(Persistent Request)

Spot 回收后自动重新竞价:

aws ec2 request-spot-fleet \
  --spot-fleet-request-config file://fleet-config.json

3. Spot Fleet(推荐生产使用)

Spot Fleet 可以:

处理 Spot 中断的正确方式

1. 监控中断信号

AWS 提供两种中断通知方式:

# 获取中断提示(元数据)
curl http://169.254.169.254/latest/meta-data/instance-action

2. 优雅关闭流程

在你的应用中添加 graceful shutdown:

# 收到中断信号后的处理脚本
/usr/bin/save-checkpoint.sh      # 保存进度
/usr/bin/notify-batch-manager.sh # 通知调度器
/usr/bin/sleep 90                # 给足 2 分钟缓冲
/usr/bin/shutdown.sh             # 安全关机

3. 使用 Checkpointing 和 Retry

设计你的作业时考虑:

Spot + Auto Scaling Group 集成

现代架构推荐组合:

成本计算示例

假设你需要 100 个 vCPU 运行机器学习训练任务:

关键要点:

Spot Instances 是云成本优化的"秘密武器",用得好可以帮你省下 60-90% 的计算费用!

$ share --net https://981263.xyz/S-003-Spot-Instances-Guide.html

Practical review before using this page

The S 003 Spot Instances Guide resource should be read together with the rest of Cloud Cost Optimization Dashboard, not as an isolated shortcut. Before acting on the page, write down the current baseline, the assumption you are making, and the result you expect to see. This makes the page more useful for comparison and reduces the chance of changing several variables at once.

For FinOps planning, cloud cost allocation, rightsizing, tagging governance, and monthly cost review, a good review habit is to separate stable facts from estimates. Stable facts might include dates, page URLs, account names, measured values, or the exact checklist items you completed. Estimates should be labeled as estimates and revisited later. If the result affects money, health, safety, compliance, or operational risk, use the page as preparation for a more careful review rather than as the final authority.

Common mistakes to avoid

Use this page as a planning reference before changing cloud accounts. Confirm pricing, contracts, and technical limits in the relevant provider console because cloud terms can change.