Kubernetes故障排查与问题定位:实战指南
一、故障排查概述
Kubernetes故障排查是运维工作中的重要环节。常见的故障类型包括:
- Pod故障:Pod无法启动、崩溃、重启
- 网络故障:Pod之间无法通信、服务不可访问
- 存储故障:持久化存储挂载失败、数据丢失
- 调度故障:Pod无法调度到节点
- 资源故障:CPU/内存不足、资源配额超限
二、Pod故障排查
2.1 查看Pod状态
# 查看所有Pod状态 kubectl get pods # 查看Pod详细状态 kubectl describe pod my-pod # 查看Pod事件 kubectl get events --field-selector involvedObject.name=my-pod # 查看Pod日志 kubectl logs my-pod kubectl logs my-pod -c my-container # 查看之前容器的日志 kubectl logs my-pod --previous2.2 常见Pod状态
| 状态 | 说明 | 排查方向 |
|---|---|---|
| Pending | Pod正在等待调度 | 检查节点资源、调度约束、网络策略 |
| Running | Pod正在运行 | 检查容器健康状态、日志 |
| CrashLoopBackOff | 容器反复崩溃 | 检查应用日志、资源配置 |
| ImagePullBackOff | 镜像拉取失败 | 检查镜像地址、镜像仓库认证 |
| InvalidImageName | 镜像名称无效 | 检查镜像名称拼写 |
2.3 Pod启动失败排查
# 检查镜像拉取 kubectl describe pod my-pod | grep -A 10 "Events" # 检查容器启动命令 kubectl get pod my-pod -o jsonpath='{.spec.containers[0].command}' # 检查环境变量 kubectl get pod my-pod -o jsonpath='{.spec.containers[0].env}' # 检查资源限制 kubectl get pod my-pod -o jsonpath='{.spec.containers[0].resources}'三、节点故障排查
3.1 查看节点状态
# 查看所有节点状态 kubectl get nodes # 查看节点详细信息 kubectl describe node my-node # 查看节点资源使用 kubectl top nodes # 检查节点条件 kubectl get nodes -o jsonpath='{.items[*].status.conditions}'3.2 节点不可用排查
# 检查节点是否被隔离 kubectl get node my-node -o jsonpath='{.spec.unschedulable}' # 检查节点污点 kubectl get node my-node -o jsonpath='{.spec.taints}' # 检查节点标签 kubectl get node my-node -o jsonpath='{.metadata.labels}' # 检查节点容量 kubectl get node my-node -o jsonpath='{.status.capacity}'3.3 Kubelet状态检查
# 检查Kubelet日志 journalctl -u kubelet -f # 检查Kubelet健康状态 curl -s http://localhost:10255/healthz # 检查Kubelet配置 cat /var/lib/kubelet/config.yaml四、网络故障排查
4.1 Pod网络连通性测试
# 在Pod中测试网络 kubectl exec -it my-pod -- ping google.com # 测试Pod之间通信 kubectl exec -it pod1 -- ping pod2-ip # 测试DNS解析 kubectl exec -it my-pod -- nslookup kubernetes.default # 测试Service访问 kubectl exec -it my-pod -- curl http://my-service:80804.2 Service故障排查
# 查看Service状态 kubectl get service my-service # 查看Service详情 kubectl describe service my-service # 检查Endpoint状态 kubectl get endpoints my-service # 检查Service选择器 kubectl get service my-service -o jsonpath='{.spec.selector}'4.3 Ingress故障排查
# 查看Ingress状态 kubectl get ingress my-ingress # 查看Ingress详情 kubectl describe ingress my-ingress # 检查Ingress Controller日志 kubectl logs -n ingress-nginx -l app.kubernetes.io/name=ingress-nginx # 测试Ingress访问 curl -H "Host: app.example.com" http://ingress-ip五、存储故障排查
5.1 PVC故障排查
# 查看PVC状态 kubectl get pvc my-pvc # 查看PVC详情 kubectl describe pvc my-pvc # 检查PV状态 kubectl get pv # 检查StorageClass kubectl get storageclass5.2 存储挂载失败
# 检查Pod挂载状态 kubectl describe pod my-pod | grep -A 5 "Volumes" # 检查节点存储配置 lsblk # 检查存储卷挂载点 df -h # 检查CSI驱动状态 kubectl get csidrivers六、资源故障排查
6.1 资源不足排查
# 查看Pod资源使用 kubectl top pods # 查看节点资源使用 kubectl top nodes # 检查资源配额 kubectl get resourcequota # 检查LimitRange kubectl get limitrange6.2 OOM Killer排查
# 检查系统日志中的OOM事件 grep -i "Out of memory" /var/log/messages # 检查Pod事件中的OOM kubectl get events | grep -i oom # 检查节点内存状态 free -h七、调度故障排查
7.1 Pod无法调度
# 查看Pod调度事件 kubectl describe pod my-pod | grep -A 20 "Events" # 检查节点选择器 kubectl get pod my-pod -o jsonpath='{.spec.nodeSelector}' # 检查节点亲和性 kubectl get pod my-pod -o jsonpath='{.spec.affinity}' # 检查节点污点 kubectl get nodes -o jsonpath='{.items[*].spec.taints}'7.2 调度器日志分析
# 查看调度器日志 kubectl logs -n kube-system -l component=kube-scheduler # 检查调度器配置 kubectl get configmap -n kube-system kube-scheduler # 检查Pod优先级 kubectl get priorityclass八、高级排查工具
8.1 使用kubectl debug
# 创建调试Pod kubectl debug my-pod -it --image=busybox # 复制Pod配置创建调试Pod kubectl debug my-pod -it --copy-to=debug-pod --image=busybox # 进入节点调试 kubectl debug node/my-node -it --image=busybox8.2 使用tcpdump抓包
# 在Pod中安装tcpdump kubectl exec -it my-pod -- apt-get update && apt-get install -y tcpdump # 抓包 kubectl exec -it my-pod -- tcpdump -i any port 8080 # 保存抓包文件 kubectl exec -it my-pod -- tcpdump -i any -w /tmp/capture.pcap8.3 使用strace调试
# 在Pod中安装strace kubectl exec -it my-pod -- apt-get update && apt-get install -y strace # 追踪系统调用 kubectl exec -it my-pod -- strace -p <pid> # 追踪进程启动 kubectl exec -it my-pod -- strace -f -s 2048 ./my-app九、日志分析
9.1 收集Pod日志
# 收集所有Pod日志 kubectl logs my-pod > my-pod.log # 收集容器日志 kubectl logs my-pod -c my-container > my-container.log # 收集之前容器的日志 kubectl logs my-pod --previous > my-pod-previous.log # 实时查看日志 kubectl logs -f my-pod9.2 分析系统日志
# 查看Kubernetes组件日志 kubectl logs -n kube-system -l component=kube-apiserver kubectl logs -n kube-system -l component=kube-controller-manager kubectl logs -n kube-system -l component=kube-scheduler # 查看节点系统日志 journalctl -u kubelet journalctl -u docker journalctl -u containerd9.3 使用ELK分析日志
apiVersion: v1 kind: ConfigMap metadata: name: fluentd-config data: fluent.conf: | <source> @type tail path /var/log/containers/*.log tag kubernetes.* format json </source> <match kubernetes.**> @type elasticsearch host elasticsearch.default.svc.cluster.local port 9200 index_name k8s-logs </match>十、故障排查流程图
1. Pod无法启动 ├─→ 检查Pod状态 (kubectl get pods) ├─→ 查看Pod详情 (kubectl describe pod) ├─→ 查看Pod日志 (kubectl logs) └─→ 检查事件 (kubectl get events) 2. 服务无法访问 ├─→ 检查Service状态 (kubectl get svc) ├─→ 检查Endpoint (kubectl get endpoints) ├─→ 检查Pod网络 (kubectl exec ping) └─→ 检查Ingress (kubectl get ingress) 3. 节点不可用 ├─→ 检查节点状态 (kubectl get nodes) ├─→ 查看节点详情 (kubectl describe node) ├─→ 检查Kubelet日志 (journalctl) └─→ 检查节点资源 (kubectl top nodes) 4. 存储挂载失败 ├─→ 检查PVC状态 (kubectl get pvc) ├─→ 检查PV状态 (kubectl get pv) ├─→ 检查StorageClass (kubectl get sc) └─→ 检查节点存储 (df -h)十一、最佳实践
11.1 日志收集
- 配置集中式日志收集系统
- 设置日志保留策略
- 配置日志轮转
11.2 监控告警
- 配置关键指标监控
- 设置合理的告警阈值
- 配置告警通知渠道
11.3 定期检查
- 定期检查集群健康状态
- 定期清理无用资源
- 定期备份重要数据
11.4 故障演练
- 定期进行故障演练
- 完善故障恢复流程
- 文档化故障处理步骤
参考资料:
- Kubernetes故障排查指南
- kubectl debug文档
- Kubernetes日志文档