Kubernetes智能扩展服务

ClaudeScale是一款基于AI的Kubernetes自动扩展服务,通过智能分析指标、模式和上下文来做出扩展决策,提供主动、可解释的多因素扩展功能。

  • by kratosvil
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 kratosvil/ClaudeScale

使用说明

ClaudeScale

智能Kubernetes自动伸缩,像人类SRE一样思考


ClaudeStale使用Claude AI根据指标、模式和上下文做出智能缩放决策,而不仅仅是简单的CPU阈值。


问题

传统的Kubernetes HPA(水平Pod自动缩放器)是反应式和基于规则的:

  • 只有在看到高CPU/内存后才会做出反应
  • 没有上下文意识(一天中的时间、模式、趋势)
  • 无法解释决策
  • 没有主动扩展

结果: 在流量高峰期间,反应时间缓慢,性能可能下降。


解决方案

ClaudeScale为Kubernetes自动扩展添加了AI智能:

  • 积极主动: 交通堵塞前的规模(基于预测/警告)
  • 上下文感知: 考虑时间、模式、历史数据
  • 可解释: 克劳德解释了为什么它会缩放
  • 多因素: 不仅仅是CPU——考虑内存、网络、趋势
  • 对话: 与您的基础设施聊天

建筑

┌─────────────────────────────────────────────────────────┐
│                        User                             │
│                          ↓                              │
│               Claude Desktop (Chat UI)                  │
│                          ↓                              │
│               MCP Server (Python)                       │
│                          ↓                              │
│          ┌───────────────┴───────────────┐              │
│          ↓                               ↓              │
│    Prometheus API                 Kubernetes API        │
│    (Metrics)                      (Scaling)             │
│          ↓                               ↓              │
│   ┌──────────┐                  ┌──────────────┐        │
│   │Prometheus│                  │  Pods (2-5x) │        │
│   │  Pod     │◄──────scrapes────┤              │        │
│   └──────────┘                  └──────────────┘        │
└─────────────────────────────────────────────────────────┘

特性

核心能力

  • 人工智能驱动决策: Claude分析指标并决定何时/如何扩展
  • 实时监控: Prometheus集成CPU、内存和网络
  • 智能缩放: 考虑多种因素,而不仅仅是阈值
  • 审计跟踪: 每个决定都会被记录和解释
  • 安全限值: 最少2个,最多5个经过验证的副本

智能功能

  • 主动扩展: 流量冲击前的缩放
  • 模式识别: 从历史数据中学习
  • 自然语言控制: “10分钟内为高流量做好准备”
  • 情境感知: 一天中的时间、趋势、最近的事件
  • 成本优化: 不需要时缩小规模

快速开始

先决条件

  • Ubuntu 22.04+或类似的Linux
  • Docker已安装并正在运行
  • 8GB+内存
  • Claude桌面应用程序

安装

# 1. Clone repository
git clone https://github.com/kratosvil/ClaudeScale.git
cd ClaudeScale

# 2. Create virtual environment
python3 -m venv venv
source venv/bin/activate
pip install -r requirements-mcp.txt

# 3. Start Minikube
minikube start --driver=docker --memory=4096 --cpus=2

# 4. Deploy Kubernetes resources
kubectl apply -f k8s-manifests/namespace.yaml
kubectl apply -f k8s-manifests/prometheus-configmap.yaml
kubectl apply -f k8s-manifests/prometheus-deployment.yaml
kubectl apply -f k8s-manifests/grafana-configmap.yaml
kubectl apply -f k8s-manifests/grafana-deployment.yaml
kubectl apply -f k8s-manifests/demo-app.yaml
kubectl apply -f k8s-manifests/demo-app-hpa.yaml

# 5. Start port-forwards (each in separate terminal)
kubectl port-forward -n claudescale svc/prometheus 9090:9090
kubectl port-forward -n claudescale svc/grafana 3000:3000
kubectl port-forward -n claudescale svc/demo-app 8080:80

# 6. Open Claude Desktop and chat!

使用示例

检查群集状态

You: Check my Kubernetes cluster status

Claude: Your cluster is healthy with 4 pods running
(demo-app: 2/2, prometheus: 1/1, grafana: 1/1).

分析指标

You: What's the CPU usage?

Claude: Current CPU usage is at 85% utilization
(0.17 cores avg). This is high - I recommend scaling.

主动扩展

You: We're expecting 3x traffic in 10 minutes

Claude: I'll scale proactively to 4 replicas now to
prepare for the spike. This gives you 2x capacity
headroom and prevents performance degradation.

生成报告

You: Create a scaling report

Claude: [Generates comprehensive markdown report with
state, metrics, actions, and recommendations]

MCP工具

ClaudeStale为Claude AI提供了4个工具:

工具 目的 示例
claudescale_get_current_state 查看部署和副本 “显示群集状态”
claudescale_get_metrics 查询Prometheus的CPU/内存 “检查CPU使用情况”
claudescale_scale_deployment 放大/缩小(2-5个副本) “扩展到4个Pod”
claudescale_generate_report 创建审计报告 “生成报告”

ClaudeScale与HPA

特征 HPA(原生K8s) ClaudeScale(AI)
决策逻辑 简单阈值(CPU>50%) 多因素AI推理
时机 反应性(尖峰后) 主动性(尖峰前)
可解释性 无 完整解释
情境感知 否 是(时间、模式、趋势)
预测 否 是
自定义指标 有限 任何普罗米修斯指标
对话式 否 是(自然语言)

演示场景

场景1:反应式扩展

  1. 产生高CPU负载: bash scripts/stress-until-scale.sh
  2. 让克劳德检查并缩放
  3. 克劳德检测到85%的CPU→ 比例尺2→3 副本
  4. 解释:“检测到高利用率,正在增加容量”

场景2:主动扩展

  1. 告诉克劳德:“预计5分钟后交通会激增”
  2. 克劳德量表2→4 立即复制副本
  3. 流量到达时,CPU保持在50%(而没有扩展时为90%)

场景3:成本优化

  1. 交通流量下降
  2. 请Claude进行优化
  3. 克劳德量表4→2 复制品:“优化成本效益”

项目结构

ClaudeScale/
├── mcp-server/                    # MCP Server (Python)
│   ├── server.py                  # Entry point FastMCP
│   ├── config.py                  # Configuration (.env)
│   ├── tools/
│   │   └── scaling_tools.py       # 4 MCP tools
│   └── utils/
│       ├── kubernetes_client.py   # kubectl wrapper
│       └── prometheus_client.py   # Prometheus wrapper
├── k8s-manifests/                 # Kubernetes YAML files
│   ├── namespace.yaml
│   ├── rbac.yaml
│   ├── prometheus-*.yaml
│   ├── grafana-*.yaml
│   └── demo-app*.yaml
├── monitoring/                    # Grafana dashboards & configs
├── scripts/                       # Automation scripts
│   ├── stress-until-scale.sh      # Stress test
│   ├── watch-autoscaling.sh       # Monitor HPA
│   ├── test-mcp-server.sh         # Automated tests
│   └── generate-load.sh           # HTTP load generator
├── docs/                          # Documentation
└── README.md

安全

  • RBAC: 具有最低权限的ServiceAccount(列表、获取、补丁部署)
  • 命名空间隔离: 专用命名空间中的所有资源 claudescale
  • 副本限制: 硬最小值(2)和最大值(5),以防止失控结垢
  • 只读指标: Prometheus查询是只读的
  • 审计跟踪: 所有记录的动作都有推理

测试

# Automated tests
./scripts/test-mcp-server.sh

# Load test (trigger scaling)
./scripts/stress-until-scale.sh

# Monitor in real-time
./scripts/watch-autoscaling.sh

监控

Grafana仪表板

kubectl port-forward -n claudescale svc/grafana 3000:3000
# Open: http://localhost:3000 (admin/admin)

普罗米修斯

kubectl port-forward -n claudescale svc/prometheus 9090:9090
# Open: http://localhost:9090

许可证

MIT许可证-请参阅 许可证 文件


作者

牛瘟

  • github: @牛瘟
  • DevOps工程师——AWS、Terraform、Kubernetes、AI/DevOps集成

致谢

  • Claude AI和MCP协议的拟人化
  • Kubernetes和CNCF社区
  • 普罗米修斯项目

旨在展示人工智能基础设施自动化的未来