排查路径
kubectl get pods → 看状态
kubectl describe pod → 看 Events
kubectl logs → 看日志
常见 Pod 状态
| 状态 | 含义 | 常见原因 |
|---|---|---|
Pending | 未调度 | 资源不足、调度约束 |
CrashLoopBackOff | 崩溃重启 | 应用异常、配置错误 |
ImagePullBackOff | 镜像拉取失败 | 镜像不存在、认证失败 |
OOMKilled | 内存溢出 | 内存限制过低 |
CrashLoopBackOff 排查
最常见故障,排查步骤:
# 查看上次崩溃日志
kubectl logs <pod> --previous
# 查看退出码
kubectl get pod <pod> -o jsonpath='{.status.containerStatuses[0].lastState.terminated.exitCode}'
退出码含义:
- 137:OOMKilled → 增加
resources.limits.memory - 1:应用错误 → 查应用日志
- 126/127:命令不存在或权限问题
ImagePullBackOff 排查
kubectl describe pod <pod> | grep -A5 Events
常见原因:镜像名拼写错误、仓库需认证、网络不通。
配置镜像拉取凭证:
spec:
imagePullSecrets:
- name: registry-secret
Pod 处于 Pending
# 查看调度失败原因
kubectl get pod <pod> -o jsonpath='{.status.conditions[?(@.type=="PodScheduled")].message}'
常见输出:
Insufficient cpu→ CPU 资源不足had untolerated taint→ 节点有污点未容忍didn't match node affinity→ 节点亲和性不匹配
网络不通排查
# 检查 Service 是否有 Endpoints
kubectl get endpoints <svc>
# 检查标签是否匹配
kubectl get pods --show-labels
kubectl get svc <svc> -o jsonpath='{.spec.selector}'
# DNS 解析测试
kubectl exec -it <pod> -- nslookup kubernetes.default
速查表
Pod 不启动 → describe pod → 看 Events
Pod 崩溃 → logs --previous → 看退出码
Service 不通 → get endpoints → 检查标签
DNS 不解析 → 检查 CoreDNS
调度失败 → describe pod → 看 conditions
OOMKilled → describe pod → 加 memory limit
总结
K8s 故障排查核心:善用 kubectl describe,理解每个异常状态的含义,逐层缩小范围。