Skip to content
0

文章发布较早,内容可能过时,阅读注意甄别。

前言

在 Kubernetes 生产环境中,快速定位和解决问题是运维的核心技能。本文总结了常见故障的排查思路和方法。

1. Pod 故障排查

1.1 Pod 无法启动

查看 Pod 状态

bash
# 查看 Pod 详细信息
kubectl describe pod <pod-name> -n <namespace>

# 查看 Pod 日志
kubectl logs <pod-name> -n <namespace>

# 查看上一个容器的日志(崩溃重启场景)
kubectl logs <pod-name> -n <namespace> --previous

常见状态及原因

状态原因解决方案
ImagePullBackOff镜像拉取失败检查镜像名称、私有仓库认证
CrashLoopBackOff容器启动后立即崩溃查看日志,检查启动命令
Pending无法调度到节点检查资源限制、节点亲和性
ContainerCreating容器创建中检查存储、网络、镜像拉取

示例:解决镜像拉取问题

bash
# 1. 检查镜像是否存在
docker pull <image-name>

# 2. 创建私有仓库密钥
kubectl create secret docker-registry regcred \
  --docker-server=<registry-url> \
  --docker-username=<username> \
  --docker-password=<password> \
  -n <namespace>

# 3. 在 Pod 中引用密钥
# imagePullSecrets:
#   - name: regcred

1.2 Pod 频繁重启

bash
# 查看重启次数
kubectl get pod -n <namespace>

# 查看事件
kubectl get events -n <namespace> --sort-by='.lastTimestamp'

# 查看资源使用
kubectl top pod <pod-name> -n <namespace>

常见原因

  • 🔴 OOM (Out of Memory)
  • 🔴 健康检查失败
  • 🔴 应用程序错误
yaml
# 调整资源限制
resources:
  requests:
    memory: "256Mi"
    cpu: "250m"
  limits:
    memory: "512Mi"
    cpu: "500m"

# 调整探针配置
livenessProbe:
  initialDelaySeconds: 30  # 增加初始延迟
  periodSeconds: 10
  timeoutSeconds: 5
  failureThreshold: 3

2. 网络问题排查

2.1 Pod 之间无法通信

bash
# 进入 Pod 进行网络测试
kubectl exec -it <pod-name> -n <namespace> -- /bin/sh

# 测试 DNS 解析
nslookup kubernetes.default

# 测试 Service 连接
curl http://<service-name>.<namespace>.svc.cluster.local

# 检查网络策略
kubectl get networkpolicy -n <namespace>

2.2 Service 无法访问

bash
# 查看 Service 详情
kubectl describe service <service-name> -n <namespace>

# 查看 Endpoints
kubectl get endpoints <service-name> -n <namespace>

# 检查标签选择器是否匹配
kubectl get pods -n <namespace> --show-labels

调试技巧

bash
# 创建临时调试 Pod
kubectl run debug-pod --image=nicolaka/netshoot -it --rm -- /bin/bash

# 测试目标 Service
curl http://<service-name>.<namespace>.svc.cluster.local:8080

3. 节点问题排查

3.1 节点状态异常

bash
# 查看节点状态
kubectl get nodes

# 查看节点详情
kubectl describe node <node-name>

# 查看节点资源使用
kubectl top node <node-name>

常见节点状态

状态含义处理方法
Ready正常-
NotReady节点不可用检查 kubelet、网络、磁盘
SchedulingDisabled已驱逐(cordon)kubectl uncordon <node>
DiskPressure磁盘压力清理磁盘空间
MemoryPressure内存压力释放内存或添加资源

3.2 节点资源不足

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

# 驱逐节点上的 Pod
kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data

# 恢复节点调度
kubectl uncordon <node-name>

4. 存储问题排查

4.1 PVC 无法绑定

bash
# 查看 PVC 状态
kubectl get pvc -n <namespace>

# 查看 PV 状态
kubectl get pv

# 查看 StorageClass
kubectl get storageclass

# 详细信息
kubectl describe pvc <pvc-name> -n <namespace>

常见问题

  • ✖️ 没有匹配的 PV
  • ✖️ StorageClass 不存在
  • ✖️ 存储容量不足

4.2 挂载失败

bash
# 查看 Pod 事件
kubectl describe pod <pod-name> -n <namespace>

# 检查节点上的挂载点
kubectl exec -it <pod-name> -n <namespace> -- df -h

5. 常用排查命令集合

5.1 查看资源

bash
# 查看所有命名空间的 Pod
kubectl get pods --all-namespaces

# 查看特定命名空间的所有资源
kubectl get all -n <namespace>

# 以 YAML 格式输出
kubectl get pod <pod-name> -n <namespace> -o yaml

# 以 JSON 格式输出并使用 jq 过滤
kubectl get pod <pod-name> -n <namespace> -o json | jq '.spec.containers[].image'

5.2 实时监控

bash
# 实时查看 Pod 日志
kubectl logs -f <pod-name> -n <namespace>

# 实时查看事件
kubectl get events -n <namespace> --watch

# 实时查看 Pod 状态
kubectl get pods -n <namespace> --watch

5.3 资源使用监控

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

# 查看 Pod 资源使用
kubectl top pods -n <namespace>

# 按内存排序
kubectl top pods -n <namespace> --sort-by=memory

# 按 CPU 排序
kubectl top pods -n <namespace> --sort-by=cpu

6. 高级排查技巧

6.1 使用临时容器调试(K8s 1.18+)

bash
# 创建临时容器(ephemeral container)
kubectl debug <pod-name> -n <namespace> --image=busybox --target=<container-name>

6.2 复制 Pod 进行调试

bash
# 复制 Pod 并修改命令(用于调试启动失败的容器)
kubectl debug <pod-name> -n <namespace> --copy-to=debug-pod --container=<container-name> -- sleep 3600

6.3 查看 API Server 审计日志

bash
# 在 master 节点上查看
tail -f /var/log/kubernetes/audit/audit.log

7. 故障排查清单

✅ Pod 故障排查清单

  • 查看 Pod 状态和事件
  • 检查容器日志
  • 验证镜像是否可拉取
  • 检查资源限制和配额
  • 验证存储挂载
  • 测试网络连通性
  • 检查健康检查配置

✅ 网络故障排查清单

  • 验证 DNS 解析
  • 检查 Service Endpoints
  • 验证网络策略
  • 测试 Pod 间通信
  • 检查 Ingress 配置
  • 验证负载均衡器状态

✅ 节点故障排查清单

  • 检查节点状态
  • 验证 kubelet 服务
  • 检查磁盘和内存使用
  • 验证容器运行时
  • 检查系统日志
  • 验证网络连接

8. 常见错误代码

错误代码含义解决方法
137容器被 SIGKILL 终止(OOM)增加内存限制
139段错误(Segmentation Fault)检查应用程序代码
143容器被 SIGTERM 终止正常终止,检查是否频繁
1应用程序错误查看应用日志

参考资料

最近更新