CLI 目录 横向对比 FCRS-1.0 标准 /llms.txt
← 返回 CLI 目录

kubectl

$ kubectl 基金会 高风险
Kubernetes / CNCF · 云原生与基础设施 · Apache-2.0 · macos / linux / windows

kubectl 在 Agent 工程化方面堪称典范:全资源统一支持 -o json 输出、-o jsonpath=... 与 -o custom-columns=... 字段裁剪、客户端/服务端 --dry-run=server 校验、在线 kubectl diff 对比以及 kubectl auth can-i RBAC 权限预检。

FCRS-1.0 综合评分
88 /100
良好 (Good)
高置信度 (实测)
零摩擦安装命令
brew install kubectl
CLI vs MCP 选型建议 优先选 CLI

kubectl 原生内置 -o json、-o jsonpath、--dry-run=server 与 kubectl diff,且大模型对其语法极其精通,无需额外 MCP 封装。

Agent 友好度体检单

本地探针实测证据
✓ 原生 JSON 输出
✓ 字段裁剪
✓ 无头非交互
✓ 支持 Dry-Run
— 只读模式开关
— 支持 NO_COLOR
✓ 自省/Schema 发现
— 官方 SKILL.md
实测版本
Client Version: v1.36.1
冷启动耗时
38 ms
退出码 (help / 非法参数)
0 / 1
NO_COLOR 无色码污染 / 无 TTY 不挂起
Yes / Safe
抓取的 --help 摘要
kubectl controls the Kubernetes cluster manager.

 Find more information at: https://kubernetes.io/docs/reference/kubectl/

Basic Commands (Beginner):
  create          Create a resource from a file or from stdin
  expose          Take a replication controller, service, deployment or pod and expose it as a new Kubernetes service
  run             Run a particular image on the cluster
  set             Set specific features on objects

Basic Commands (Intermediate):
  explain         Get documentation for a resource
  get             Display one or many resources
  edit            Edit a resource on the server
  delete          Delete resources by file names, stdin, resources and names, or by resources and label selector

Deploy Commands:
  rollout         Manage the rollout of a resource
  scale           Set a new size for a deployment, replica set, or replication controller
  autoscale       Auto-scale a deployment, replica set, stateful set, or replication controller

Cluster Management Commands:
  certificate     Modify certificate resources
  cluster-info    Display cluster information
  top             Display resource (CPU/memory) usage
  cordon          Mark node as unschedulable
  uncordon        Mark node as schedulable
  drain           Drain node in preparation for maintenance
  taint           Update the taints on one or more nodes

Troubleshooting and Debugging Commands:
  describe        Show details of a specific resource or group of resources
  logs            Print the logs for a container in a pod
  attach          Attach to a running container
  exec            Execute a command in a container
  port-forward    Forward one or more local ports to a pod
  proxy           Run a proxy to the Kubernetes API server
  cp              Copy files and directories to and from containers
  auth            Inspect authorization
  debug           Create debugging sessions for troubleshooting workloads and nodes
  events          List events

Advanced Commands:
  diff            Diff the live version against a would-be applied version
  apply           Apply a configuration to a resource by file name or stdin
  patch           Update fields of a resource
  replace         Replace a resource by file name or stdin
  wait            Wait for a specific condition on one or many resources
  kustomize       Build a kustomization target from a directory or URL

Settings Commands:
  label           Update the labels on a resource
  annotate        Update the 

FCRS-1.0 六维评分拆解

Agent 友好度 19 / 25

全命令支持 -o json、-o jsonpath=...、--field-selector、--dry-run=client|server 与 kubectl explain 自省。

  • Supports -o json, -o jsonpath, --dry-run=server, and kubectl explain
安全与权限边界 21 / 25

kubectl diff、--dry-run=server 与 kubectl auth can-i 构成业界顶尖的变更预检体系,但多集群上下文误选仍具高风险。

  • Server-side --dry-run=server, kubectl diff, and RBAC auth can-i
安装与运行摩擦 15 / 15

单文件静态 Go 二进制,可通过 Homebrew 与官方安装包跨平台部署。

  • Static Go binary on all major platforms
能力覆盖与边际价值 14 / 15

服务端标签/字段过滤配合 -o jsonpath 即使在大规模集群下也能保持极低 Token 消耗。

  • Server-side --field-selector and -l label filtering
维护与来源背书 10 / 10

CNCF 毕业旗舰项目(kubernetes/kubectl),随每个 Kubernetes 版本同步维护。

  • Official CNCF kubernetes/kubectl under Apache-2.0
文档与可验证性 9 / 10

除在线参考文档外,还内置 kubectl explain <resource> 可直接查询 API 与 CRD Schema。

  • Offline kubectl explain API schema docs

安全边界、鉴权与放权指南

鉴权方式
本地配置文件 服务账号密钥 浏览器 OAuth
凭证存储位置

从 ~/.kube/config 或 KUBECONFIG 环境变量读取集群上下文、客户端证书与外部认证插件配置。

潜在副作用
只读查询 本地写入 远端写入 破坏性删除
费用与配额提示

CLI 本身开源免费(Apache-2.0);副本扩容等集群变更会影响底层云节点成本。

可复制到 CLAUDE.md / AGENTS.md 的安全护栏
# kubectl Agent Rules
- ALWAYS check `kubectl config current-context` and pass `--context` + `-n <namespace>` explicitly.
- Run `kubectl diff -f` and `kubectl apply --dry-run=server` before mutating cluster resources.
- Never run `kubectl delete` or `kubectl drain` on production contexts without explicit user approval.

Agent 高频安全命令配方

只读查询(安全推荐)

核验当前集群上下文与 RBAC 权限
kubectl config current-context && kubectl auth can-i --list -n default

在执行任何变更前确认当前目标集群及有效的 RBAC 权限清单。

使用 -o json 与 --field-selector 查询 Pod
kubectl get pods -n default --field-selector=status.phase!=Running -o json

借助服务端 --field-selector 过滤非 Running Pod 并以 JSON 输出。

受控写入与预演

应用清单前执行服务端预演与差异对比
kubectl diff -f deploy.yaml && kubectl apply -f deploy.yaml --dry-run=server -o json

在不变更集群状态的前提下校验准入控制器、Schema 与实时差异。

核心优势

  • + --dry-run=server 与 kubectl diff 可经由真实准入 Webhook 验证变更安全性。
  • + kubectl explain 让 Agent 能直接探查原生资源及 CRD 字段定义。
  • + 全部资源类型一致支持 -o json 与 -o jsonpath。

注意事项与踩坑点

  • ! 若未显式锁定 --context,~/.kube/config 默认上下文切错可能误触生产集群。
  • ! 完整 -o json 输出往往包含冗长的 managedFields 元数据。

已验证兼容的 Agent 宿主

Claude Code Codex CLI Gemini CLI