kubectl
$ kubectl 基金会 高风险kubectl 在 Agent 工程化方面堪称典范:全资源统一支持 -o json 输出、-o jsonpath=... 与 -o custom-columns=... 字段裁剪、客户端/服务端 --dry-run=server 校验、在线 kubectl diff 对比以及 kubectl auth can-i RBAC 权限预检。
brew install kubectl kubectl 原生内置 -o json、-o jsonpath、--dry-run=server 与 kubectl diff,且大模型对其语法极其精通,无需额外 MCP 封装。
Agent 友好度体检单
本地探针实测证据抓取的 --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 六维评分拆解
全命令支持 -o json、-o jsonpath=...、--field-selector、--dry-run=client|server 与 kubectl explain 自省。
- Supports
-o json,-o jsonpath,--dry-run=server, andkubectl explain
kubectl diff、--dry-run=server 与 kubectl auth can-i 构成业界顶尖的变更预检体系,但多集群上下文误选仍具高风险。
- Server-side
--dry-run=server,kubectl diff, and RBACauth can-i
单文件静态 Go 二进制,可通过 Homebrew 与官方安装包跨平台部署。
- Static Go binary on all major platforms
服务端标签/字段过滤配合 -o jsonpath 即使在大规模集群下也能保持极低 Token 消耗。
- Server-side
--field-selectorand-llabel filtering
CNCF 毕业旗舰项目(kubernetes/kubectl),随每个 Kubernetes 版本同步维护。
- Official CNCF
kubernetes/kubectlunder Apache-2.0
除在线参考文档外,还内置 kubectl explain <resource> 可直接查询 API 与 CRD Schema。
- Offline
kubectl explainAPI schema docs
安全边界、鉴权与放权指南
从 ~/.kube/config 或 KUBECONFIG 环境变量读取集群上下文、客户端证书与外部认证插件配置。
CLI 本身开源免费(Apache-2.0);副本扩容等集群变更会影响底层云节点成本。
# 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 高频安全命令配方
只读查询(安全推荐)
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元数据。