k8s_top_pods
Monitor CPU and memory usage for Kubernetes pods to identify resource-intensive containers and optimize cluster performance.
Instructions
Show CPU and memory usage for pods (requires metrics-server)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| namespace | No | Kubernetes namespace (default: 'default') |
Implementation Reference
- src/tools/kubernetes/resources.ts:13-46 (handler)The `topPods` function implements the `k8s_top_pods` tool logic, fetching CPU and memory usage from the Kubernetes Metrics API.
export async function topPods(args: Record<string, unknown>): Promise<string> { const api = getMetricsApi(); const namespace = (args.namespace as string) || "default"; const response = await api.listNamespacedCustomObject( "metrics.k8s.io", "v1beta1", namespace, "pods" ); const body = response.body as { items: MetricsItem[] }; const items = body.items || []; if (items.length === 0) { return `No metrics available for pods in namespace '${namespace}'. Is metrics-server installed?`; } const headers = ["POD", "CONTAINER", "CPU", "MEMORY"]; const rows: string[][] = []; for (const item of items) { for (const container of item.containers || []) { rows.push([ item.metadata.name, container.name, container.usage.cpu, container.usage.memory, ]); } } return `Resource usage for pods in namespace '${namespace}':\n\n${formatTable(headers, rows)}`; } - src/tools/kubernetes/index.ts:76-85 (schema)Definition of the `k8s_top_pods` tool schema in the Kubernetes tools registry.
{ name: "k8s_top_pods", description: "Show CPU and memory usage for pods (requires metrics-server)", inputSchema: { type: "object" as const, properties: { namespace: { type: "string", description: "Kubernetes namespace (default: 'default')" }, }, }, }, - src/tools/kubernetes/index.ts:201-201 (registration)Registration of the `k8s_top_pods` tool within the tool dispatcher in `src/tools/kubernetes/index.ts`.
case "k8s_top_pods": return topPods(a);