/ 目錄 / 演練場 / k8s-mcp-server
● 社群 alexei-led ⚡ 即開即用

k8s-mcp-server

作者 alexei-led · alexei-led/k8s-mcp-server

Give Claude kubectl, helm, istioctl, and argocd — with command validation, non-root execution, and scoped kubeconfig — so you can ops your cluster by chat.

k8s-mcp-server runs as a Docker container that mounts your kubeconfig read-only. It wraps kubectl/helm/istioctl/argocd plus Unix utilities (jq, grep, sed) for pipelines. Two security modes (default restrictive, permissive) and a command validator so Claude can't accidentally kubectl delete namespace your prod.

為什麼要用

核心特性

即時演示

實際使用效果

k8s.replay ▶ 就緒
0/0

安裝

選擇你的客戶端

~/Library/Application Support/Claude/claude_desktop_config.json  · Windows: %APPDATA%\Claude\claude_desktop_config.json
{
  "mcpServers": {
    "k8s": {
      "command": "uvx",
      "args": [
        "k8s-mcp-server"
      ],
      "_inferred": true
    }
  }
}

開啟 Claude Desktop → Settings → Developer → Edit Config。儲存後重啟應用。

~/.cursor/mcp.json · .cursor/mcp.json
{
  "mcpServers": {
    "k8s": {
      "command": "uvx",
      "args": [
        "k8s-mcp-server"
      ],
      "_inferred": true
    }
  }
}

Cursor 使用與 Claude Desktop 相同的 mcpServers 格式。專案級設定優先於全域。

VS Code → Cline → MCP Servers → Edit
{
  "mcpServers": {
    "k8s": {
      "command": "uvx",
      "args": [
        "k8s-mcp-server"
      ],
      "_inferred": true
    }
  }
}

點擊 Cline 側欄中的 MCP Servers 圖示,然後選 "Edit Configuration"。

~/.codeium/windsurf/mcp_config.json
{
  "mcpServers": {
    "k8s": {
      "command": "uvx",
      "args": [
        "k8s-mcp-server"
      ],
      "_inferred": true
    }
  }
}

格式與 Claude Desktop 相同。重啟 Windsurf 生效。

~/.continue/config.json
{
  "mcpServers": [
    {
      "name": "k8s",
      "command": "uvx",
      "args": [
        "k8s-mcp-server"
      ]
    }
  ]
}

Continue 使用伺服器物件陣列,而非映射。

~/.config/zed/settings.json
{
  "context_servers": {
    "k8s": {
      "command": {
        "path": "uvx",
        "args": [
          "k8s-mcp-server"
        ]
      }
    }
  }
}

加入 context_servers。Zed 儲存後熱重載。

claude mcp add k8s -- uvx k8s-mcp-server

一行命令搞定。用 claude mcp list 驗證,claude mcp remove 移除。

使用場景

實戰用法: k8s-mcp-server

Morning cluster health check via chat

👤 SRE, platform engineers ⏱ ~5 min intermediate

何時使用: Daily, before stand-up — anything red? Any pods crashlooping?

前置條件
  • kubeconfig on host — Usually ~/.kube/config
步驟
  1. Start the MCP container
    Start k8s-mcp-server via Docker mounting ~/.kube read-only.✓ 已複製
    → Container up
  2. Run the checks
    Across all namespaces, list pods not in Running state, nodes not Ready, and recent (last 1h) events of type Warning.✓ 已複製
    → Three-part health report
  3. Triage
    For each crashlooping pod, get last 50 log lines and propose most likely cause.✓ 已複製
    → Log excerpts + diagnoses

結果: 5-minute daily cluster pulse check.

注意事項
  • Running kubectl get events cluster-wide floods context — Always filter by time and type

Debug a failed Helm release

👤 Devs shipping Helm charts ⏱ ~15 min intermediate

何時使用: helm upgrade failed and you want to know why without copying error messages back and forth.

步驟
  1. Get history
    helm history my-app -n production. What changed between v4 (working) and v5 (failed)?✓ 已複製
    → Version diff
  2. Diff templates
    Render the template for v5 with current values and compare against v4. Highlight differences.✓ 已複製
    → Diff of rendered manifests
  3. Roll back if needed
    Roll back to v4. Confirm all pods become Ready.✓ 已複製
    → Rollback + health check

結果: Fast recovery with understanding, not just luck.

注意事項
  • Rolling back without capturing the failure first means you can't fix forward — Always save helm get all v5 to a file before rolling back
搭配使用: filesystem

Audit Argo CD apps for drift

👤 GitOps practitioners ⏱ ~20 min advanced

何時使用: You suspect someone kubectl'd something into prod that Argo CD doesn't know about.

步驟
  1. List out-of-sync apps
    argocd app list --output json. Filter to apps where sync status isn't Synced.✓ 已複製
    → Out-of-sync list
  2. Diff each
    For each drifted app, run argocd app diff and summarize what's different from Git.✓ 已複製
    → Per-app diff summary

結果: Clear picture of manual drift to reconcile.

組合

與其他 MCP 搭配,撬動十倍槓桿

k8s + github

When an app is broken in cluster, open GitHub issue with pod logs

If any pod in 'payments' namespace is crashlooping, fetch logs and open a GitHub issue with them attached.✓ 已複製

工具

此 MCP 暴露的能力

工具輸入參數何時呼叫成本
kubectl command: str (full kubectl args) General cluster ops free
helm command: str Release management free
istioctl command: str Service mesh ops free
argocd command: str GitOps ops (requires argocd login in container) free

成本與限制

運行它的成本

API 配額
None
每次呼叫 Token 數
kubectl get -o yaml can be huge; prefer -o json | jq to trim
費用
Free, MIT
提示
Always use -o wide or -o jsonpath=... to trim. kubectl get all is a token-bomb.

安全

權限、密鑰、影響範圍

最小權限: Kubeconfig scoped to a least-privilege ServiceAccount
憑證儲存: Mount ~/.kube read-only into the container
資料出站: Traffic to your cluster API server only
切勿授予: cluster-admin ServiceAccount to the kubeconfig used by the MCP

故障排查

常見錯誤與修復

The connection to the server localhost:8080 was refused

kubeconfig not mounted or KUBECONFIG env var wrong in the container.

驗證: docker exec ... kubectl config view
Command blocked in restrictive mode

Either craft a narrower command, or explicitly opt into permissive mode for that session.

Timeout on long-running command

Raise K8S_MCP_TIMEOUT. For truly long ops, run async via a Job and poll.

替代方案

k8s-mcp-server 對比其他方案

替代方案何時用它替代權衡
Direct kubectl via ssh-managerYou prefer running kubectl on a jump hostLess structured; no command validation
Kagent / AIOps SaaS MCPsYou want managed service rather than self-hostedCosts money; less control

更多

資源

📖 閱讀 GitHub 上的官方 README

🐙 查看未解決的 issue

🔍 瀏覽全部 400+ MCP 伺服器和 Skills