mcp-k8s-context-server
MCP K8s 上下文服务器
一个FastMCP服务器,将Kubernetes暴露为一组只读工具,可供LLM使用,同时提供用于Pod健康分析和资源趋势跟踪的分析层。
功能
工具 | 描述 |
| 列出命名空间中的Pod,包含阶段和重启信息 |
| 详细的Pod状态、条件和容器状态 |
| 获取Pod最近的日志 |
| 完整的部署规格(JSON格式) |
| 分析:扫描日志,检测错误模式,对不健康的Pod进行排名 |
| 分析:通过Metrics API获取CPU/内存数据,并持久化到SQLite历史记录中 |
Related MCP server: Kube MCP
项目结构
mcp-k8s-context-server/
├── k8s_mcp_server.py # FastMCP server (all tools)
├── requirements.txt # Python dependencies
├── Dockerfile # Container image definition
├── k8s/
│ ├── serviceaccount.yaml # ServiceAccount + Namespace
│ ├── role.yaml # Least-privilege ClusterRole (read-only)
│ ├── rolebinding.yaml # ClusterRoleBinding
│ └── deployment.yaml # Deployment + Service
└── .github/
└── workflows/
└── ci.yml # Build + kubeconform validation本地开发
# Create and activate a virtual environment
python -m venv .venv && source .venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Run with local kubeconfig (falls back automatically from in-cluster config)
python k8s_mcp_server.py集群内部署(minikube)
前提条件
# Install minikube, kubectl, docker
minikube version # >= 1.32
kubectl version # >= 1.28
docker version # >= 24步骤1 — 启动minikube
minikube start --cpus=2 --memory=4096步骤2 — 启用metrics-server(get_resource_trends所需)
minikube addons enable metrics-server步骤3 — 构建镜像并加载到minikube中
# Build locally
docker build -t mcp-k8s-server:latest .
# Load into minikube's image registry (no registry push needed)
minikube image load mcp-k8s-server:latest
# Verify the image is available
minikube image ls | grep mcp-k8s-server步骤4 — 应用Kubernetes清单
# Apply in dependency order: SA → Role → Binding → Deployment
kubectl apply -f k8s/serviceaccount.yaml
kubectl apply -f k8s/role.yaml
kubectl apply -f k8s/rolebinding.yaml
kubectl apply -f k8s/deployment.yaml步骤5 — 验证Pod是否运行
kubectl get pods -n mcp-system
# Expected:
# NAME READY STATUS RESTARTS AGE
# mcp-k8s-server-xxxxxxxxx-xxxxx 1/1 Running 0 30s
kubectl logs -n mcp-system deploy/mcp-k8s-server
# Expected: "Using in-cluster Kubernetes config (ServiceAccount token)"步骤6 — 在集群内测试只读工具
# Port-forward to access the server from your laptop
kubectl port-forward -n mcp-system svc/mcp-k8s-server 8000:8000 &
# Create a test pod to query
kubectl run nginx-test --image=nginx --restart=Never
# Test list_pods
curl -s http://localhost:8000/tools/list_pods \
-H 'Content-Type: application/json' \
-d '{"namespace":"default"}' | jq .
# Test get_pod_logs
curl -s http://localhost:8000/tools/get_pod_logs \
-H 'Content-Type: application/json' \
-d '{"pod_name":"nginx-test","namespace":"default","tail_lines":20}' | jq .
# Test analyze_pod_health
curl -s http://localhost:8000/tools/analyze_pod_health \
-H 'Content-Type: application/json' \
-d '{"namespace":"default","hours":1}' | jq .步骤7 — 证明RBAC阻止写操作
该ServiceAccount没有写动词。为确认这一点:
# Exec into the pod and try to delete another pod using the SA token
MCP_POD=$(kubectl get pod -n mcp-system -l app=mcp-k8s-server -o jsonpath='{.items[0].metadata.name}')
kubectl exec -n mcp-system $MCP_POD -- \
kubectl delete pod nginx-test --namespace=default \
--token=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token) \
--server=https://kubernetes.default.svc \
--certificate-authority=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt预期输出:
Error from server (Forbidden): pods "nginx-test" is forbidden:
User "system:serviceaccount:mcp-system:mcp-server-sa" cannot delete
resource "pods" in API group "" in the namespace "default"来自API服务器的403 Forbidden响应就是RBAC作用域生效的实时证明——ServiceAccount可以读取但不能修改任何资源。
RBAC最小权限设计
设计理念
只授予所需权限,明确拒绝所有其他操作。
MCP服务器是一个可观测性工具——它读取集群状态,帮助操作员和AI系统了解正在发生的事情。它没有正当理由创建、修改或删除任何资源。
授予的权限
资源 | 动词 | 原因 |
|
|
|
|
|
|
|
|
|
|
|
|
明确未授予的权限
动词 | 排除原因 |
| 没有工具创建任何资源 |
| 没有工具修改任何资源 |
| 如果误用后果严重;没有只读工具需要它 |
| 防止权限提升 |
这意味着,一个被入侵的MCP服务器不能删除Pod、将部署缩容到零、修改密钥或影响任何正在运行的工作负载。被入侵MCP服务器的爆炸半径仅限于读取信息——不会造成破坏。
分析层
analyze_pod_health
列出命名空间中的所有Pod。
每个Pod获取最多500行日志。
针对已知故障指示器目录进行模式匹配:
OOMKilled,CrashLoopBackOffPython/Java异常(Traceback, RuntimeError等)
Panic, SIGSEGV/SIGKILL, 连接错误, 权限拒绝
存活/就绪探针失败
计算每个Pod的健康分数(越低越差)。
按最差到最好的顺序返回Pod,并附有错误频率计数。
将结果持久化到SQLite,用于历史分析。
get_resource_trends
从Deployment规约中解析Pod选择器。
从Pod规约中读取资源限制。
查询Kubernetes Metrics API(
metrics.k8s.io/v1beta1)获取实时CPU/内存数据。计算:CPU和内存的平均值、峰值以及占限制的百分比。
将每个快照持久化到
mcp_analytics.db,以便在多次调用中积累趋势。
需要
metrics-server插件:minikube addons enable metrics-server
CI / 持续集成
GitHub Actions工作流(.github/workflows/ci.yml)在每次推送和PR上运行:
Docker构建 — 构建镜像但不推送(验证Dockerfile和依赖项)。
kubeconform — 针对Kubernetes 1.29模式以严格模式验证所有
k8s/*.yaml清单。ruff — 对
k8s_mcp_server.py进行Python错误和风格检查。
环境变量
变量 | 默认值 | 描述 |
|
| SQLite分析数据库的路径 |
许可证
MIT
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Servers
- Alicense-qualityBmaintenanceProvides read-only access to Kubernetes clusters for AI assistants.23MIT
- AlicenseAqualityCmaintenanceEnables AI assistants to interact with and manage Kubernetes clusters, supporting operations on pods, deployments, services, configmaps, secrets, namespaces, metrics, and events with built-in safety features for destructive actions.9141MIT
- AlicenseAqualityAmaintenanceEnables safe, read-only interaction with Kubernetes clusters, allowing users to list resources and fetch logs without any create/update/delete operations.116Apache 2.0
- Flicense-qualityCmaintenanceExposes Kubernetes cluster management tools to LLMs, enabling querying pods, deployments, logs, metrics, and managing port forwards via natural language.1
Related MCP Connectors
Read-only bank access for your AI agent. Connects Claude, ChatGPT, Cursor, Gemini, Codex.
Read-only access to Auralogs production logs: search logs, inspect errors, review AI analyses.
Operate Linux, macOS and Windows from your LLM. Every action runs through an auditable allowlist.
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/Krishna1704M/mcp-k8s-context-server'
If you have feedback or need assistance with the MCP directory API, please join our Discord server