执行 Kubernetes 集群管理操作使用 kubectl 命令行工具。
概述
该技能使代理能够:
- 查询资源 — 列出和获取 pod、部署、服务、节点等的详细信息
- 部署与更新 — 创建、应用、修补和更新 Kubernetes 资源
- 调试与故障排除 — 查看日志、在容器中执行命令、检查事件
- 管理配置 — 更新 kubeconfig、切换上下文、管理命名空间
- 监控健康状态 — 检查资源使用情况、滚动更新状态、事件和 pod 条件
- 执行操作 — 缩放部署、排空节点、管理污点和标签
先决条件
- kubectl 二进制文件 安装并在 PATH 中可访问(v1.20+)
- kubeconfig 文件 配置了集群凭据(默认:
~/.kube/config)
- 活动连接 到 Kubernetes 集群
... (中间内容与原文相同,省略) ...
环境变量
KUBECONFIG — kubeconfig 文件路径(可以包含多个由 : 分隔的路径)
KUBECTL_CONTEXT — 覆盖默认上下文
资源
版本: 1.0.0
许可证: MIT
兼容性: kubectl v1.20+、Kubernetes v1.20+
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
- 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:
brew install kubernetes-cli
Linux:
apt-get install -y kubectl # Ubuntu/Debian
yum install -y kubectl # RHEL/CentOS
Verify:
kubectl version --client
kubectl cluster-info # Test connection
Essential Commands
Query Resources
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
View Logs
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
Execute Commands
kubectl exec -it POD_NAME -- /bin/bash # Interactive shell
kubectl exec POD_NAME -- COMMAND # Run single command
Deploy Applications
kubectl apply -f deployment.yaml # Apply config
kubectl create -f deployment.yaml # Create resource
kubectl apply -f deployment.yaml --dry-run=client # Test
Update Applications
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
Manage Configuration
kubectl config view # Show kubeconfig
kubectl config get-contexts # List contexts
kubectl config use-context CONTEXT # Switch context
Common Patterns
Debugging a 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'
Deploying a New Version
# 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
Preparing Node for Maintenance
# 1. Drain node (evicts all pods)
kubectl drain NODE_NAME --ignore-daemonsets# 2. Do maintenance
# ...
# 3. Bring back online
kubectl uncordon NODE_NAME
Output Formats
The --output (-o) flag supports multiple formats:
table — Default tabular format
wide — Extended table with additional columns
json — JSON format (useful with jq)
yaml — YAML format
jsonpath — JSONPath expressions
custom-columns — Define custom output columns
name — Only resource names
Examples:
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
Global Flags (Available to All Commands)
-n, --namespace= # Operate in specific namespace
-A, --all-namespaces # Operate across all namespaces
--context= # Use specific kubeconfig context
-o, --output= # Output format (json, yaml, table, etc.)
--dry-run= # Dry-run mode (none, client, server)
-l, --selector= # Filter by labels
--field-selector= # Filter by fields
-v, --v= # Verbosity level (0-9)
Dry-Run Modes
--dry-run=client — Fast client-side validation (test commands safely)
--dry-run=server — Server-side validation (more accurate)
--dry-run=none — Execute for real (default)
Always test with --dry-run=client first:
kubectl apply -f manifest.yaml --dry-run=client
Advanced Topics
For detailed reference material, command-by-command documentation, troubleshooting guides, and advanced workflows, see:
Helpful Tips
- Use label selectors for bulk operations:
kubectl delete pods -l app=myapp
kubectl get pods -l env=prod,tier=backend
- Watch resources in real-time:
kubectl get pods -w # Watch for changes
- Use
-A flag for all namespaces:
kubectl get pods -A # See pods everywhere
- Save outputs for later comparison:
kubectl get deployment my-app -o yaml > deployment-backup.yaml
kubectl delete pod POD_NAME --dry-run=client
Getting Help
kubectl help # General help
kubectl COMMAND --help # Command help
kubectl explain pods # Resource documentation
kubectl explain pods.spec # Field documentation
Environment Variables
KUBECONFIG — Path to kubeconfig file (can include multiple paths separated by :)
KUBECTL_CONTEXT — Override default context
Resources
Version: 1.0.0
License: MIT
Compatible with: kubectl v1.20+, Kubernetes v1.20+