K8s cluster ops
通过 kubectl 命令行工具管理 Kubernetes 集群,执行查询资源状态、部署应用、查看日志、调试容器、切换上下文和监控集群健康等操作。适用于日常运维、发布和故障排查。当用户询问集群状态、Pod/Deployment信息、查看日志、执行容器命令、切换集群或上下文,或使用 kubectl get/describe/logs/exec/apply 等相关命令短语时触发。From its SKILL.md
npx -y skills add serejaris/kimi-skills --skill k8s-cluster-opsAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- 22 days oldThe repository was created 22 days ago. New is not bad, but a brand new repository carrying a familiar-sounding name is the shape a typosquat arrives in, and there has been no time for anyone else to find a problem with it.
- 5 stars5 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
What its file declares
Copied from the file, not written here
The file declares its own license as MIT. That is the author’s claim about this one file, and it is not the same thing as the license GitHub reports for the repository, which is listed with the other numbers below.
SKILL.md
7.2 KB, ~1.9k tokens by cl100k_base, as published. Nobody here has run it
kubectl 技能
使用 kubectl 命令行工具执行 Kubernetes 集群管理操作。
概述
此技能可以帮助 Agent 完成以下操作:
- 查询资源 — 列出和获取 Pod、Deployment、Service、Node 等资源的详细信息
- 部署与更新 — 创建、应用、补丁和更新 Kubernetes 资源
- 调试与排障 — 查看日志、在容器中执行命令、检查事件
- 管理配置 — 更新 kubeconfig、切换上下文、管理命名空间
- 监控健康 — 检查资源使用情况、滚动更新状态、事件和 Pod 状态
- 执行运维操作 — 扩缩 Deployment、腾空节点、管理污点和标签
前置条件
- kubectl 二进制文件已安装并在 PATH 中可用(v1.20+)
- kubeconfig 文件已配置集群凭证(默认路径:
~/.kube/config) - 已建立与 Kubernetes 集群的网络连接
快速配置
安装 kubectl
macOS:
brew install kubernetes-cli
Linux:
apt-get install -y kubectl # Ubuntu/Debian
yum install -y kubectl # RHEL/CentOS
验证安装:
kubectl version --client
kubectl cluster-info # Test connection
常用命令
查询资源
kubectl get pods # List all pods in current namespace
kubectl get pods -A # All namespaces
kubectl get pods -o wide # More columns
kubectl get nodes # List nodes
kubectl describe pod POD_NAME # Detailed info with events
查看日志
kubectl logs POD_NAME # Get logs
kubectl logs -f POD_NAME # Follow logs (tail -f)
kubectl logs POD_NAME -c CONTAINER # Specific container
kubectl logs POD_NAME --previous # Previous container logs
在容器中执行命令
kubectl exec -it POD_NAME -- /bin/bash # Interactive shell
kubectl exec POD_NAME -- COMMAND # Run single command
部署应用
kubectl apply -f deployment.yaml # Apply config
kubectl create -f deployment.yaml # Create resource
kubectl apply -f deployment.yaml --dry-run=client # Test
更新应用
kubectl set image deployment/APP IMAGE=IMAGE:TAG # Update image
kubectl scale deployment/APP --replicas=3 # Scale pods
kubectl rollout status deployment/APP # Check status
kubectl rollout undo deployment/APP # Rollback
管理配置
kubectl config view # Show kubeconfig
kubectl config get-contexts # List contexts
kubectl config use-context CONTEXT # Switch context
常见操作场景
调试 Pod
# 1. Identify the issue
kubectl describe pod POD_NAME
# 2. Check logs
kubectl logs POD_NAME
kubectl logs POD_NAME --previous
# 3. Execute debug commands
kubectl exec -it POD_NAME -- /bin/bash
# 4. Check events
kubectl get events --sort-by='.lastTimestamp'
发布新版本
# 1. Update image
kubectl set image deployment/MY_APP my-app=my-app:v2
# 2. Monitor rollout
kubectl rollout status deployment/MY_APP -w
# 3. Verify
kubectl get pods -l app=my-app
# 4. Rollback if needed
kubectl rollout undo deployment/MY_APP
节点维护准备
# 1. Drain node (evicts all pods)
kubectl drain NODE_NAME --ignore-daemonsets
# 2. Do maintenance
# ...
# 3. Bring back online
kubectl uncordon NODE_NAME
输出格式
--output(-o)参数支持多种输出格式:
table— 默认表格格式wide— 扩展表格,包含更多列json— JSON 格式(可配合jq使用)yaml— YAML 格式jsonpath— JSONPath 表达式custom-columns— 自定义输出列name— 仅显示资源名称
示例:
kubectl get pods -o json | jq '.items[0].metadata.name'
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase
全局参数(适用于所有命令)
-n, --namespace=<ns> # Operate in specific namespace
-A, --all-namespaces # Operate across all namespaces
--context=<context> # Use specific kubeconfig context
-o, --output=<format> # Output format (json, yaml, table, etc.)
--dry-run=<mode> # Dry-run mode (none, client, server)
-l, --selector=<labels> # Filter by labels
--field-selector=<selector> # Filter by fields
-v, --v=<int> # Verbosity level (0-9)
试运行模式
--dry-run=client— 快速的客户端验证(安全测试命令)--dry-run=server— 服务端验证(结果更准确)--dry-run=none— 真正执行(默认行为)
建议始终先用 --dry-run=client 测试:
kubectl apply -f manifest.yaml --dry-run=client
进阶内容
如需详细的参考资料、逐条命令文档、故障排查指南和高级工作流,请参阅:
- references/REFERENCE.md — 完整的 kubectl 命令参考
- scripts/ — 常见任务的辅助脚本
实用技巧
-
使用标签选择器进行批量操作:
kubectl delete pods -l app=myapp kubectl get pods -l env=prod,tier=backend -
实时监视资源变化:
kubectl get pods -w # Watch for changes -
使用
-A参数查看所有命名空间:kubectl get pods -A # See pods everywhere -
导出配置以便日后对比:
kubectl get deployment my-app -o yaml > deployment-backup.yaml -
删除前先做试运行确认:
kubectl delete pod POD_NAME --dry-run=client
获取帮助
kubectl help # General help
kubectl COMMAND --help # Command help
kubectl explain pods # Resource documentation
kubectl explain pods.spec # Field documentation
环境变量
KUBECONFIG— kubeconfig 文件路径(可包含多个路径,用:分隔)KUBECTL_CONTEXT— 覆盖默认上下文
参考资源
版本: 1.0.0
许可证: MIT
兼容: kubectl v1.20+、Kubernetes v1.20+
What ships with it: 7 files
25.9 KB alongside SKILL.md, 4 of them executable
references/
- REFERENCE.md17.3 KB
scripts/
- kubectl-cluster-info.shruns1.1 KB
- kubectl-deploy-update.shruns1.3 KB
- kubectl-node-drain.shruns1.0 KB
- kubectl-pod-debug.shruns1.3 KB
- _meta.json126 B
- README.md3.8 KB