📖 kubectl 排查速查

按使用场景分组,点击"复制"按钮一键复制命令

Pod 状态排查
查看 Pod 列表与节点分布
kubectl get pods -n <ns> -o wide
查看 Pod 事件详情(启动失败首选)
kubectl describe pod <pod> -n <ns>
查看容器日志(最近 200 行)
kubectl logs <pod> -n <ns> -c <container> --tail=200
查看上次崩溃前的日志(CrashLoopBackOff)
kubectl logs <pod> -n <ns> --previous
按时间排序查看命名空间事件
kubectl get events -n <ns> --sort-by=.lastTimestamp
容器内调试
进入容器执行 shell
kubectl exec -it <pod> -n <ns> -- /bin/sh
镜像无 shell 时挂临时调试容器
kubectl debug -it <pod> --image=busybox --target=<container>
端口转发到本机调试
kubectl port-forward <pod> 8080:80 -n <ns>
从容器拷出文件
kubectl cp <ns>/<pod>:/path/file ./file
查看容器资源限制
kubectl get pod <pod> -n <ns> -o jsonpath='{.spec.containers[*].resources}'
资源与服务连通
查看 Pod CPU / 内存占用
kubectl top pod -n <ns>
查看 Service 实际后端端点
kubectl get endpoints <svc> -n <ns>
查看 Ingress 转发规则
kubectl get ingress -n <ns> -o wide
按标签筛出同一服务的 Pod
kubectl get pods -l app=<name> -n <ns>
按重启次数排序(定位频繁重启)
kubectl get pods -n <ns> --sort-by='.status.containerStatuses[0].restartCount'