Skip to main content
Glama
Krishna1704M

mcp-k8s-context-server

by Krishna1704M

MCP K8s 上下文服务器

一个FastMCP服务器,将Kubernetes暴露为一组只读工具,可供LLM使用,同时提供用于Pod健康分析和资源趋势跟踪的分析层。


功能

工具

描述

list_pods(namespace)

列出命名空间中的Pod,包含阶段和重启信息

get_pod_status(pod_name, namespace)

详细的Pod状态、条件和容器状态

get_pod_logs(pod_name, namespace, tail_lines)

获取Pod最近的日志

get_deployment_manifest(deployment_name, namespace)

完整的部署规格(JSON格式)

analyze_pod_health(namespace, hours)

分析:扫描日志,检测错误模式,对不健康的Pod进行排名

get_resource_trends(deployment_name, namespace)

分析:通过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系统了解正在发生的事情。它没有正当理由创建、修改或删除任何资源。

授予的权限

资源

动词

原因

pods

get, list, watch

list_pods, get_pod_status, analyze_pod_health

pods/log

get, list, watch

get_pod_logs, analyze_pod_health

deployments

get, list, watch

get_deployment_manifest, get_resource_trends

metrics.k8s.io/pods

get, list

get_resource_trends (Metrics API)

明确未授予的权限

动词

排除原因

create

没有工具创建任何资源

update / patch

没有工具修改任何资源

delete / deletecollection

如果误用后果严重;没有只读工具需要它

escalate / bind

防止权限提升

这意味着,一个被入侵的MCP服务器不能删除Pod、将部署缩容到零、修改密钥或影响任何正在运行的工作负载。被入侵MCP服务器的爆炸半径仅限于读取信息——不会造成破坏。


分析层

analyze_pod_health

  1. 列出命名空间中的所有Pod。

  2. 每个Pod获取最多500行日志。

  3. 针对已知故障指示器目录进行模式匹配:

    • OOMKilled, CrashLoopBackOff

    • Python/Java异常(Traceback, RuntimeError等)

    • Panic, SIGSEGV/SIGKILL, 连接错误, 权限拒绝

    • 存活/就绪探针失败

  4. 计算每个Pod的健康分数(越低越差)。

  5. 按最差到最好的顺序返回Pod,并附有错误频率计数。

  6. 将结果持久化到SQLite,用于历史分析。

  1. 从Deployment规约中解析Pod选择器。

  2. 从Pod规约中读取资源限制

  3. 查询Kubernetes Metrics APImetrics.k8s.io/v1beta1)获取实时CPU/内存数据。

  4. 计算:CPU和内存的平均值、峰值以及占限制的百分比。

  5. 将每个快照持久化mcp_analytics.db,以便在多次调用中积累趋势。

需要metrics-server插件:minikube addons enable metrics-server


CI / 持续集成

GitHub Actions工作流(.github/workflows/ci.yml)在每次推送和PR上运行:

  1. Docker构建 — 构建镜像但不推送(验证Dockerfile和依赖项)。

  2. kubeconform — 针对Kubernetes 1.29模式以严格模式验证所有k8s/*.yaml清单。

  3. ruff — 对k8s_mcp_server.py进行Python错误和风格检查。


环境变量

变量

默认值

描述

MCP_DB_PATH

mcp_analytics.db

SQLite分析数据库的路径


许可证

MIT

F
license - not found
-
quality - not tested
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

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

View all related MCP servers

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.

View all MCP Connectors

Latest Blog Posts

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