Kubernetes AI助手

一个AI驱动的Kubernetes SRE助手,帮助诊断和解决集群问题,支持本地工具和LLM集成。

  • by ash-ketchum0011
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 ash-ketchum0011/MCP-with-Kubernetes

使用说明

Kubernetes AI 助手

一个基于人工智能的Kubernetes站点可靠性工程(SRE)助手,利用本地工具和大型语言模型(LLM)集成来帮助诊断和解决集群问题。

特点/功能

  • 🤖 基于人工智能的聚类分析
  • 🔍 只读Kubernetes操作
  • 📊 Pod指标和日志检查
  • 🎯 使用 kubectl 命令进行智能推荐
  • 🔐 自动日志清理(IP地址、电子邮件、令牌)

先决条件

  • Python 3.8及以上版本
  • `kubectl` 已配置为可访问集群
  • (可选)用于GPT-4o-mini的OpenAI API密钥
  • (可选)用于Pod指标的metrics-server

快速入门

1. 克隆并设置

# Ensure your kubectl is configured
kubectl cluster-info

# Run the setup script
chmod +x run.sh
./run.sh

2. 选择您的LLM适配器

选项A:本地模式(无需API密钥)

# Default - uses simple local adapter for testing
python cli/chat.py

选项B:OpenAI模式

# Set your API key
export OPENAI_API_KEY="sk-..."
export LLM_ADAPTER="openai"

python cli/chat.py

3. 开始聊天

🤖 Kubernetes AI Assistant
Type 'exit' to quit

You: Show me all pods in default namespace

项目结构

.
├── mcp/
│   ├── __init__.py       # MCP module exports
│   ├── server.py         # Tool registry and local MCP server
│   └── tools.py          # Kubernetes tool implementations
├── ai/
│   ├── __init__.py
│   ├── adapters.py       # LLM adapters (OpenAI, Local)
│   └── agent.py          # AI agent orchestration
├── cli/
│   └── chat.py           # Terminal chat interface
├── requirements.txt      # Python dependencies
├── run.sh               # Setup and run script
└── README.md            # This file

可用工具

助手可以访问这些只读的 Kubernetes 工具:

  • get_pods - 列出状态为(某种状态)的Pods
  • get_pod_logs - 获取(已清理的)Pod日志
  • get_pod_metrics - CPU/内存使用率(需要metrics-server)
  • get_cluster_events - 最近的集群事件
  • get_deployments - 列出部署
  • get_services - 列出服务

示例查询

- "Are there any crashing pods?"
- "Show me logs for pod nginx-abc123"
- "What's using the most memory?"
- "Check the cluster events"
- "Why is my deployment not ready?"

配置

环境变量

  • LLM_ADAPTER - 设置为“openai”或“local”(默认:“local”)
  • OPENAI_API_KEY - 您的OpenAI API密钥(openai适配器必需)

Kubernetes 访问

该工具使用您的 kubectl 配置:

  • ~/.kube/config (默认)
  • 或者,如果在 Kubernetes 中运行,则使用集群内配置

安全特性

  • ✅ 仅支持只读操作
  • ✅ 自动日志净化
  • ✅ 不允许有集群突变
  • ✅ 在日志中对令牌/IP/电子邮件进行模糊处理

故障排除

“无法加载 Kubernetes 配置”

# Check your kubectl access
kubectl cluster-info
kubectl get nodes

指标API不可用

# Install metrics-server (optional)
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

“OPENAI_API_KEY未设置”

# Set your API key
export OPENAI_API_KEY="sk-your-key-here"
export LLM_ADAPTER="openai"

发展

运行测试

source .venv/bin/activate
python -m pytest tests/

添加新工具

  1. 添加功能到 mcp/tools.py:
@tool()
async def my_new_tool(arg1: str):
    """Tool description"""
    # Implementation
    return {"result": "data"}
  1. 工具会自动注册并可供AI使用

许可证

MIT 许可证 - 详见 LICENSE 文件

贡献

欢迎贡献!请提交问题或拉取请求(PR)。