关于 kubectl
用于通过 kubectl 管理 Kubernetes 集群的技能,面向负责日常巡检、故障排查和部署变更的运维人员。它预设了生产、预发布和本地环境别名,并为高风险操作设置确认要求。
主要功能
- 01预设集群与命名空间别名
- 02查看 Kubernetes 资源与日志
- 03排查 Pod 和服务问题
- 04管理扩缩容与滚动更新
- 05高风险变更需确认
适用场景
- 01检查不同环境中的 Pod 状态
- 02结合事件和日志排查启动故障
- 03扩缩容部署或监控资源用量
- 04在确认后执行生产环境变更
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ oldwinter/ skills --skill kubectl安装后在 Claude Code 中输入/kubectl确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/jsonpath_formatting.mdtroubleshooting.mdscripts/cluster_status.pyrestart_monitor.py详细介绍
Kubectl 技能
此技能可使用 kubectl 及相关工具,全面管理 Kubernetes 集群。
环境
集群别名
已预先配置以下三组集群/命名空间组合:
| 别名 | 集群 | 命名空间 | 用途 |
|---|---|---|---|
k1 | AWS EKS Production | production | 生产环境 |
k2 | AWS EKS Production | staging | 预发布环境 |
k | K3s (192.168.10.117) | simplex | 本地开发环境 |
用法:
k1 get pods # 查看生产环境 pods
k2 get pods # 查看预发布环境 pods
k get pods # 查看本地环境 pods
其他工具
kubectx- 在集群之间切换kubens- 在命名空间之间切换argocd- GitOps 部署(参见单独的技能)kargo- 渐进式交付(参见单独的技能)
安全协议
需要确认的危险操作
执行以下任一操作前,必须明确向用户确认:
- 删除操作:
delete pod、delete deployment、delete service、delete pvc - 缩容至零:
scale --replicas=0 - 生产环境修改:任何会修改资源的
k1命令 - 排空/封锁节点:
drain、cordon、uncordon - 应用/修补:对生产环境资源进行更改
确认格式
⚠️ 危险操作确认
环境: [Production/Staging/Local]
操作: [具体操作描述]
资源: [受影响的资源]
影响: [潜在影响说明]
是否继续执行?
常见操作参考
查看资源
Pod
# List pods with status
k1 get pods
k1 get pods -o wide # Include node and IP info
k1 get pods --show-labels # Show labels
k1 get pods -l app=simplex-api # Filter by label
# Pod details
k1 describe pod <pod-name>
# Watch pods in real-time
k1 get pods -w
Deployment
# List deployments
k1 get deployments
k1 get deploy -o wide
# Deployment details
k1 describe deployment <name>
# Rollout status
k1 rollout status deployment/<name>
# Rollout history
k1 rollout history deployment/<name>
Service 和 Endpoint
# List services
k1 get services
k1 get svc
# Service details with endpoints
k1 describe svc <name>
k1 get endpoints <name>
所有资源
# Get all common resources
k1 get all
# Get specific resource types
k1 get pods,svc,deploy
# Get all resources with labels
k1 get all -l app=simplex-api
日志和调试
查看日志
# Basic logs
k1 logs <pod-name>
# Follow logs (streaming)
k1 logs -f <pod-name>
# Last N lines
k1 logs --tail=100 <pod-name>
# Logs since time
k1 logs --since=1h <pod-name>
k1 logs --since=10m <pod-name>
# Previous container logs (after restart)
k1 logs --previous <pod-name>
# Multi-container pod
k1 logs <pod-name> -c <container-name>
# All containers in pod
k1 logs <pod-name> --all-containers=true
执行命令
# Execute command in container
k1 exec <pod-name> -- <command>
# Interactive shell
k1 exec -it <pod-name> -- /bin/sh
k1 exec -it <pod-name> -- /bin/bash
# Specific container in multi-container pod
k1 exec -it <pod-name> -c <container> -- /bin/sh
调试
# Pod events and status
k1 describe pod <pod-name>
# Get pod YAML
k1 get pod <pod-name> -o yaml
# Debug with ephemeral container
k1 debug <pod-name> -it --image=busybox
# Check resource usage
k1 top pods
k1 top nodes
部署管理
扩缩容
# Scale deployment
k1 scale deployment/<name> --replicas=3
# Autoscale
k1 autoscale deployment/<name> --min=2 --max=5 --cpu-percent=80
滚动更新
# Update image
k1 set image deployment/<name> <container>=<image>:<tag>
# Rollout status
k1 rollout status deployment/<name>
# Pause/resume rollout
k1 rollout pause deployment/<name>
k1 rollout resume deployment/<name>
# Rollback
k1 rollout undo deployment/<name>
k1 rollout undo deployment/<name> --to-revision=2
重启
# Restart deployment (rolling restart)
k1 rollout restart deployment/<name>
配置资源
ConfigMap
# List ConfigMaps
k1 get configmaps
k1 get cm
# View ConfigMap content
k1 describe cm <name>
k1 get cm <name> -o yaml
# Create from file
k1 create configmap <name> --from-file=<path>
# Create from literal
k1 create configmap <name> --from-literal=key=value
Secret
# List Secrets
k1 get secrets
# View Secret (base64 encoded)
k1 get secret <name> -o yaml
# Decode Secret value
k1 get secret <name> -o jsonpath='{.data.password}' | base64 -d
# Create Secret
k1 create secret generic <name> --from-literal=password=xxx
PersistentVolumeClaim
# List PVCs
k1 get pvc
# PVC details
k1 describe pvc <name>
网络操作
端口转发
# Forward local port to pod
k1 port-forward pod/<name> 8080:80
# Forward to service
k1 port-forward svc/<name> 8080:80
# Background port-forward
k1 port-forward pod/<name> 8080:80 &
暴露服务
# Expose deployment as service
k1 expose deployment/<name> --port=80 --target-port=8080
# Get service external IP
k1 get svc <name> -o jsonpath='{.status.loadBalancer.ingress[0].hostname}'
集群管理
节点
# List nodes
k1 get nodes
k1 get nodes -o wide
# Node details
k1 describe node <name>
# Node resource usage
k1 top nodes
命名空间
# List namespaces
k1 get namespaces
# Switch namespace (using kubens)
kubens <namespace>
# Create namespace
k1 create namespace <name>
上下文管理
# List contexts
kubectx
# Switch context
kubectx <context-name>
# Show current context
kubectl config current-context
资源监控
# Pod resource usage
k1 top pods
k1 top pods --sort-by=cpu
k1 top pods --sort-by=memory
# Node resource usage
k1 top nodes
# HPA status
k1 get hpa
k1 describe hpa <name>
输出格式
状态检查
提供简洁的摘要:
✅ Pod 状态 (production)
┌──────────────────────────┬─────────┬──────────┬─────────┐
│ Pod │ Status │ Restarts │ Age │
├──────────────────────────┼─────────┼──────────┼─────────┤
│ simplex-api-xxx-abc │ Running │ 0 │ 2d │
│ simplex-api-xxx-def │ Running │ 0 │ 2d │
└──────────────────────────┴─────────┴──────────┴─────────┘
故障排查
调查问题时,收集以下信息:
- Pod 状态:
k1 get pod <name> - Pod 事件:
k1 describe pod <name> - 最近的日志:
k1 logs --tail=50 <name> - 资源用量:
k1 top pod <name>
自定义输出格式
# JSON output
k1 get pods -o json
# YAML output
k1 get pod <name> -o yaml
# Custom columns
k1 get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase
# JSONPath
k1 get pods -o jsonpath='{.items[*].metadata.name}'
故障排查工作流
Pod 无法启动
- 检查 Pod 状态:
k1 get pod <name> - 检查事件:
k1 describe pod <name>(查看 Events 部分) - 检查日志:
k1 logs <name>或k1 logs --previous <name> - 常见问题:
ImagePullBackOff:检查镜像名称和注册表凭据CrashLoopBackOff:检查应用日志Pending:检查资源请求和节点容量
资源用量过高
- 检查 Pod 用量:
k1 top pods --sort-by=memory - 检查节点用量:
k1 top nodes - 检查 HPA 状态:
k1 get hpa - 考虑扩缩容:
k1 scale deployment/<name> --replicas=N
Service 无法访问
- 检查 Service:
k1 get svc <name> - 检查 Endpoint:
k1 get endpoints <name> - 检查 Pod 标签是否与 Service 选择器匹配
- 在集群内测试:
k1 exec -it <pod> -- curl <service>:<port>
集成说明
对于 GitOps 操作(通过 git 部署),请使用 ArgoCD 和 Kargo 技能:
- ArgoCD:Application 同步、回滚、状态
- Kargo:渐进式交付、货运晋级
对于 AWS 基础设施操作,请使用 AWS CLI 技能。
评论
支持 Markdown