Kubernetes网关故障跟踪MCP服务器
MCP(模型上下文协议)服务器,通过提供智能日志检索和分析功能来帮助跟踪和分析Kubernetes网关故障。
概述
此服务器将工具暴露给:
- 通过特定于部署的过滤从多个Kubernetes命名空间中检索pod日志
- 支持init容器(标有
[INIT]前缀) - 分析整个Kubernetes基础架构中与网关相关的故障
- 按每个命名空间中的部署名称筛选日志
- 为每个命名空间配置不同的尾线限制
特性
- 🔍 多命名空间日志检索 具有命名空间范围的部署筛选
- 🎯 特定于部署的筛选 -指定每个命名空间要监视的部署
- 🔧 Init容器支持 -自动检测并标记初始化容器
- 📊 结构化日志输出 具有清晰的pod、容器和命名空间分离
- ⚙️ 灵活配置 -每个命名空间的不同尾线限制
- 🔄 以前的容器日志 支持对崩溃的容器进行故障排除
- 🚀 使用MCP SDK构建 实现无缝的AI助手集成
安装
先决条件
- 通过有效身份访问Kubernetes集群
kubectl配置 kubectl已安装并配置命令行工具
从源代码安装
# Clone the repository
git clone https://github.com/challamani/trace-gateway-failures.git
cd trace-gateway-failures
# Build the server
go build -o mcp-server .
# Run the server
./mcp-server用法
运行服务器
您可以使用以下任一方式运行服务器 stdio 或 sse 运输:
# Using stdio transport (default)
./mcp-server --transport stdio
# Using SSE transport
./mcp-server --transport sse可用工具
get_pod_logs
检索给定命名空间和部署名称的pod日志,以跟踪网关故障。 现在支持init容器 (标有 [INIT] 前缀)和 命名空间范围的部署筛选.
参数:
namespaces(对象数组,必填):命名空间配置数组
- name (string,必填):Kubernetes命名空间名称 - deployments (字符串数组,必填):用于过滤此命名空间中Pod的部署名称列表 - tail_lines (整数,可选,默认值:100):此命名空间日志末尾的行数
previous(boolean,可选,默认值:false):从之前终止的容器中检索日志
示例请求-单个命名空间:
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "get_pod_logs",
"arguments": {
"namespaces": [
{
"name": "production",
"deployments": ["api-gateway", "auth-service"],
"tail_lines": 200
}
],
"previous": false
}
}
}示例请求-具有不同配置的多个命名空间:
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "get_pod_logs",
"arguments": {
"namespaces": [
{
"name": "production",
"deployments": ["api-gateway"],
"tail_lines": 500
},
{
"name": "staging",
"deployments": ["api-gateway", "payment-service"],
"tail_lines": 100
}
],
"previous": false
}
}
}示例响应:
该工具返回格式化的日志,显示:
- 命名空间和pod信息
- Pod状态和所有者参考(部署/ReplicaSet)
- 初始化容器日志
[INIT]前缀 (例如。,[INIT] istio-init) - 来自所有常规容器的日志(包括Sidecar,如
istio-proxy) - 不同吊舱和容器之间应明确分隔
样本输出:
=== Pod Logs Analysis for Gateway Failure Tracing ===
Namespace: production
================================================================================
Pod: api-gateway-7d8f9c5b6-x4k2m (Status: Running)
Owner: ReplicaSet/api-gateway-7d8f9c5b6
--------------------------------------------------------------------------------
Container: [INIT] istio-init
2025-12-30T15:10:15Z Initializing iptables rules
2025-12-30T15:10:16Z iptables configuration completed
Container: api-gateway
2025-12-30T15:10:30Z Server started on port 8080
2025-12-30T15:11:00Z Processing request GET /api/v1/status
Container: istio-proxy
2025-12-30T15:10:20Z Envoy proxy initialized
2025-12-30T15:11:00Z [outbound] upstream connect to backend-service:8080与AI助手集成
Claude桌面配置
添加到您的Claude Desktop配置文件中:
MacOS: ~/Library/Application\ Support/Code/User/globalStorage/github.copilot-chat/mcp.json 视窗: %APPDATA%/Claude/claude_desktop_config.json
{
"mcpServers": {
"trace_gateway_failures": {
"command": "/Users/mani/workspace/trace-gateway-failures/mcp-server",
"args": [],
"env": {}
}
}
}工作流示例
- 请Claude调查网关故障:
"Check the logs for api-gateway and auth-service deployments in the production
namespace for any errors in the last 200 lines"- Claude将使用新格式的工具:
{
"namespaces": [
{
"name": "production",
"deployments": ["api-gateway", "auth-service"],
"tail_lines": 200
}
]
}- 跨多个命名空间分析结果:
"Compare the gateway logs between production and staging environments,
focusing on the api-gateway deployment"建筑
┌─────────────────────────────────────────────────────────┐
│ AI Assistant (Claude) │
└────────────────────┬────────────────────────────────────┘
│ MCP Protocol
│
┌────────────────────▼────────────────────────────────────┐
│ Trace Gateway Failures MCP Server │
│ ┌──────────────────────────────────────────────────┐ │
│ │ Tools: │ │
│ │ - get_pod_logs (namespace + deployment filter) │ │
│ │ - init container detection │ │
│ └──────────────────────────────────────────────────┘ │
└────────────────────┬────────────────────────────────────┘
│ kubectl commands
│
┌────────────────────▼────────────────────────────────────┐
│ Kubernetes Cluster │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Namespace 1 │ │ Namespace 2 │ │ Namespace N │ │
│ │ - Pods │ │ - Pods │ │ - Pods │ │
│ │ - Init Ctrs │ │ - Init Ctrs │ │ - Init Ctrs │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────┘发展
项目结构
trace-gateway-failures/
├── pkg/
│ └──mcp/
│ └── server.go
├── go.mod
├── main.go
└── README.md运行测试
# Run the server in debug mode
./mcp-server --transport stdio贡献
欢迎投稿!请随时提交拉取请求。
故障排除
未找到kubectl
确保 kubectl 已安装并在系统PATH中可用。
权限不足
验证您的Kubernetes配置是否具有读取Pod和日志的适当RBAC权限。
未检索到日志
- 检查指定命名空间中是否存在Pod
- 验证部署名称是否正确
- 确保Pod处于运行状态或已终止先前日志的容器
许可证
\[在此处添加您的许可证\]
作者
查尔拉马尼-
致谢
- 建于 模型上下文协议SDK
- Kubernetes日志检索由
kubectl
