kubectl_scale
Scale Kubernetes deployments, replicasets, or statefulsets by adjusting replica counts to manage application capacity and resource allocation.
Instructions
Scale a Kubernetes deployment
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | Name of the deployment to scale | |
| namespace | No | Kubernetes namespace | default |
| replicas | Yes | Number of replicas to scale to | |
| resourceType | No | Resource type to scale (deployment, replicaset, statefulset) | deployment |
| context | No | Kubeconfig Context to use for the command (optional - defaults to null) |
Implementation Reference
- src/tools/kubectl-scale.ts:37-109 (handler)The main handler function that scales a Kubernetes resource (deployment, replicaset, statefulset) using the 'kubectl scale' command via execFileSync. Handles errors and returns structured content.export async function kubectlScale( k8sManager: KubernetesManager, input: { name: string; namespace?: string; replicas: number; resourceType?: string; context?: string; } ) { try { const namespace = input.namespace || "default"; const resourceType = input.resourceType || "deployment"; const context = input.context || ""; const command = "kubectl"; const args = [ "scale", resourceType, input.name, `--replicas=${input.replicas}`, `--namespace=${namespace}`, ]; // Add context if provided if (context) { args.push("--context", context); } // Execute the command try { const result = execFileSync(command, args, { encoding: "utf8", maxBuffer: getSpawnMaxBuffer(), env: { ...process.env, KUBECONFIG: process.env.KUBECONFIG }, }); return { content: [ { success: true, message: `Scaled ${resourceType} ${input.name} to ${input.replicas} replicas`, }, ], }; } catch (error: any) { throw new McpError( ErrorCode.InternalError, `Failed to scale ${resourceType}: ${error.message}` ); } } catch (error: any) { if (error instanceof McpError) { return { content: [ { success: false, message: error.message, }, ], }; } return { content: [ { success: false, message: `Failed to scale resource: ${error.message}`, }, ], }; } }
- src/tools/kubectl-scale.ts:7-35 (schema)Input schema definition for the kubectl_scale tool, including parameters like name, replicas, namespace, resourceType, and context.export const kubectlScaleSchema = { name: "kubectl_scale", description: "Scale a Kubernetes deployment", annotations: { destructiveHint: true, }, inputSchema: { type: "object", properties: { name: { type: "string", description: "Name of the deployment to scale", }, namespace: namespaceParameter, replicas: { type: "number", description: "Number of replicas to scale to", }, resourceType: { type: "string", description: "Resource type to scale (deployment, replicaset, statefulset)", default: "deployment", }, context: contextParameter, }, required: ["name", "replicas"], }, };
- src/index.ts:502-513 (registration)Tool handler registration and dispatch within the CallToolRequestSchema handler switch statement.case "kubectl_scale": { return await kubectlScale( k8sManager, input as { name: string; namespace?: string; replicas: number; resourceType?: string; context?: string; } ); }
- src/index.ts:111-111 (registration)Tool schema registration in the allTools array used for ListToolsRequestSchema.kubectlScaleSchema,
- src/index.ts:45-45 (registration)Import statement for the kubectlScale handler and schema.import { kubectlScale, kubectlScaleSchema } from "./tools/kubectl-scale.js";