kubectl Skill
Execute Kubernetes cluster management operations using the kubectl command-line tool.
Overview
This skill enables agents to:
- - Query Resources — List and get details about pods, deployments, services, nodes, etc.
- Deploy & Update — Create, apply, patch, and update Kubernetes resources
- Debug & Troubleshoot — View logs, execute commands in containers, inspect events
- Manage Configuration — Update kubeconfig, switch contexts, manage namespaces
- Monitor Health — Check resource usage, rollout status, events, and pod conditions
- Perform Operations — Scale deployments, drain nodes, manage taints and labels
Prerequisites
- 1. kubectl binary installed and accessible on PATH (v1.20+)
- kubeconfig file configured with cluster credentials (default:
~/.kube/config) - Active connection to a Kubernetes cluster
Quick Setup
Install kubectl
macOS:
CODEBLOCK0
Linux:
CODEBLOCK1
Verify:
CODEBLOCK2
Essential Commands
Query Resources
CODEBLOCK3
View Logs
CODEBLOCK4
Execute Commands
CODEBLOCK5
Deploy Applications
CODEBLOCK6
Update Applications
CODEBLOCK7
Manage Configuration
CODEBLOCK8
Common Patterns
Debugging a Pod
CODEBLOCK9
Deploying a New Version
CODEBLOCK10
Preparing Node for Maintenance
CODEBLOCK11
Output Formats
The --output (-o) flag supports multiple formats:
- -
table — Default tabular format - INLINECODE5 — Extended table with additional columns
- INLINECODE6 — JSON format (useful with
jq) - INLINECODE8 — YAML format
- INLINECODE9 — JSONPath expressions
- INLINECODE10 — Define custom output columns
- INLINECODE11 — Only resource names
Examples:
CODEBLOCK12
Global Flags (Available to All Commands)
CODEBLOCK13
Dry-Run Modes
- -
--dry-run=client — Fast client-side validation (test commands safely) - INLINECODE13 — Server-side validation (more accurate)
- INLINECODE14 — Execute for real (default)
Always test with --dry-run=client first:
CODEBLOCK14
Advanced Topics
For detailed reference material, command-by-command documentation, troubleshooting guides, and advanced workflows, see:
Helpful Tips
- 1. Use label selectors for bulk operations:
CODEBLOCK15
- 2. Watch resources in real-time:
CODEBLOCK16
- 3. Use
-A flag for all namespaces:
CODEBLOCK17
- 4. Save outputs for later comparison:
CODEBLOCK18
- 5. Check before you delete:
CODEBLOCK19
Getting Help
CODEBLOCK20
Environment Variables
- -
KUBECONFIG — Path to kubeconfig file (can include multiple paths separated by :) - INLINECODE19 — Override default context
Resources
Version: 1.0.0
License: MIT
Compatible with: kubectl v1.20+, Kubernetes v1.20+
kubectl 技能
使用 kubectl 命令行工具执行 Kubernetes 集群管理操作。
概述
该技能使代理能够:
- - 查询资源 — 列出并获取 Pod、部署、服务、节点等资源的详细信息
- 部署与更新 — 创建、应用、修补和更新 Kubernetes 资源
- 调试与故障排查 — 查看日志、在容器中执行命令、检查事件
- 管理配置 — 更新 kubeconfig、切换上下文、管理命名空间
- 监控健康状态 — 检查资源使用情况、滚动更新状态、事件和 Pod 状态
- 执行操作 — 扩缩部署、排空节点、管理污点和标签
前提条件
- 1. kubectl 二进制文件 已安装并可在 PATH 中访问(v1.20+)
- kubeconfig 文件 已配置集群凭据(默认:~/.kube/config)
- 与 Kubernetes 集群的活动连接
快速设置
安装 kubectl
macOS:
bash
brew install kubernetes-cli
Linux:
bash
apt-get install -y kubectl # Ubuntu/Debian
yum install -y kubectl # RHEL/CentOS
验证:
bash
kubectl version --client
kubectl cluster-info # 测试连接
基本命令
查询资源
bash
kubectl get pods # 列出当前命名空间中的所有 Pod
kubectl get pods -A # 所有命名空间
kubectl get pods -o wide # 更多列
kubectl get nodes # 列出节点
kubectl describe pod POD_NAME # 详细信息及事件
查看日志
bash
kubectl logs POD_NAME # 获取日志
kubectl logs -f POD_NAME # 跟踪日志(tail -f)
kubectl logs POD_NAME -c CONTAINER # 特定容器
kubectl logs POD_NAME --previous # 前一个容器的日志
执行命令
bash
kubectl exec -it POD_NAME -- /bin/bash # 交互式 Shell
kubectl exec POD_NAME -- COMMAND # 运行单个命令
部署应用
bash
kubectl apply -f deployment.yaml # 应用配置
kubectl create -f deployment.yaml # 创建资源
kubectl apply -f deployment.yaml --dry-run=client # 测试
更新应用
bash
kubectl set image deployment/APP IMAGE=IMAGE:TAG # 更新镜像
kubectl scale deployment/APP --replicas=3 # 扩缩 Pod
kubectl rollout status deployment/APP # 检查状态
kubectl rollout undo deployment/APP # 回滚
管理配置
bash
kubectl config view # 显示 kubeconfig
kubectl config get-contexts # 列出上下文
kubectl config use-context CONTEXT # 切换上下文
常见模式
调试 Pod
bash
1. 识别问题
kubectl describe pod POD_NAME
2. 检查日志
kubectl logs POD_NAME
kubectl logs POD_NAME --previous
3. 执行调试命令
kubectl exec -it POD_NAME -- /bin/bash
4. 检查事件
kubectl get events --sort-by=.lastTimestamp
部署新版本
bash
1. 更新镜像
kubectl set image deployment/MY_APP my-app=my-app:v2
2. 监控滚动更新
kubectl rollout status deployment/MY_APP -w
3. 验证
kubectl get pods -l app=my-app
4. 必要时回滚
kubectl rollout undo deployment/MY_APP
准备节点进行维护
bash
1. 排空节点(驱逐所有 Pod)
kubectl drain NODE_NAME --ignore-daemonsets
2. 执行维护
...
3. 恢复上线
kubectl uncordon NODE_NAME
输出格式
--output(-o)标志支持多种格式:
- - table — 默认表格格式
- wide — 扩展表格,包含更多列
- json — JSON 格式(可与 jq 配合使用)
- yaml — YAML 格式
- jsonpath — JSONPath 表达式
- custom-columns — 自定义输出列
- name — 仅显示资源名称
示例:
bash
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
全局标志(适用于所有命令)
bash
-n, --namespace= # 在特定命名空间中操作
-A, --all-namespaces # 在所有命名空间中操作
--context= # 使用特定的 kubeconfig 上下文
-o, --output= # 输出格式(json、yaml、table 等)
--dry-run= # 试运行模式(none、client、server)
-l, --selector= # 按标签过滤
--field-selector= # 按字段过滤
-v, --v= # 详细级别(0-9)
试运行模式
- - --dry-run=client — 快速客户端验证(安全测试命令)
- --dry-run=server — 服务端验证(更准确)
- --dry-run=none — 实际执行(默认)
始终先使用 --dry-run=client 进行测试:
bash
kubectl apply -f manifest.yaml --dry-run=client
高级主题
有关详细的参考材料、逐命令文档、故障排查指南和高级工作流程,请参阅:
实用技巧
- 1. 使用标签选择器进行批量操作:
bash
kubectl delete pods -l app=myapp
kubectl get pods -l env=prod,tier=backend
- 2. 实时监控资源:
bash
kubectl get pods -w # 监控变化
- 3. 使用 -A 标志查看所有命名空间:
bash
kubectl get pods -A # 查看所有 Pod
- 4. 保存输出以便后续比较:
bash
kubectl get deployment my-app -o yaml > deployment-backup.yaml
- 5. 删除前先检查:
bash
kubectl delete pod POD_NAME --dry-run=client
获取帮助
bash
kubectl help # 常规帮助
kubectl COMMAND --help # 命令帮助
kubectl explain pods # 资源文档
kubectl explain pods.spec # 字段文档
环境变量
- - KUBECONFIG — kubeconfig 文件路径(可包含多个路径,用 : 分隔)
- KUBECTL_CONTEXT — 覆盖默认上下文
资源
版本: 1.0.0
许可证: MIT
兼容性: kubectl v1.20+,Kubernetes v1.20+