大模型
dev.to ·
@Sanskriti Harmukh
使用 K8s MCP Server 管理 Kubernetes 集群
文章介绍如何运行 Kubernetes MCP Server 容器,并将其接入 Claude Desktop,从而用自然语言与 Kubernetes 集群交互、以对话方式管理集群。文中说明 MCP 通过定义用户意图、记忆和可用工具来为 LLM 组织上下文,并列出前置条件:已部署的 K8s 集群、kubectl、kubeconfig 文件以及 Claude Desktop 和 Docker Desktop。
AI推荐理由 将 MCP 与 Kubernetes 结合的实操指南,演示用自然语言通过 Claude Desktop 管理集群,体现 Agent 与开发运维工具链的落地路径。步骤具体、可复现,对关注 MCP 生态和 AI 运维的开发者有实际参考价值。
66
正文
Model Context Protocol (MCP) is a new approach to organizing context for large language models, letting them act more intelligently and maintain continuity across tasks, tools, and sessions. By defining key elements like user intent, memory, and available tools, MCP enables more seamless, context-aware interactions in AI applications. This guide walks through running a Kubernetes MCP server container so you can interact with a Kubernetes cluster using natural language, and shows how to connect it to Claude Desktop to manage the cluster conversationally. By the end, you'll have a Kubernetes MCP server running locally and wired into Claude Desktop for natural-language cluster management.
Prerequisites
Before you begin:
Deploy a Kubernetes cluster.
Install kubectl.
Download your cluster's kubeconfig file.
Download the Claude Desktop application.
Download the Docker Desktop application.
Get Started Right Away
Make sure you've downloaded your Kubernetes cluster's kubeconfig file to your local system before proceeding.
1. Display all available contexts defined in the kubeconfig file:
$ kubectl config get-contexts
Enter fullscreen mode
Exit fullscreen mode
Copy the context value to use in the later steps.
2. Pull the sample Kubernetes MCP server Docker container image:
$ docker pull ghcr.io/alexei-led/k8s-mcp-server:latest
Enter fullscreen mode
Exit fullscreen mode
3. Run the sample Kubernetes MCP server Docker container image:
$ docker run -i --rm \
-v ~/.kube:/home/appuser/.kube:ro \
-e K8S_CONTEXT= \
ghcr.io/alexei-led/k8s-mcp-server:latest
Enter fullscreen mode
Exit fullscreen mode
4. Confirm the image is running in the Docker Desktop application.
Understand the MCP Server
Unified Kubernetes CLI Tools — built-in support for multiple Kubernetes tools like kubectl, helm, istioctl, and argocd, all within a single containerized environment.
Cloud-Native Integration — native compatibility with major managed Kubernetes providers.
Secure by Design — runs as a non-root user and enforces strict validation on incoming commands to maintain security and control.
Flexible Command Chaining — supports command piping and integrates smoothly with Unix utilities like jq, grep, and sed for complex workflows.
Simplified Configuration — easily customizable through environment variables, minimizing setup time and complexity.
Use Claude Desktop to Interact
The Claude Desktop application automatically detects MCP servers defined in its configuration file and makes them accessible within any conversation.
1. Start the Claude Desktop application.
2. Locate the settings option in the application.
3. Select Developer settings.
4. Click Edit Config.
5. Add the following configuration snippet to your Claude Desktop config file:
{
"mcpServers": {
"kubernetes": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-v",
"/Users/YOUR_USER_NAME/.kube:/home/appuser/.kube:ro",
"-e",
"K8S_CONTEXT=",
"ghcr.io/alexei-led/k8s-mcp-server:latest"
]
}
}
}
Enter fullscreen mode
Exit fullscreen mode
Replace the value of K8S_CONTEXT with your actual Kubernetes context name. Also update the mount path /Users/YOUR_USER_NAME/.kube to point to your real .kube directory (usually ~/.kube on most systems).
6. Restart the Claude Desktop application once the new configuration is in place.
7. Confirm the server is running: go to the Developer settings in Claude Desktop and check that the status next to the JSON file shows as running.
8. Make sure the MCP server tools are available to the application.
Ask Questions and Perform Operations
You can manage your Kubernetes cluster by chatting naturally using the Claude Desktop application.
Make sure the responses match the actual state of your cluster.
Example Operations Supported by the Kubernetes MCP Server
Advanced Operations
Check the status of the Istio service mesh.
Create an ArgoCD application for a repository.
Deployments & Configuration
Deploy the Nginx Helm chart.
Create a deployment with 3 replicas of nginx:latest.
Set up an Ingress controller.
Troubleshooting
Describe the failing pod and explain the error.
Check if a service is properly connected to its pods.
Basic Commands
Show all pods in the default namespace.
Get all services across all namespaces.
Display the logs for the Nginx pod.
Next Steps
Extend the MCP server configuration to manage multiple cluster contexts side by side.
Pair the MCP server with the ArgoCD and Istio tooling already built into the container.
Explore additional natural-language operations for scaling, troubleshooting, and Helm deployments.
For the full guide with additional tips, visit the original article on Vultr Docs.