Kubernetes 排障命令速查
常用命令
kubectl get pods -A -o wide
kubectl get nodes -o wide
kubectl get events -A --sort-by=.lastTimestamp
kubectl describe pod <pod> -n <ns>
kubectl logs <pod> -n <ns> --previous
kubectl logs <pod> -n <ns> -c <container> -f
kubectl exec -it <pod> -n <ns> -- sh
kubectl top pod -n <ns>
kubectl top node
kubectl rollout status deploy/<name> -n <ns>
kubectl rollout history deploy/<name> -n <ns>
kubectl rollout undo deploy/<name> -n <ns>
kubectl port-forward svc/<name> 8080:80 -n <ns>
kubectl auth can-i --list -n <ns>
kubectl api-resources
kubectl cordon <node>
kubectl drain <node> --ignore-daemonsets --delete-emptydir-data
场景速查
| 现象 |
先看 |
| Pod Pending |
describe pod 的 Events、节点资源、taints、PVC |
| CrashLoopBackOff |
logs --previous、退出码、探针、内存 limit |
| ImagePullBackOff |
镜像名/tag、imagePullSecrets、节点网络 |
| Pod Evicted |
节点 DiskPressure/MemoryPressure、磁盘和日志 |
| 节点 NotReady |
kubelet、容器运行时、CNI、磁盘/内存 |
| Service 不通 |
Endpoints、selector、NetworkPolicy、kube-proxy |
| 集群 DNS 失败 |
CoreDNS Pod、kube-dns Endpoints、53 端口策略 |
| PVC Pending |
StorageClass、provisioner、容量/access mode |
易错点
kubectl logs 只显示当前容器;重启前的日志要加 --previous。
describe 的 Events 只保留最近一段时间,排障要尽快看。
exec 需要容器里有 shell;distroless 镜像要用 debug 容器或 kubectl debug。
- Service 的 selector 必须匹配 Pod 标签;Endpoints 为空先查这里。
drain 会驱逐 Pod;先确认副本和 PDB,别把单副本服务打挂。
- 命名空间和 context 容易搞错;命令里显式带
-n 和 --context。
kubectl top 需要 metrics-server;没装时不是节点资源不足。
相关命令
kubectl — 集群操作
crictl — 节点侧容器诊断
排障 分支的 K8s 页面 — 具体故障处理