8
n8n 中文网amn8n.com

使用GPT-4o和MCP工具的自然语言Kubernetes管理

中级

这是一个DevOps, Multimodal AI领域的自动化工作流,包含 11 个节点。主要使用 McpClient, McpClientTool, Agent, ChatTrigger, LmChatOpenAi 等节点。 使用GPT-4o和MCP工具的自然语言Kubernetes管理

前置要求
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "dBi294X47V7gDxkr",
  "meta": {
    "instanceId": "4a409a91d2a3d01c353bbc5fe897fb8ad93edeeea7f4f1264dcf7a9735497f14",
    "templateCredsSetupCompleted": true
  },
  "name": "K8s分析",
  "tags": [],
  "nodes": [
    {
      "id": "69df7205-ee0a-46bf-81e5-3874601a8505",
      "name": "OpenAI K8s模型",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        448,
        16
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "cjWgmpi35pveOOWw",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "18eb6ec5-e732-41e4-8840-27264225f102",
      "name": "K8s查询分析器",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        448,
        -160
      ],
      "parameters": {
        "text": "=You are a Kubernetes expert assistant that analyzes user queries and generates kubectl commands for execution via MCP tools.\n\n**Your Primary Task:**\nAnalyze the user's query and generate the appropriate kubectl command to fulfill their request.\n\n**Available kubectl operations (read-only only):**\n- `kubectl get` - List resources (pods, deployments, services, nodes, etc.)\n- `kubectl describe` - Show detailed resource information\n- `kubectl explain` - Show resource documentation\n- `kubectl config view` - Show kubeconfig settings\n- `kubectl version` - Show kubectl and cluster versions\n- `kubectl cluster-info` - Show cluster information\n- `kubectl api-resources` - List available API resources\n\n**Critical Instructions:**\n1. **Analyze the user query** to understand what Kubernetes information they need\n2. **Generate ONLY the kubectl command** - do not execute it yourself\n3. **Output MUST be valid JSON only - NO markdown, NO backticks, NO formatting**\n4. **Output format example:**\n{\"command\": \"kubectl get pods --all-namespaces\"}\n\n**Command Generation Guidelines:**\n- Always start commands with \"kubectl\"\n- Use appropriate namespaces (--all-namespaces, -n namespace-name)\n- Use default namespace unless specified.\n- Do not add --cluster flag.\n- Include helpful flags like --field-selector, --label-selector when relevant\n- You can also use grep flag to get accurate output as per user request.\n- For status queries, use field selectors (e.g., status.phase=Running, status.phase=Failed)\n- Include output formatting when helpful (-o wide, -o yaml, -o json)\n- Only use read-only commands (get, describe, explain, config view, version, cluster-info, api-resources)\n\n**Example Transformations:**\n- \"Show me all pods\" → {\"command\": \"kubectl get pods --all-namespaces\"}\n- \"List failing pods\" → {\"command\": \"kubectl get pods --all-namespaces --field-selector=status.phase=Failed\"}\n- \"Get pod details in default namespace\" → {\"command\": \"kubectl get pods -n default -o wide\"}\n- \"Show me deployments in kube-system\" → {\"command\": \"kubectl get deployments -n kube-system\"}\n- \"What nodes are ready?\" → {\"command\": \"kubectl get nodes\"}\n- \"Describe pod nginx\" → {\"command\": \"kubectl describe pod nginx\"}\n- \"Show cluster info\" → {\"command\": \"kubectl cluster-info\"}\n\n**CRITICAL RULES:**\n- Output ONLY raw JSON - no ```json blocks, no backticks, no explanations\n- Response must be parseable JSON\n- If user asks for modifications/deletions, output: {\"command\": \"echo 'Error: Only read-only operations supported'\"}\n- Ensure kubectl syntax is correct\n\n**User Query:** {{ $('When chat message received').item.json.chatInput }}\n\n**Response (Only JSON):**",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 2.1
    },
    {
      "id": "53bfb274-b4a6-40e8-889f-4964692dbdc8",
      "name": "当收到聊天消息时",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -288,
        -160
      ],
      "webhookId": "299f8b1f-0e54-4900-b773-b8a49b20a7ad",
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "8795dd11-da4d-45d5-97a7-f28ffae285f4",
      "name": "Kubectl MCP工具",
      "type": "n8n-nodes-mcp.mcpClient",
      "position": [
        768,
        -160
      ],
      "parameters": {
        "toolName": "run_kubectl_command_ro",
        "operation": "executeTool",
        "toolParameters": "={{ $json.output }}"
      },
      "credentials": {
        "mcpClientApi": {
          "id": "3hXyv45IocVhWsE4",
          "name": "K8s Creds"
        }
      },
      "retryOnFail": true,
      "typeVersion": 1
    },
    {
      "id": "2125c903-bf7d-476a-af42-f915d695bb37",
      "name": "OpenAI 聊天模型",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -96,
        16
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o",
          "cachedResultName": "gpt-4o"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "cjWgmpi35pveOOWw",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "7ac15f78-2e8d-418e-a34a-65d5d8ad2d50",
      "name": "列出K8s集群",
      "type": "n8n-nodes-mcp.mcpClientTool",
      "position": [
        48,
        16
      ],
      "parameters": {
        "toolName": "list_clusters",
        "operation": "executeTool",
        "toolParameters": "={}"
      },
      "credentials": {
        "mcpClientApi": {
          "id": "3hXyv45IocVhWsE4",
          "name": "K8s Creds"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "b78e3c21-bac7-41d1-b949-d29484d236c3",
      "name": "K8s集群分析器",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -96,
        -160
      ],
      "parameters": {
        "text": "=You are a Kubernetes multi-cluster assistant that extracts cluster names from user queries using dynamic cluster discovery.\n\n**Your Tasks:**\n1. **Use the List K8s Clusters tool** to get the current available clusters\n2. **Analyze the user query** to extract cluster context\n3. **Match user input** to available cluster names\n4. **Output clean JSON** for context switching\n\n**Workflow Steps:**\n1. Call the List K8s Clusters tool to get current cluster list\n2. Parse the cluster names from the result\n3. Create smart mappings based on cluster naming patterns\n4. Match user query against available clusters\n\n**Smart Cluster Matching Logic:**\n- Extract keywords from cluster names (artemis, hermes, kiwi, dev, prod, staging, etc.)\n- Match partial names (case-insensitive)\n- Handle common abbreviations (prod → production, dev → development)\n- Support environment-based matching (staging, production, development)\n\n**Output Rules:**\n- NO markdown, NO backticks, NO code blocks, NO explanations\n- ONLY raw JSON that can be parsed directly\n- If cluster found: {\"context\": \"exact-full-cluster-name-from-list\"}\n- If no cluster specified: {\"message\": \"Please specify which cluster you'd like to work with. Available clusters: [list-short-names]\"}\n- If ambiguous match: {\"message\": \"Multiple clusters found. Please be more specific: [matching-options]\"}\n\n**Examples:**\nUser: \"show pods in artemis\"\n1. Get clusters via List K8s Clusters tool\n2. Find cluster containing \"artemis\" → \"us-east-1.teleport.cloudservices.acquia.io-csp-artemis2a7\"\n3. Output: {\"context\": \"us-east-1.teleport.cloudservices.acquia.io-csp-artemis2a7\"}\n\nUser: \"list deployments\"\n1. Get clusters via List K8s Clusters tool\n2. No cluster specified in query\n3. Extract short names from cluster list\n4. Output: {\"message\": \"Please specify which cluster you'd like to work with. Available clusters: artemis, hermes, kiwi, dev, saas, tools, minikube\"}\n\n**Critical Instructions:**\n- ALWAYS use List K8s Clusters tool first to get current cluster list\n- Base all cluster matching on the ACTUAL available clusters\n- Never use hardcoded cluster names or mappings\n- Extract meaningful short names from full cluster names for user guidance\n- Match user input against both full names and extracted keywords\n\n**User Query:** {{ $json.chatInput }}\n\n**Start by using the List K8s Clusters tool, then provide your analysis:**",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 2.1
    },
    {
      "id": "2bdd9886-d726-4db4-a039-936f34260482",
      "name": "切换K8s上下文",
      "type": "n8n-nodes-mcp.mcpClient",
      "position": [
        240,
        -160
      ],
      "parameters": {
        "toolName": "switch_context",
        "operation": "executeTool",
        "toolParameters": "={{ $json.output }}"
      },
      "credentials": {
        "mcpClientApi": {
          "id": "3hXyv45IocVhWsE4",
          "name": "K8s Creds"
        }
      },
      "notesInFlow": false,
      "typeVersion": 1
    },
    {
      "id": "d8bfc9f4-8ffc-4611-9262-807258bdc159",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -128,
        -352
      ],
      "parameters": {
        "color": 3,
        "width": 496,
        "height": 496,
        "content": "## 集群上下文切换"
      },
      "typeVersion": 1
    },
    {
      "id": "eee427c1-7df5-4916-af30-8bbf22e2ba36",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        416,
        -352
      ],
      "parameters": {
        "color": 5,
        "width": 480,
        "height": 496,
        "content": "## kubectl命令生成"
      },
      "typeVersion": 1
    },
    {
      "id": "1c7a5100-cdc7-4574-b2b2-8905c703a52d",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -544,
        -208
      ],
      "parameters": {
        "width": 304,
        "height": 208,
        "content": "## 使用聊天尝试此功能"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "e45aeee6-2600-4c8d-91c7-0bbd548807da",
  "connections": {
    "69df7205-ee0a-46bf-81e5-3874601a8505": {
      "ai_languageModel": [
        [
          {
            "node": "18eb6ec5-e732-41e4-8840-27264225f102",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "7ac15f78-2e8d-418e-a34a-65d5d8ad2d50": {
      "ai_tool": [
        [
          {
            "node": "b78e3c21-bac7-41d1-b949-d29484d236c3",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "2125c903-bf7d-476a-af42-f915d695bb37": {
      "ai_languageModel": [
        [
          {
            "node": "b78e3c21-bac7-41d1-b949-d29484d236c3",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "18eb6ec5-e732-41e4-8840-27264225f102": {
      "main": [
        [
          {
            "node": "8795dd11-da4d-45d5-97a7-f28ffae285f4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "2bdd9886-d726-4db4-a039-936f34260482": {
      "main": [
        [
          {
            "node": "18eb6ec5-e732-41e4-8840-27264225f102",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "b78e3c21-bac7-41d1-b949-d29484d236c3": {
      "main": [
        [
          {
            "node": "2bdd9886-d726-4db4-a039-936f34260482",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "53bfb274-b4a6-40e8-889f-4964692dbdc8": {
      "main": [
        [
          {
            "node": "b78e3c21-bac7-41d1-b949-d29484d236c3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

中级 - 开发运维, 多模态 AI

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
中级
节点数量11
分类2
节点类型6
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
Aadarsh Jain

Aadarsh Jain

@aadarsh-jain

An enthusiast DevOps Engineer with 3+ years of experience in devops tools and automation.

外部链接
在 n8n.io 查看

分享此工作流

分类

分类: 34