k8s-mcp-server
K8s 集群运维 MCP Server
基于 FastMCP 的 Kubernetes 运维查询 MCP 服务。把 kubectl get / describe / logs / events --watch 等日常排查动作封装成 LLM 可调用的只读工具,让 Claude 等客户端直接读集群状态、快速定位故障。
本项目是底层 MCP 能力层。上层基于 LangChain / LangGraph 的智能运维 Agent(K8s Ops Agent),通过 MCP 协议接入本 Server,用 LLM 编排工具实现「一句话完成集群排障」,支持 CLI 与飞书机器人双端交互,代码在独立仓库 LangChain_bot。
当前版本只暴露只读工具(无 create/update/delete)。后续会逐步加入写操作(create / update / delete / exec / scale),届时会同步补齐安全机制(context 白名单、Secret 遮蔽等),见下文「安全说明」。
工具清单
工具 | 说明 | 对应 kubectl |
| 节点列表概览 |
|
| 节点详情 |
|
| 节点资源请求/限制汇总与过载评估 |
|
| 扫描问题节点(NotReady / 各种 Pressure) | — |
| Pod 列表(支持 namespace / label / field 过滤) |
|
| Pod 详情(支持 label / workload / 名称前缀定位) |
|
| 读取 Pod 容器日志 |
|
| 查询集群事件(去重 + 排序) |
|
| 实时监听事件流 |
|
| 实时监听节点状态变化 |
|
| 实时监听 Pod 状态变化 |
|
另有 @mcp.prompt(节点排查 / 集群巡检引导)和 @mcp.resource(k8s://cluster/summary 集群摘要)两类附加能力。
Related MCP server: k8s-readonly-mcp
架构图
flowchart TB
subgraph LB["LangChain_bot(自研 Agent)"]
CLI["CLI 终端"] --> LG["LangGraph Agent"]
Lark["飞书机器人"] --> LG
LG --> LLM["LLM(OpenAI 兼容)"]
LG --> Mem["多轮记忆 MemorySaver"]
LG --> Builtin["内置工具(自动发现)"]
end
subgraph IDE["第三方 AI IDE"]
Trae["Trae / Cursor / Claude Code"]
end
subgraph Server["k8s-mcp-server(本仓库 · FastMCP)"]
Entry["k8s_server.py<br/>入口:导入即注册"]
App["app.py<br/>FastMCP 实例(叶子模块)"]
Tools["Tools/ · 11 个只读工具"]
Prompts["prompts.py · 排查引导"]
Resources["resources.py · 集群摘要"]
Utils["utils/ · 解析 + watch 循环"]
KC["k8s_client.py<br/>懒加载客户端(单一配置来源)"]
end
subgraph Cluster["Kubernetes 集群"]
API["API Server"]
Obj["Node / Pod / Event"]
end
LG ==>|"MCP 协议<br/>stdio / http"| Entry
Trae ==>|"MCP 协议<br/>stdio / http"| Entry
Entry --> App
App --> Tools
App --> Prompts
App --> Resources
Tools --> Utils
Tools --> KC
KC ==>|"kubernetes SDK<br/>(只读查询)"| API
API --> Obj分层说明:
层 | 模块 | 职责 |
传输层 |
| 按 |
应用层 |
| FastMCP 实例 + Prompt 引导 + Resource 资源 |
工具层 |
| 11 个只读工具,每个一个文件, |
解析层 |
| 纯解析 / 状态快照对比 / 通用 watch 循环,无 K8s 与 MCP 依赖 |
连接层 |
| K8s 客户端懒加载,连接优先级 in-cluster → 显式 kubeconfig → 标准解析链 |
项目演示
底层 MCP Server 采用标准 MCP 协议,可被任意 MCP 客户端复用。下面按「第三方 IDE → 自研 Agent → 团队协作」的顺序,展示接入后的实际对话效果。
接入 Trae AI IDE


接入自研 LangChain Agent —— CLI 对话

接入飞书机器人 —— 团队协作

目录结构
├── app.py # FastMCP 实例(叶子模块,破循环导入)
├── k8s_server.py # 入口:导入 Tools 即自动注册全部工具
├── k8s_client.py # K8s 客户端懒加载 + 单一配置来源
├── config.py # 12-Factor 环境变量配置
├── prompts.py # @mcp.prompt 引导
├── resources.py # @mcp.resource 资源
├── Tools/ # 每个工具一个文件,导入即注册
├── utils/ # 纯解析/辅助模块(无 K8s/MCP 依赖)
└── tests/ # 单元测试安装与运行
# 1. 创建虚拟环境并安装依赖
python -m venv .venv
# Windows
.venv\Scripts\activate
# Linux / macOS
source .venv/bin/activate
pip install -r requirements.txt
# 2. 运行(HTTP transport,默认 127.0.0.1:8081)
python k8s_server.py运行测试
pip install -r requirements-dev.txt
python -m pytest -q配置
全部通过环境变量配置,带默认值(见 config.py):
变量 | 默认值 | 说明 |
|
| 显式指定 kubeconfig 路径;留空走官方解析链( |
|
| 是否校验 TLS 证书;仅在调试自签且明确风险时置 |
|
| MCP 服务名 |
|
| 传输方式, |
|
| 监听地址 |
|
| 监听端口 |
K8s 连接配置加载优先级:in-cluster(Pod 内 SA)→ 显式 KUBECONFIG_PATH → 官方 kubeconfig 解析链,只取一份配置,不从多个文件拼装。
接入 MCP 客户端
以 Claude Code 为例(HTTP transport 已在 127.0.0.1:8081 运行时):
{
"mcpServers": {
"k8s-node-server": {
"url": "http://127.0.0.1:8081/mcp"
}
}
}安全说明
只读工具:本项目只暴露只读操作(list / get / describe / logs / events),不提供任何变更操作(create / update / delete)。生产部署建议使用只读 RBAC,仅授予 get / list / watch 权限。
默认监听本机:
MCP_HOST默认127.0.0.1,服务仅对本机可访问。HTTP transport 不提供认证与限流,如需对外暴露,应在反向代理层增加 TLS 与 Token / OIDC 认证。TLS 证书校验:默认开启(
K8S_VERIFY_SSL=true)。false仅用于调试自签证书,生产环境保持开启。凭据不入库:kubeconfig 文件包含集群凭据,已通过
.gitignore中的kubeconfig、kubeconfig.*规则排除在版本控制之外。
已知边界
所有工具为只读,不含变更操作(这是刻意设计)。
watch_*系列受全局并发锁限制:同一时刻只允许一个 watch 运行,避免给 API Server 加压。事件/日志输出有长度上限,超大内容会被截断(见
read_pod_log的截断逻辑与query_events的max_count)。
This server cannot be deployed
Maintenance
Related MCP Connectors
Provides read access to your GKE and Kubernetes resources.
The Google GKE MCP server is a managed Model Context Protocol server that provides AI applications with tools to manage Google Kubernetes Engine (GKE) clusters and Kubernetes resources. It exposes a structured, discoverable interface that allows AI agents to interact with GKE and Kubernetes APIs, enabling them to inspect cluster configurations, retrieve Kubernetes resource YAMLs, monitor operations like cluster upgrades, diagnose issues, and optimize costs—all without needing to parse text output or use complex kubectl commands.
Read-only MCP tools for AI agent discovery, structured resources, and NIULAI information.
Read-only MCP access to a documented IT fleet: state, changes, posture. 15 tools.
Related MCP Servers
- AlicenseAqualityAmaintenanceProvides a set of read-only Kubernetes functions via an MCP server, enabling interaction with Kubernetes clusters through agents or coding assistants like GitHub Copilot.21406 PyPI3Apache 2.0
- AlicenseAqualityDmaintenanceA read-only MCP server for inspecting Kubernetes clusters, allowing LLMs to list resources, describe pods, and read logs without mutation.5MIT
- FlicenseNot gradedqualityCmaintenanceProvides a read-only interface to Kubernetes clusters, enabling LLMs to list pods, get pod status and logs, fetch deployment manifests, and perform pod health analysis with resource trend tracking.-
- AlicenseNot gradedqualityBmaintenanceEnables read-only Kubernetes cluster diagnostics through MCP tools, allowing a local LLM to inspect pods and events and debug issues via natural language queries.MIT