代理式Kubernetes管理
Strands AI代理利用MCP服务器来暴露Kubernetes工具(Pods、命名空间、日志),以实现自然语言集群管理。
  
🌟 特点
- 自然语言接口 - 使用对话式人工智能管理Kubernetes集群
- MCP协议 - 利用模型上下文协议实现可扩展的工具集成
- Strands框架 - 基于Strands AI智能体框架构建,实现稳健的智能体行为
- AWS Bedrock 集成 - 由亚马逊Bedrock平台上的Claude模型提供支持
- 实时操作 - 列出Pod,检查命名空间,并即时获取日志
- 情境感知 - 保存对话历史以便进行智能后续查询
- 准备就绪,可用于生产 - 错误处理、优雅关闭和全面日志记录
🏗️ 建筑学
┌─────────────────┐
│ User Input │
│ (Natural Lang) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Strands Agent │
│ (Claude Model) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ MCP Server │
│ (Tool Provider) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Kubernetes API │
│ │
└─────────────────┘🚀 快速入门
先决条件
- Python 3.12 或更高版本
- Kubernetes 集群访问(已配置 kubeconfig)
- 为Bedrock访问配置的AWS凭证
- 已启用Anthropic模型
安装
- 克隆仓库
git clone https://github.com/Tarique-B-DevOps/Agentic-Kubernetes-Management.git
cd Agentic-Kubernetes-Management- 安装依赖项
pip install -r requirements.txt- 设置环境变量
export BEDROCK_MODEL_REGION="us-east-1"
export BEDROCK_MODEL_ID="apac.anthropic.claude-3-5-sonnet-20241022-v2:0"- 验证Kubernetes访问权限
kubectl cluster-info- 运行代理程序
python agent.py💬 使用示例
基本查询
🔍 Your query: Show me all namespaces
📊 Response: Found 5 namespaces:
- default (Active)
- kube-system (Active)
- kube-public (Active)
- monitoring (Active)
- production (Active)🔍 Your query: List pods in the production namespace
📊 Response: Found 12 pods in namespace 'production':
- nginx-deployment-7d8f9c5b6-abc12 (Running)
- redis-master-0 (Running)
- api-server-v2-5f9d8c7b-xyz34 (Running)
...🔍 Your query: Get logs from api-server-v2-5f9d8c7b-xyz34
📊 Response: Logs from pod 'api-server-v2-5f9d8c7b-xyz34' (container: 'api', namespace: 'production'):
2024-01-15 10:23:45 INFO Starting API server...
2024-01-15 10:23:46 INFO Connected to database
...高级查询
🔍 Your query: Are there any failing pods in kube-system?
🔍 Your query: Show me the last 50 lines of logs from the nginx pod
🔍 Your query: Which pods are running on node worker-01?🛠️ 可用工具
| 工具 | 描述 | 参数 |
|---|---|---|
get-pods | 列出命名空间中的Pod | namespace (默认:“default”) |
get-namespaces | 列出所有命名空间 | 无 |
check-logs | 获取Pod日志 | pod_name, namespace, container, tail_lines |
📁 项目结构
Agentic-Kubernetes-Management/
├── k8s_agent.py # Main Strands agent with interactive CLI
├── k8s_mcp_server.py # MCP server exposing Kubernetes tools
├── requirements.txt # Python dependencies
├── README.md # Project documentation
└── .env.example # Environment variables template🔧 配置
环境变量
| 变量 | 描述 | 默认值 |
|---|---|---|
BEDROCK_MODEL_REGION | Bedrock 所在的 AWS 区域 | us-east-1 |
BEDROCK_MODEL_ID | Claude模型标识符 | apac.anthropic.claude-3-5-sonnet-20241022-v2:0 |
Kubernetes 配置
确保您的 ~/.kube/config 已正确配置:
kubectl config view
kubectl config current-context🧩 工作原理
1. MCP 服务器(k8s_mcp_server.py)
MCP服务器使用FastMCP框架将Kubernetes操作作为可调用工具暴露出来:
get-pods查询Kubernetes API以获取Pod信息get-namespaces- 列出集群中的所有命名空间check-logs- 从Pod中检索容器日志
2. Strands 代理(agent.py)
The Strands 的代理:
- 通过stdio传输连接到MCP服务器
- 动态加载可用工具
- 使用Claude处理自然语言查询
- 在多个查询之间保持对话上下文
- 根据用户意图调用相应的工具
3. 模型上下文协议(MCP)
MCP启用:
- 工具发现 - 代理发现可用的Kubernetes工具
- 结构化沟通 - 标准化的请求/响应格式
- 可扩展性 - 轻松添加新工具,无需更改代理代码
🚧 路线图/发展路线图
- \[ \] 上下文设置
- \[ \] 添加部署管理工具
- \[ \] 对ConfigMaps和Secrets的支持
- \[ \] 节点资源监控
- \[ \] 服务和入口(Ingress)管理
- \[ \] 支持多集群
- \[ \] Prometheus 指标集成
- \[ \] Slack/Discord机器人集成
- \[ \] Web UI 控制面板
致谢
- Strands人工智能框架 - 代理编排
- 模型上下文协议 - 工具集成标准
- Anthropic的Claude - 语言模型
- AWS Bedrock - 模型托管
- Kubernetes Python 客户端 K8s API 访问
