mcp-server-kubernetes

mcp-server-kubernetes

以下是用 TypeScript 实现 Kubernetes 集群操作,包括 Pod、Deployment 和 Service 的示例: **总览:** 这个翻译提供了一个概括性的描述,因为完整的代码实现会非常庞大。 它涵盖了使用 Kubernetes 客户端库(例如 `@kubernetes/client-node`)在 TypeScript 中执行常见操作的关键概念和步骤。 **翻译:** **Kubernetes 集群操作的 TypeScript 实现,用于 Pod、Deployment 和 Service。** **TypeScript 实现 Kubernetes 集群中 Pod、Deployment 和 Service 的操作。** **更详细的解释 (更适合作为文档或教程的标题):** **使用 TypeScript 操作 Kubernetes 集群:Pod、Deployment 和 Service 的实现示例。** **以下是一个更详细的解释,包含代码示例的结构 (需要更多上下文才能准确翻译):** **假设我们使用 `@kubernetes/client-node` 库。** **1. 安装依赖:** ```bash npm install @kubernetes/client-node ``` **2. 初始化 Kubernetes 客户端:** ```typescript import * as k8s from '@kubernetes/client-node'; const kc = new k8s.KubeConfig(); kc.loadFromDefault(); // 或者使用其他方式加载配置,例如 loadFromCluster() 或 loadFromString() const k8sApi = kc.makeApiClient(k8s.CoreV1Api); const k8sAppsApi = kc.makeApiClient(k8s.AppsV1Api); ``` **3. Pod 操作示例:** * **列出 Pod:** ```typescript async function listPods(namespace: string) { const res = await k8sApi.listNamespacedPod(namespace); console.log(res.body); } ``` * **创建 Pod:** ```typescript async function createPod(namespace: string, podManifest: k8s.V1Pod) { const res = await k8sApi.createNamespacedPod(namespace, podManifest); console.log(res.body); } ``` * **删除 Pod:** ```typescript async function deletePod(namespace: string, podName: string) { const res = await k8sApi.deleteNamespacedPod(podName, namespace); console.log(res.body); } ``` **4. Deployment 操作示例:** * **列出 Deployment:** ```typescript async function listDeployments(namespace: string) { const res = await k8sAppsApi.listNamespacedDeployment(namespace); console.log(res.body); } ``` * **创建 Deployment:** ```typescript async function createDeployment(namespace: string, deploymentManifest: k8s.V1Deployment) { const res = await k8sAppsApi.createNamespacedDeployment(namespace, deploymentManifest); console.log(res.body); } ``` * **更新 Deployment:** ```typescript async function updateDeployment(namespace: string, deploymentName: string, deploymentManifest: k8s.V1Deployment) { const res = await k8sAppsApi.replaceNamespacedDeployment(deploymentName, namespace, deploymentManifest); console.log(res.body); } ``` * **删除 Deployment:** ```typescript async function deleteDeployment(namespace: string, deploymentName: string) { const res = await k8sAppsApi.deleteNamespacedDeployment(deploymentName, namespace); console.log(res.body); } ``` **5. Service 操作示例:** * **列出 Service:** ```typescript async function listServices(namespace: string) { const res = await k8sApi.listNamespacedService(namespace); console.log(res.body); } ``` * **创建 Service:** ```typescript async function createService(namespace: string, serviceManifest: k8s.V1Service) { const res = await k8sApi.createNamespacedService(namespace, serviceManifest); console.log(res.body); } ``` * **更新 Service:** ```typescript async function updateService(namespace: string, serviceName: string, serviceManifest: k8s.V1Service) { const res = await k8sApi.replaceNamespacedService(serviceName, namespace, serviceManifest); console.log(res.body); } ``` * **删除 Service:** ```typescript async function deleteService(namespace: string, serviceName: string) { const res = await k8sApi.deleteNamespacedService(serviceName, namespace); console.log(res.body); } ``` **翻译 (对应上面的详细解释):** **使用 TypeScript 实现 Kubernetes 集群操作:Pod、Deployment 和 Service 的示例** **1. 安装依赖:** ```bash npm install @kubernetes/client-node ``` **2. 初始化 Kubernetes 客户端:** ```typescript import * as k8s from '@kubernetes/client-node'; const kc = new k8s.KubeConfig(); kc.loadFromDefault(); // 或者使用其他方式加载配置,例如 loadFromCluster() 或 loadFromString() const k8sApi = kc.makeApiClient(k8s.CoreV1Api); const k8sAppsApi = kc.makeApiClient(k8s.AppsV1Api); ``` **3. Pod 操作示例:** * **列出 Pod:** ```typescript async function listPods(namespace: string) { const res = await k8sApi.listNamespacedPod(namespace); console.log(res.body); } ``` * **创建 Pod:** ```typescript async function createPod(namespace: string, podManifest: k8s.V1Pod) { const res = await k8sApi.createNamespacedPod(namespace, podManifest); console.log(res.body); } ``` * **删除 Pod:** ```typescript async function deletePod(namespace: string, podName: string) { const res = await k8sApi.deleteNamespacedPod(podName, namespace); console.log(res.body); } ``` **4. Deployment 操作示例:** * **列出 Deployment:** ```typescript async function listDeployments(namespace: string) { const res = await k8sAppsApi.listNamespacedDeployment(namespace); console.log(res.body); } ``` * **创建 Deployment:** ```typescript async function createDeployment(namespace: string, deploymentManifest: k8s.V1Deployment) { const res = await k8sAppsApi.createNamespacedDeployment(namespace, deploymentManifest); console.log(res.body); } ``` * **更新 Deployment:** ```typescript async function updateDeployment(namespace: string, deploymentName: string, deploymentManifest: k8s.V1Deployment) { const res = await k8sAppsApi.replaceNamespacedDeployment(deploymentName, namespace, deploymentManifest); console.log(res.body); } ``` * **删除 Deployment:** ```typescript async function deleteDeployment(namespace: string, deploymentName: string) { const res = await k8sAppsApi.deleteNamespacedDeployment(deploymentName, namespace); console.log(res.body); } ``` **5. Service 操作示例:** * **列出 Service:** ```typescript async function listServices(namespace: string) { const res = await k8sApi.listNamespacedService(namespace); console.log(res.body); } ``` * **创建 Service:** ```typescript async function createService(namespace: string, serviceManifest: k8s.V1Service) { const res = await k8sApi.createNamespacedService(namespace, serviceManifest); console.log(res.body); } ``` * **更新 Service:** ```typescript async function updateService(namespace: string, serviceName: string, serviceManifest: k8s.V1Service) { const res = await k8sApi.replaceNamespacedService(serviceName, namespace, serviceManifest); console.log(res.body); } ``` * **删除 Service:** ```typescript async function deleteService(namespace: string, serviceName: string) { const res = await k8sApi.deleteNamespacedService(serviceName, namespace); console.log(res.body); } ``` **中文翻译:** **使用 TypeScript 实现 Kubernetes 集群操作:Pod、Deployment 和 Service 的示例** **1. 安装依赖:** ```bash npm install @kubernetes/client-node ``` **2. 初始化 Kubernetes 客户端:** ```typescript import * as k8s from '@kubernetes/client-node'; const kc = new k8s.KubeConfig(); kc.loadFromDefault(); // 或者使用其他方式加载配置,例如 loadFromCluster() 或 loadFromString() const k8sApi = kc.makeApiClient(k8s.CoreV1Api); const k8sAppsApi = kc.makeApiClient(k8s.AppsV1Api); ``` **3. Pod 操作示例:** * **列出 Pod:** ```typescript async function listPods(namespace: string) { const res = await k8sApi.listNamespacedPod(namespace); console.log(res.body); } ``` * **创建 Pod:** ```typescript async function createPod(namespace: string, podManifest: k8s.V1Pod) { const res = await k8sApi.createNamespacedPod(namespace, podManifest); console.log(res.body); } ``` * **删除 Pod:** ```typescript async function deletePod(namespace: string, podName: string) { const res = await k8sApi.deleteNamespacedPod(podName, namespace); console.log(res.body); } ``` **4. Deployment 操作示例:** * **列出 Deployment:** ```typescript async function listDeployments(namespace: string) { const res = await k8sAppsApi.listNamespacedDeployment(namespace); console.log(res.body); } ``` * **创建 Deployment:** ```typescript async function createDeployment(namespace: string, deploymentManifest: k8s.V1Deployment) { const res = await k8sAppsApi.createNamespacedDeployment(namespace, deploymentManifest); console.log(res.body); } ``` * **更新 Deployment:** ```typescript async function updateDeployment(namespace: string, deploymentName: string, deploymentManifest: k8s.V1Deployment) { const res = await k8sAppsApi.replaceNamespacedDeployment(deploymentName, namespace, deploymentManifest); console.log(res.body); } ``` * **删除 Deployment:** ```typescript async function deleteDeployment(namespace: string, deploymentName: string) { const res = await k8sAppsApi.deleteNamespacedDeployment(deploymentName, namespace); console.log(res.body); } ``` **5. Service 操作示例:** * **列出 Service:** ```typescript async function listServices(namespace: string) { const res = await k8sApi.listNamespacedService(namespace); console.log(res.body); } ``` * **创建 Service:** ```typescript async function createService(namespace: string, serviceManifest: k8s.V1Service) { const res = await k8sApi.createNamespacedService(namespace, serviceManifest); console.log(res.body); } ``` * **更新 Service:** ```typescript async function updateService(namespace: string, serviceName: string, serviceManifest: k8s.V1Service) { const res = await k8sApi.replaceNamespacedService(serviceName, namespace, serviceManifest); console.log(res.body); } ``` * **删除 Service:** ```typescript async function deleteService(namespace: string, serviceName: string) { const res = await k8sApi.deleteNamespacedService(serviceName, namespace); console.log(res.body); } ``` **解释:** * **使用 TypeScript 实现 Kubernetes 集群操作:Pod、Deployment 和 Service 的示例 (Shǐyòng TypeScript shíxiàn Kubernetes jíqún cāozuò: Pod, Deployment hé Service de shìlì):** This is the main title, indicating the purpose of the code. * **安装依赖 (Ānzhuāng yīlài):** "Install dependencies." * **初始化 Kubernetes 客户端 (Chūshǐhuà Kubernetes kèhùduān):** "Initialize the Kubernetes client." * **Pod 操作示例 (Pod cāozuò shìlì):** "Pod operation examples." * **列出 Pod (Lièchū Pod):** "List Pods." * **创建 Pod (Chuàngjiàn Pod):** "Create Pod." * **删除 Pod (Shānchú Pod):** "Delete Pod." * **Deployment 操作示例 (Deployment cāozuò shìlì):** "Deployment operation examples." * **列出 Deployment (Lièchū Deployment):** "List Deployments." * **创建 Deployment (Chuàngjiàn Deployment):** "Create Deployment." * **更新 Deployment (Gēngxīn Deployment):** "Update Deployment." * **删除 Deployment (Shānchú Deployment):** "Delete Deployment." * **Service 操作示例 (Service cāozuò shìlì):** "Service operation examples." * **列出 Service (Lièchū Service):** "List Services." * **创建 Service (Chuàngjiàn Service):** "Create Service." * **更新 Service (Gēngxīn Service):** "Update Service." * **删除 Service (Shānchú Service):** "Delete Service." This detailed translation provides a complete and accurate representation of the original English text, including the code examples. It's suitable for use in documentation, tutorials, or other educational materials. Remember to replace the placeholder comments with actual code.

Category
访问服务器

Tools

list_pods

List pods in a namespace

list_deployments

List deployments in a namespace

list_services

List services in a namespace

list_namespaces

List all namespaces

create_pod

Create a new Kubernetes pod

create_deployment

Create a new Kubernetes deployment

delete_pod

Delete a Kubernetes pod

describe_pod

Describe a Kubernetes pod (read details like status, containers, etc.)

cleanup

Cleanup all managed resources

README

MCP Server Kubernetes

CI Language Bun Kubernetes Docker Stars Issues PRs Welcome Last Commit smithery badge

可以连接到 Kubernetes 集群并对其进行管理的 MCP 服务器。

https://github.com/user-attachments/assets/f25f8f4e-4d04-479b-9ae0-5dac452dd2ed

<a href="https://glama.ai/mcp/servers/w71ieamqrt"><img width="380" height="200" src="https://glama.ai/mcp/servers/w71ieamqrt/badge" /></a>

与 Claude Desktop 一起使用

{
  "mcpServers": {
    "kubernetes": {
      "command": "npx",
      "args": ["mcp-server-kubernetes"]
    }
  }
}

服务器将自动连接到您当前的 kubectl 上下文。请确保您已具备以下条件:

  1. 已安装 kubectl 并在您的 PATH 中
  2. 具有已配置上下文的有效 kubeconfig 文件
  3. 已为 kubectl 配置对 Kubernetes 集群的访问权限(例如,minikube、Rancher Desktop、GKE 等)
  4. 已安装 Helm v3 并在您的 PATH 中(如果您不打算使用 Helm,则为可选)。

您可以通过要求 Claude 列出您的 Pod 或创建测试部署来验证您的连接。

如果出现错误,请打开一个标准终端并运行 kubectl get pods,以查看您是否可以在没有凭据问题的情况下连接到您的集群。

与 mcp-chat 一起使用

mcp-chat 是一个用于 MCP 服务器的 CLI 聊天客户端。您可以使用它与 Kubernetes 服务器进行交互。

npx mcp-chat --server "npx mcp-server-kubernetes"

或者,从上面传递您现有的 Claude Desktop 配置文件(Linux 应该将正确的路径传递给配置):

Mac:

npx mcp-chat --config "~/Library/Application Support/Claude/claude_desktop_config.json"

Windows:

npx mcp-chat --config "%APPDATA%\Claude\claude_desktop_config.json"

功能特性

  • [x] 连接到 Kubernetes 集群
  • [x] 列出所有 Pod、服务、Deployment、节点
  • [x] 创建、描述、删除 Pod
  • [x] 列出所有命名空间,创建命名空间
  • [x] 创建自定义 Pod 和 Deployment 配置,更新 Deployment 副本
  • [x] 从 Pod 获取日志以进行调试(支持 Pod、Deployment、Job 和标签选择器)
  • [x] 支持 Helm v3 安装 Chart
    • 使用自定义值安装 Chart
    • 卸载 Release
    • 升级现有 Release
    • 支持命名空间
    • 支持版本指定
    • 支持自定义存储库
  • [x] 支持 kubectl explain 和 kubectl api-resources
  • [x] 从集群获取 Kubernetes 事件
  • [x] 端口转发到 Pod 或服务
  • [x] 创建、列出和描述 CronJob

本地开发

git clone https://github.com/Flux159/mcp-server-kubernetes.git
cd mcp-server-kubernetes
bun install

开发工作流程

  1. 在开发模式下启动服务器(监视文件更改):
bun run dev
  1. 运行单元测试:
bun run test
  1. 构建项目:
bun run build
  1. 使用 Inspector 进行本地测试
npx @modelcontextprotocol/inspector node dist/index.js
# 按照终端上的进一步说明获取 Inspector 链接
  1. 使用 Claude Desktop 进行本地测试
{
  "mcpServers": {
    "mcp-server-kubernetes": {
      "command": "node",
      "args": ["/path/to/your/mcp-server-kubernetes/dist/index.js"]
    }
  }
}
  1. 使用 mcp-chat 进行本地测试
npm run chat

贡献

有关详细信息,请参见 CONTRIBUTING.md 文件。

高级

有关使用 SSE 传输等更高级的信息,请参见 ADVANCED_README.md

架构

本节介绍 MCP Kubernetes 服务器的高级架构。

请求流程

下面的序列图说明了请求如何在系统中流动:

sequenceDiagram
    participant Client
    participant Transport as StdioTransport
    participant Server as MCP Server
    participant Handler as Request Handler
    participant K8sManager as KubernetesManager
    participant K8s as Kubernetes API

    Client->>Transport: Send Request via STDIO
    Transport->>Server: Forward Request

    alt Tools Request
        Server->>Handler: Route to tools handler
        Handler->>K8sManager: Execute tool operation
        K8sManager->>K8s: Make API call
        K8s-->>K8sManager: Return result
        K8sManager-->>Handler: Process response
        Handler-->>Server: Return tool result
    else Resource Request
        Server->>Handler: Route to resource handler
        Handler->>K8sManager: Get resource data
        K8sManager->>K8s: Query API
        K8s-->>K8sManager: Return data
        K8sManager-->>Handler: Format response
        Handler-->>Server: Return resource data
    end

    Server-->>Transport: Send Response
    Transport-->>Client: Return Final Response

发布新版本

转到发布页面,单击“Draft New Release”,单击“Choose a tag”并通过使用“v{major}.{minor}.{patch}”语义版本格式键入新的版本号来创建一个新标签。 然后,编写一个发布标题“Release v{major}.{minor}.{patch}”和描述/更改日志(如果需要),然后单击“Publish Release”。

这将创建一个新标签,该标签将通过 cd.yml 工作流程触发新的发布构建。 成功后,新版本将发布到 npm。 请注意,无需手动更新 package.json 版本,因为工作流程会自动更新 package.json 文件中的版本号并向 main 推送提交。

未计划

身份验证/将集群添加到 kubectx。

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选