Skip to content

03 - K8s 故障排查指南

排查流程

问题发生


┌────────────────────┐
│ 1. 看 Pod 状态      │  kubectl get pods
│    是否 Running?    │
└────────┬───────────┘

    ┌────┴────┐
    │ 不正常   │ 正常但功能异常
    ▼         ▼
┌──────┐  ┌──────┐
│看事件 │  │看日志 │  kubectl logs
│看描述 │  │看指标 │
└──────┘  └──────┘

Pod 状态异常排查

Pod 卡在 Pending

bash
kubectl describe pod <pod-name>
# 查看 Events 部分

常见原因和解决:

原因Events 关键词解决
资源不足Insufficient cpu/memory增加节点或降低 requests
节点选择器无匹配MatchNodeSelector检查 nodeSelector/affinity
PVC 未绑定pod has unbound PersistentVolumeClaims检查 PVC/PV/StorageClass
污点不容忍didn't tolerate添加 toleration

Pod 卡在 ContainerCreating

bash
kubectl describe pod <pod-name>
kubectl get events --sort-by=.metadata.creationTimestamp
原因Events 关键词解决
拉取镜像失败ErrImagePull / ImagePullBackOff检查镜像名、tag、仓库权限
ConfigMap/Secret 不存在configmap not found先创建 ConfigMap/Secret
挂载卷失败MountVolume.SetUp failed检查 PVC 和存储后端

Pod 处于 CrashLoopBackOff

容器启动后又崩溃,反复重启。

bash
# 查看当前日志
kubectl logs <pod-name>

# 查看上一次崩溃的日志
kubectl logs <pod-name> --previous

# 查看 Pod 描述
kubectl describe pod <pod-name>
原因解决
应用启动报错看日志修复代码
探针配置不当调整探针参数
资源不足 (OOMKilled)增加内存限制
权限问题检查 securityContext

Pod 处于 ImagePullBackOff

bash
# 检查镜像名是否正确
kubectl describe pod <pod-name> | grep Image

# 如果是私有仓库,检查 imagePullSecret
kubectl get secret <secret-name> -o yaml

# 手动测试拉取
docker pull <image-name>

Service 网络排查

bash
# 1. 确认 Service 存在
kubectl get svc <service-name>

# 2. 确认 Endpoints 有 Pod IP
kubectl get endpoints <service-name>
# 如果 endpoints 为空 → selector 不匹配

# 3. 确认 Pod 标签匹配
kubectl get pods --show-labels
kubectl get pods -l <selector>

# 4. 在集群内测试连通性
kubectl run test --rm -it --image=busybox -- sh
wget -qO- http://<service-name>:<port>
nslookup <service-name>

# 5. 检查 kube-proxy
kubectl get pods -n kube-system -l k8s-app=kube-proxy
kubectl logs -n kube-system <kube-proxy-pod>

DNS 排查

bash
# 测试 DNS 解析
kubectl run test-dns --rm -it --image=busybox -- nslookup kubernetes.default

# 检查 CoreDNS
kubectl get pods -n kube-system -l k8s-app=kube-dns
kubectl logs -n kube-system <coredns-pod>

节点排查

bash
# 查看节点状态
kubectl get nodes
kubectl describe node <node-name>

# 关注 Conditions 部分
# Ready: 节点是否健康
# MemoryPressure: 内存是否紧张
# DiskPressure: 磁盘是否紧张
# PIDPressure: 进程数是否过多

# 查看节点资源使用
kubectl top nodes

# 查看节点上的 Pod
kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=<node-name>

常用排查命令速查

bash
# 事件排序查看
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl get events -n <namespace> --sort-by=.lastTimestamp

# 查看所有 非 Running 的 Pod
kubectl get pods -A --field-selector status.phase!=Running

# 查看 Pod 重启次数
kubectl get pods -o wide --sort-by='.status.containerStatuses[0].restartCount'

# 查看 资源使用
kubectl top pods
kubectl top pods --sort-by=memory
kubectl top pods --sort-by=cpu

# 进入 Pod 调试(即使容器崩溃也能进入)
kubectl debug <pod-name> -it --image=busybox

# 临时复制一个 Pod 用于调试
kubectl debug <pod-name> -it --copy-to=debug-pod --container=app -- sh

# 查看 YAML(获取完整的资源定义)
kubectl get pod <pod-name> -o yaml

# 查看 API 资源的字段说明
kubectl explain pod.spec.containers
kubectl explain deployment.spec.strategy

排查思维导图

Pod 异常
├── Pending
│   ├── 资源不足 → kubectl describe → 扩节点/降 requests
│   ├── 调度约束 → 检查 nodeSelector/affinity/taint
│   └── PVC 未绑定 → 检查 StorageClass/PV
├── ContainerCreating
│   ├── 镜像拉取失败 → 检查镜像名/仓库权限/网络
│   ├── 配置缺失 → 检查 ConfigMap/Secret 是否存在
│   └── 卷挂载失败 → 检查 PVC/CSI
├── CrashLoopBackOff
│   ├── 应用错误 → kubectl logs --previous
│   ├── OOMKilled → 增加 memory limits
│   └── 探针失败 → 调整探针配置
├── Running 但不工作
│   ├── Service Endpoints 为空 → 检查 label selector
│   ├── DNS 解析失败 → 检查 CoreDNS
│   └── 网络不通 → 检查 NetworkPolicy/CNI
└── Evicted
    ├── 节点资源压力 → kubectl describe node
    └── 设置 PDB 防止误驱逐

下一步

04 - 大厂 K8s 最佳实践