在 CentOS 上运行的 Kubernetes(k8s)中,容器退出(CrashLoopBackOff / Completed / Error) 是非常常见的问题。原因通常可以归为以下几类,我按最常见 → 较少见来说明,并给出排查方法。
Kubernetes 容器必须有一个前台进程一直运行,否则就会退出。
典型错误示例:
CMD /opt/start.sh # 脚本里是后台运行
或
CMD service nginx start
✅ 正确方式:
CMD ["nginx", "-g", "daemon off;"]
查看:
kubectl describe pod <pod-name>
kubectl logs <pod-name>
常见情况:
查看日志:
kubectl logs <pod-name>
kubectl logs <pod-name> --previous
CentOS + k8s 常见:
检查:
kubectl describe pod <pod-name>
看到:
Reason: OOMKilled
Exit Code: 137
✅ 解决:
resources.limits.memory示例错误:
Liveness probe failed: HTTP probe failed with status 500
✅ 检查:
kubectl describe pod <pod-name>
CentOS 默认开启 SELinux。
常见现象:
临时验证:
setenforce 0
✅ 长期方案:
:z / :ZCentOS 的 firewalld 可能拦截:
systemctl status firewalld
✅ 建议:
systemctl stop firewalld
systemctl disable firewalld
kubectl describe pod
kubectl describe pod
看到:
Failed to pull image
ImagePullBackOff
CentOS 7:cgroup v1
CentOS 8/9:cgroup v2(部分旧镜像不支持)
# 1. 看 pod 状态
kubectl get pod
# 2. 看详细信息
kubectl describe pod <pod>
# 3. 看日志
kubectl logs <pod>
kubectl logs <pod> --previous
# 4. 进容器(如果能起来)
kubectl exec -it <pod> -- sh
如果你愿意,可以贴出:
kubectl describe pod <pod> 输出kubectl logs <pod>我可以直接告诉你具体是哪一个原因 + 怎么改 YAML。