mcpField
C
cost-optimization

cost-optimization

github.com/hermeticormus云与 DevOps12375 次安装4 周前更新
代码仓库

关于 cost-optimization

面向管理 AWS、Azure 或 GCP 支出的团队,介绍云成本优化方法,涵盖成本可见性、资源规格调整、定价选择、架构和监控。

主要功能

  1. 01成本分摊与预算提醒
  2. 02资源利用率分析与规格调整
  3. 03预留与 Spot 定价选项
  4. 04存储和架构优化
  5. 05多云成本监控

适用场景

  1. 01降低云支出
  2. 02检查超配或闲置资源
  3. 03建立云成本治理
  4. 04规划多云预算

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/hermeticormus/libreuiux-claude-code --skill cost-optimization

安装后在 Claude Code 中输入/cost-optimization确认已加载。

详细介绍

云成本优化

用于优化 AWS、Azure 和 GCP 云成本的策略与模式。

目的

实施系统化的成本优化策略,在保持性能和可靠性的同时降低云支出。

适用场景

  • 降低云支出
  • 调整资源规格
  • 实施成本治理
  • 优化多云成本
  • 满足预算限制

成本优化框架

1. 可见性
  • 实施成本分配标签
  • 使用云成本管理工具
  • 设置预算提醒
  • 创建成本仪表板
2. 调整资源规格
  • 分析资源利用率
  • 缩减过度配置的资源
  • 使用自动扩缩
  • 移除闲置资源
3. 定价模式
  • 使用预留容量
  • 利用 Spot/可抢占实例
  • 实施节省计划
  • 使用承诺使用折扣
4. 架构优化
  • 使用托管服务
  • 实施缓存
  • 优化数据传输
  • 使用生命周期策略

AWS 成本优化

预留实例
Savings: 30-72% vs On-Demand
Term: 1 or 3 years
Payment: All/Partial/No upfront
Flexibility: Standard or Convertible
节省计划
Compute Savings Plans: 66% savings
EC2 Instance Savings Plans: 72% savings
Applies to: EC2, Fargate, Lambda
Flexible across: Instance families, regions, OS
Spot 实例
Savings: Up to 90% vs On-Demand
Best for: Batch jobs, CI/CD, stateless workloads
Risk: 2-minute interruption notice
Strategy: Mix with On-Demand for resilience
S3 成本优化
resource "aws_s3_bucket_lifecycle_configuration" "example" {
  bucket = aws_s3_bucket.example.id

  rule {
    id     = "transition-to-ia"
    status = "Enabled"

    transition {
      days          = 30
      storage_class = "STANDARD_IA"
    }

    transition {
      days          = 90
      storage_class = "GLACIER"
    }

    expiration {
      days = 365
    }
  }
}

Azure 成本优化

预留 VM 实例
  • 期限为 1 年或 3 年
  • 最高可节省 72%
  • 灵活调整规格
  • 可兑换
Azure 混合权益
  • 使用现有的 Windows Server 许可证
  • 配合 RI 最高可节省 80%
  • 适用于 Windows 和 SQL Server
Azure Advisor 建议
  • 调整 VM 规格
  • 删除未使用的资源
  • 使用预留容量
  • 优化存储

GCP 成本优化

承诺使用折扣
  • 承诺期限为 1 年或 3 年
  • 最高可节省 57%
  • 适用于 vCPU 和内存
  • 按资源或支出计费
持续使用折扣
  • 自动折扣
  • 运行中的实例最高可享 30% 折扣
  • 无需承诺
  • 适用于 Compute Engine、GKE
可抢占 VM
  • 最高可节省 80%
  • 最长运行时间为 24 小时
  • 最适合批处理工作负载

标签策略

AWS 标签
locals {
  common_tags = {
    Environment = "production"
    Project     = "my-project"
    CostCenter  = "engineering"
    Owner       = "[email protected]"
    ManagedBy   = "terraform"
  }
}

resource "aws_instance" "example" {
  ami           = "ami-12345678"
  instance_type = "t3.medium"

  tags = merge(
    local.common_tags,
    {
      Name = "web-server"
    }
  )
}

**参考:**参见 references/tagging-standards.md

成本监控

预算提醒
# AWS Budget
resource "aws_budgets_budget" "monthly" {
  name              = "monthly-budget"
  budget_type       = "COST"
  limit_amount      = "1000"
  limit_unit        = "USD"
  time_period_start = "2024-01-01_00:00"
  time_unit         = "MONTHLY"

  notification {
    comparison_operator        = "GREATER_THAN"
    threshold                  = 80
    threshold_type            = "PERCENTAGE"
    notification_type         = "ACTUAL"
    subscriber_email_addresses = ["[email protected]"]
  }
}
成本异常检测
  • AWS Cost Anomaly Detection
  • Azure Cost Management 提醒
  • GCP Budget 提醒

架构模式

模式 1:无服务器优先
  • 使用 Lambda/Functions 处理事件驱动工作负载
  • 仅按执行时间付费
  • 内置自动扩缩
  • 无闲置成本
模式 2:调整数据库规格
Development: t3.small RDS
Staging: t3.large RDS
Production: r6g.2xlarge RDS with read replicas
模式 3:多层存储
Hot data: S3 Standard
Warm data: S3 Standard-IA (30 days)
Cold data: S3 Glacier (90 days)
Archive: S3 Deep Archive (365 days)
模式 4:自动扩缩
resource "aws_autoscaling_policy" "scale_up" {
  name                   = "scale-up"
  scaling_adjustment     = 2
  adjustment_type        = "ChangeInCapacity"
  cooldown              = 300
  autoscaling_group_name = aws_autoscaling_group.main.name
}

resource "aws_cloudwatch_metric_alarm" "cpu_high" {
  alarm_name          = "cpu-high"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "2"
  metric_name         = "CPUUtilization"
  namespace           = "AWS/EC2"
  period              = "60"
  statistic           = "Average"
  threshold           = "80"
  alarm_actions       = [aws_autoscaling_policy.scale_up.arn]
}

成本优化检查清单

  • 实施成本分配标签
  • 删除未使用的资源(EBS、EIP、快照)
  • 根据利用率调整实例规格
  • 为稳定的工作负载使用预留容量
  • 实施自动扩缩
  • 优化存储类别
  • 使用生命周期策略
  • 启用成本异常检测
  • 设置预算提醒
  • 每周检查成本
  • 使用 Spot/可抢占实例
  • 优化数据传输成本
  • 实施缓存层
  • 使用托管服务
  • 持续监控和优化

工具

  • AWS: Cost Explorer、Cost Anomaly Detection、Compute Optimizer
  • Azure: Cost Management、Advisor
  • GCP: Cost Management、Recommender
  • 多云: CloudHealth、Cloudability、Kubecost

参考文件

  • references/tagging-standards.md - 标签约定
  • assets/cost-analysis-template.xlsx - 成本分析电子表格

相关技能

  • terraform-module-library - 用于资源配置
  • multi-cloud-architecture - 用于云平台选择

评论

支持 Markdown