K8s 场景:Pod 一直 CrashLoopBackOff,怎么定位问题?
什么是 CrashLoopBackOff?
Pod 启动 → 崩溃退出 → K8s 重启 → 再次崩溃 → 等待时间递增重启
(10s → 20s → 40s → 最大5min)
📋 系统化排查流程
第一步:获取基本信息
kubectl get pod <pod-name> -n <namespace>
kubectl describe pod <pod-name> -n <namespace>
第二步:查看日志(最关键)
kubectl logs <pod-name> -n <namespace>
kubectl logs <pod-name> -n <namespace> --previous
kubectl logs <pod-name> -n <namespace> -f
kubectl logs <pod-name> -c <container-name> --previous
🚨 Exit Code 速查表
Exit Code 含义 常见原因
0 正常退出 容器任务执行完毕,不该作为常驻服务
1 通用错误 应用启动报错、配置错误
2 误用命令 Shell 命令错误
137 SIGKILL (128+9) OOM 被杀、手动 kill -9
139 段错误 (128+11) 内存访问违规
143 SIGTERM (128+15) 优雅终止超时
126 权限不足 命令无执行权限
127 命令不存在 镜像内找不到命令
🔧 常见原因及解决方案
原因1:应用启动失败(Exit Code 1)
kubectl logs <pod-name> --previous | tail -50
YAML
kubectl exec <pod-name> -- env | grep APP_
env:
- name: DB_HOST
valueFrom:
secretKeyRef:
name: db-secret
key: host # ← 确认 secret 和 key 存在
原因2:OOM 内存溢出(Exit Code 137)
Bash
kubectl describe pod <pod-name> | grep -A5 "Last State"
kubectl describe node <node-name> | grep -A10 "Events"
YAML
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi" # ← 适当增大
cpu: "500m"
原因3:镜像问题
Bash
kubectl describe pod <pod-name> | grep -E "Image|Pull"
YAML
image: myapp:v1.2.3 # ← 确认 tag 存在
imagePullSecrets:
- name: registry-secret
docker pull <image-name>
原因4:启动命令/入口错误(Exit Code 127)
Bash
kubectl run debug --image=<your-image> \
--command -- sleep 3600
kubectl exec -it debug -- /bin/sh
YAML
spec:
containers:
- name: app
image: myapp:v1
command: ["/bin/sh"] # ← 确认路径存在
args: ["-c", "start.sh"] # ← 确认脚本可执行
原因5:健康检查失败
Bash
kubectl describe pod <pod-name> | grep -E "Liveness|Readiness|probe"
YAML
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30 # ← 给应用足够启动时间
periodSeconds: 10
failureThreshold: 3 # ← 失败几次才重启
timeoutSeconds: 5 # ← 超时时间
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 10
原因6:权限/安全上下文问题
Bash
kubectl logs <pod-name> --previous | grep -E "permission|denied|unauthorized"
YAML
securityContext:
runAsUser: 1000
runAsNonRoot: true
readOnlyRootFilesystem: true # ← 只读根目录,需确保应用不写根目录
volumeMounts:
- name: tmp-dir
mountPath: /tmp # ← 提供可写临时目录
volumes:
- name: tmp-dir
emptyDir: {}
🛠️ 进阶排查工具
临时调试容器(不修改原镜像)
Bash
kubectl debug -it <pod-name> \
--image=busybox \
--target=<container-name>
kubectl debug <pod-name> \
-it \
--copy-to=debug-pod \
--image=busybox \
-- sh
替换启动命令强制 Pod 存活
YAML
spec:
containers:
- name: app
image: myapp:v1
command: ["sleep"]
args: ["infinity"] # ← 覆盖原启动命令
kubectl exec -it <pod-name> -- /bin/sh
查看资源使用情况
Bash
kubectl top pod <pod-name> -n <namespace>
kubectl get events -n <namespace> \
--sort-by='.lastTimestamp' | grep <pod-name>
📊 排查决策树
text
CrashLoopBackOff
│
├── kubectl logs --previous 有输出?
│ ├── YES → 分析应用错误日志
│ │ ├── 配置/连接错误 → 检查 ConfigMap/Secret/网络
│ │ ├── OOM → 增加内存限制
│ │ └── 启动命令错误 → 检查 command/args
│ │
│ └── NO → kubectl describe pod 查看 Events
│ ├── OOMKilled → 增加内存限制
│ ├── Error Code 137 → 检查内存
│ ├── ImagePullBackOff → 检查镜像
│ └── Liveness probe failed → 调整探针参数
│
└── 无法复现 → 替换 command 为 sleep infinity 进入容器排查
✅ 排查检查清单
text
□ kubectl logs --previous 查看崩溃日志
□ kubectl describe pod 查看 Exit Code 和 Events
□ 确认镜像 Tag 正确且可拉取
□ 确认 ConfigMap/Secret 存在且内容正确
□ 确认依赖服务(DB/Redis)可访问
□ 检查资源限制是否合理(尤其内存)
□ 检查 LivenessProbe 的 initialDelaySeconds
□ 检查文件权限和 SecurityContext
□ 检查启动命令路径是否正确
□ 查看 Node 级别事件是否有异常
💡 核心原则:--previous 参数查看崩溃前日志是最高效的定位手段,80% 的问题都能从应用日志直接找到答案。
题目与答案就在本页;收藏、批注和错题重练在应用内