Kubernetes集群管理服务器

一个生产就绪的Model Context Protocol (MCP)服务器,用于通过Claude和其他AI助手管理Kubernetes集群,提供20种全面的Kubernetes操作工具。

  • by pratik-mahalle
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 pratik-mahalle/k8s-mcp

使用说明

Kubernetes MCP服务器

一个生产就绪的模型上下文协议(MCP)服务器,用于通过Claude和其他AI助手管理Kubernetes集群。

概述

此MCP服务器为Kubernetes集群管理提供了20个全面的工具,使AI助手能够执行以下操作:

  • 管理上下文和集群
  • 列出和检查资源(Pod、部署、服务、节点)
  • 扩展部署
  • 查看日志
  • 监控事件
  • 还有更多!

特性

  • 20个Kubernetes工具 -全面覆盖常见的K8s操作
  • 多上下文支持 -在多个集群之间轻松切换
  • 双输出格式 -JSON用于解析,Markdown用于可读性
  • 错误处理 -带有建议的可操作错误消息
  • 类型安全 -带有Zod验证的完整TypeScript
  • 生产就绪 -RBAC感知,具有适当的错误处理能力

快速开始

看 QUICKSTART.md 获取5分钟的设置指南。

安装

npm install
npm run build

使用MCP检查员进行测试

npm run inspector

尝试跑步: k8s_list_contexts

与Claude Desktop集成

编辑您的 claude_desktop_config.json:

{
  "mcpServers": {
    "kubernetes": {
      "command": "node",
      "args": ["/absolute/path/to/k8s-mcp/dist/index.js"]
    }
  }
}

重启Claude Desktop,开始管理你的Kubernetes集群!

可用工具

集群和上下文管理(3个工具)

k8s_list_contexts

从kubeconfig中列出所有可用的Kubernetes上下文。

参数:

  • format (可选):输出格式- json 或 markdown (默认值: markdown)

例子:

List my Kubernetes contexts

k8s_set_context

切换到不同的Kubernetes上下文。

参数:

  • contextName (必填):要切换到的上下文的名称

例子:

Switch to the production context

k8s_get_cluster_info

获取有关当前群集的信息。

参数:

  • format (可选):输出格式- json 或 markdown (默认值: markdown)

例子:

Show me cluster information

命名空间管理(2个工具)

k8s_list_namespaces

列出群集中的所有命名空间。

参数:

  • format (可选):输出格式- json 或 markdown (默认值: markdown)

例子:

List all namespaces

k8s_create_namespace

创建一个新的命名空间。

参数:

  • name (必需):要创建的命名空间的名称

例子:

Create a namespace called staging

Pod管理(4个工具)

k8s_list_pods

列出命名空间中的Pod。

参数:

  • namespace (可选):要从中列出的命名空间(默认值: default)
  • labelSelector (可选):按标签过滤(例如。, app=nginx)
  • fieldSelector (可选):按字段过滤(例如。, status.phase=Running)
  • format (可选):输出格式- json 或 markdown (默认值: markdown)

示例:

List all pods in the default namespace
Show me failing pods in production
List pods with label app=nginx

k8s_get_pod

获取特定pod的详细信息。

参数:

  • name (必填):吊舱名称
  • namespace (可选):pod的命名空间(默认值: default)
  • format (可选):输出格式- json 或 markdown (默认值: markdown)

例子:

Show me details of pod my-app-12345

k8s_get_pod_logs

从pod中获取日志。

参数:

  • name (必填):吊舱名称
  • namespace (可选):pod的命名空间(默认值: default)
  • container (可选):多容器Pod的容器名称
  • tailLines (可选):尾部的行数
  • timestamps (可选):包括时间戳(默认值: false)
  • follow (可选):流日志(MCP中的有限支持)

示例:

Get logs from pod my-app-12345
Show me the last 100 lines of logs from pod my-app-12345

k8s_delete_pod

从集群中删除一个pod。

参数:

  • name (必填):要删除的pod的名称
  • namespace (可选):pod的命名空间(默认值: default)
  • gracePeriodSeconds (可选):强制删除前的宽限期

例子:

Delete pod my-app-12345

部署管理(5个工具)

k8s_list_部署

列出命名空间中的部署。

参数:

  • namespace (可选):要从中列出的命名空间(默认值: default)
  • labelSelector (可选):按标签筛选
  • format (可选):输出格式- json 或 markdown (默认值: markdown)

例子:

List all deployments in production

k8s_get_部署

获取有关部署的详细信息。

参数:

  • name (必填):部署名称
  • namespace (可选):部署的命名空间(默认值: default)
  • format (可选):输出格式- json 或 markdown (默认值: markdown)

例子:

Show me details of deployment my-app

k8s_scale_部署

将部署扩展到特定数量的副本。

参数:

  • name (必填):部署名称
  • namespace (可选):部署的命名空间(默认值: default)
  • replicas (必填):所需的副本数量

例子:

Scale my-app deployment to 5 replicas

k8s_重新启动_部署

重新启动部署(触发滚动重新启动)。

参数:

  • name (必填):部署名称
  • namespace (可选):部署的命名空间(默认值: default)

例子:

Restart the my-app deployment

k8s_回滚_部署

将部署回滚到以前的版本。

参数:

  • name (必填):部署名称
  • namespace (可选):部署的命名空间(默认值: default)
  • toRevision (可选):要回滚到的特定修订

例子:

Rollback my-app deployment

服务管理(2个工具)

k8s_list_服务

列出命名空间中的服务。

参数:

  • namespace (可选):要从中列出的命名空间(默认值: default)
  • labelSelector (可选):按标签筛选
  • format (可选):输出格式- json 或 markdown (默认值: markdown)

例子:

List all services in the default namespace

k8s_get_service

获取有关服务的详细信息。

参数:

  • name (必填):服务名称
  • namespace (可选):服务的命名空间(默认值: default)
  • format (可选):输出格式- json 或 markdown (默认值: markdown)

例子:

Show me details of service my-service

节点管理(2个工具)

k8s_list_nodes

列出群集中的所有节点。

参数:

  • labelSelector (可选):按标签筛选
  • format (可选):输出格式- json 或 markdown (默认值: markdown)

例子:

List all nodes
Show me nodes that are not ready

k8s_get_node

获取节点的详细信息。

参数:

  • name (必填):节点名称
  • format (可选):输出格式- json 或 markdown (默认值: markdown)

例子:

Show me details of node worker-1

事件和监控(2个工具)

k8s_get_events

获取用于调试的群集事件。

参数:

  • namespace (可选):从中获取事件的命名空间(全部省略)
  • fieldSelector (可选):按字段筛选
  • limit (可选):最大事件数
  • format (可选):输出格式- json 或 markdown (默认值: markdown)

例子:

Show me recent events in the production namespace
Get the last 50 cluster events

k8s_get_resource_status

获取特定资源的状态。

参数:

  • resourceType (必填):类型- pod, deployment, service,或 node
  • name (必填):资源名称
  • namespace (可选):命名空间(不适用于节点)
  • format (可选):输出格式- json 或 markdown (默认值: markdown)

例子:

What's the status of deployment my-app?
Check the status of pod my-pod-12345

配置

服务器遵循标准的Kubernetes配置:

  • KUBECONFIG:kubeconfig文件的路径(默认: ~/.kube/config)
  • 支持kubeconfig中定义的所有上下文
  • 尊重RBAC权限

可选环境变量(创建 .env 文件):

# Default context
K8S_CONTEXT=minikube

# Default namespace
K8S_NAMESPACE=default

# Output format
OUTPUT_FORMAT=markdown

# Log level
LOG_LEVEL=info

与克劳德的对话示例

一旦与Claude Desktop集成,您就可以进行自然对话:

You: "What Kubernetes contexts do I have?"
Claude: [Lists all contexts with current one highlighted]

You: "Switch to production and show me all failing pods"
Claude: [Switches context and shows pods with issues]

You: "Get logs from pod xyz"
Claude: [Shows recent logs from the pod]

You: "Scale the api deployment to 10 replicas"
Claude: [Scales the deployment and confirms]

You: "Show me cluster events from the last hour"
Claude: [Displays recent events]

建筑

k8s-mcp/
├── src/
│   ├── index.ts              # MCP server entry point
│   ├── types.ts              # TypeScript types and Zod schemas
│   ├── k8s-client.ts         # Kubernetes client wrapper
│   └── tools/
│       ├── formatters.ts     # Output formatting utilities
│       ├── context-tools.ts  # Context management tools
│       ├── namespace-tools.ts # Namespace tools
│       ├── pod-tools.ts      # Pod management tools
│       ├── deployment-tools.ts # Deployment tools
│       ├── service-tools.ts  # Service tools
│       ├── node-tools.ts     # Node tools
│       └── event-tools.ts    # Events and monitoring tools
├── dist/                     # Compiled JavaScript
├── package.json
├── tsconfig.json
└── README.md

发展

构建

npm run build

观看模式

npm run watch

检验员测试

npm run inspector

这将打开一个交互式界面来测试所有工具。

故障排除

“加载kubeconfig失败”

  • 确保 kubectl 已安装并配置
  • 检查一下 ~/.kube/config 存在
  • 验证您是否可以运行 kubectl get pods

“权限被拒绝”错误

  • 检查您在群集中的RBAC权限
  • 确保您的服务帐户具有必要的权限
  • 请联系您的群集管理员

“找不到上下文”

  • 列出可用上下文 k8s_list_contexts
  • 确保上下文存在于kubeconfig中
  • 尝试 kubectl config get-contexts 验证

“找不到资源”

  • 验证资源名称和命名空间
  • 检查您是否使用了正确的上下文
  • 使用kubectl确保资源存在

安全考虑

  • 服务器尊重kubeconfig中的RBAC权限
  • MCP服务器不存储凭据
  • 所有身份验证都使用您现有的Kubernetes凭据
  • 记录操作以供审计

需求

  • Node.js>=18.0.0
  • kubectl配置了有效凭据
  • 访问Kubernetes集群

贡献

欢迎投稿!请随时提交问题或拉取请求。

许可证

麻省理工学院

相关文件

支持

对于问题、疑问或贡献,请在GitHub上打开问题。