Kubernetes故障排查与性能调优完全指南

Kubernetes故障排查与性能调优

故障排查方法论

故障排查流程


1. 确认问题
   ├── 观察现象(Pod无法启动、服务不可达)
   └── 收集信息(kubectl describe、kubectl logs)

2. 分析原因
   ├── 资源问题(CPU/内存/磁盘不足)
   ├── 配置问题(镜像错误、配置错误)
   ├── 网络问题(Service异常、NetworkPolicy)
   └── 权限问题(RBAC、Secret权限)

3. 解决问题
   ├── 修复配置
   ├── 调整资源
   └── 恢复服务

4. 总结经验
   ├── 更新文档
   └── 预防措施

常用排查命令

Pod故障排查


# 查看Pod状态
kubectl get pods -A -o wide

# 查看Pod详细信息
kubectl describe pod  -n 

# 查看Pod日志
kubectl logs  -n 
kubectl logs  -n  --previous

# 进入Pod容器
kubectl exec -it  -n  -- /bin/bash

# 查看所有Pod事件
kubectl get events --sort-by='.metadata.creationTimestamp'

# 强制删除Pod
kubectl delete pod  -n  --force --grace-period=0

Service故障排查


# 查看Service
kubectl get svc -A

# 查看Endpoints
kubectl get endpoints  -n 

# 测试Service连通性
kubectl exec -it  -n  -- curl :

# DNS解析测试
kubectl exec -it  -n  -- nslookup 

# 查看kube-proxy日志
kubectl logs -n kube-system -l k8s-app=kube-proxy

节点故障排查


# 查看节点状态
kubectl get nodes
kubectl describe node 

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

# 查看节点上的Pod
kubectl get pods -o wide --field-selector=spec.nodeName=

#  cordon节点(标记不可调度)
kubectl cordon 

# uncordon节点
kubectl uncordon 

# drain节点
kubectl drain  --ignore-daemonsets --delete-emptydir-data

常见问题与解决方案

问题1:Pod一直处于Pending状态


# 排查步骤
kubectl describe pod 

# 可能原因
# 1. 资源不足
kubectl get nodes
kubectl describe nodes | grep -A5 "Allocated resources"

# 2. 节点选择器不匹配
kubectl get nodes --show-labels
kubectl describe pod  | grep -A5 "Node-Selectors"

# 3. PV挂载失败
kubectl describe pod  | grep -A10 "Events"

问题2:Pod一直处于CrashLoopBackOff


# 查看重启次数
kubectl get pod  -o jsonpath='{.status.containerStatuses[0].restartCount}'

# 查看容器退出码
kubectl describe pod  | grep -A5 "Last State"

# 查看应用日志
kubectl logs  --previous

# 常见原因
# - 应用启动失败(配置错误、依赖服务不可用)
# - 内存溢出(OOMKilled)
# - 健康检查过于严格

问题3:Service无法访问


# 检查Endpoints
kubectl get endpoints 

# 如果Endpoints为空
# 1. 检查Pod标签是否匹配
kubectl get pods --show-labels
kubectl get svc  -o jsonpath='{.spec.selector}'

# 2. 检查Pod是否Running
kubectl get pod -l 

# 3. 检查网络策略
kubectl get networkpolicy -A
kubectl describe networkpolicy 

问题4:镜像拉取失败


# 检查镜像名称
kubectl describe pod  | grep "Failed to pull image"

# 可能原因
# 1. 镜像名称错误
# 2. 私有仓库未配置Secret
# 3. 镜像仓库不可达
# 4. 镜像tag不存在

# 解决方案
# 配置imagePullSecrets
kubectl create secret docker-registry my-registry \
  --docker-server= \
  --docker-username= \
  --docker-password=

# Pod引用Secret
spec:
  imagePullSecrets:
  - name: my-registry

问题5:PV挂载失败


# 查看PVC状态
kubectl get pvc

# 查看PV详情
kubectl describe pv 

# 检查存储类
kubectl get storageclass
kubectl describe storageclass 

# 检查节点对PVC的支持
kubectl describe node  | grep "Attach"

性能调优

资源优化


# 合理设置资源请求和限制
spec:
  containers:
  - name: app
    image: myapp:v1
    resources:
      requests:
        cpu: 500m      # 基于实际测试
        memory: 512Mi  # 基于P95使用量
      limits:
        cpu: 1000m
        memory: 1Gi

调度优化


# 分散Pod到不同节点
spec:
  affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - myapp
        topologyKey: kubernetes.io/hostname

网络优化


# 使用Service Mesh优化流量
# 配置合适的HPA阈值
# 使用Ingress Controller优化入口流量

etcd性能优化


# 监控etcd性能
etcdctl endpoint health
etcdctl endpoint status
etcdctl endpoint statistics --cluster

# 优化建议
# - 使用SSD存储
# - 限制历史快照保留数量
# - 适当调整心跳间隔

监控与告警

关键监控指标


# 节点层面
- node_cpu_usage
- node_memory_usage
- node_disk_io
- node_network_traffic

# Pod层面
- pod_cpu_usage
- pod_memory_usage
- pod_restarts
- pod_ready

# 集群层面
- api_server_requests
- scheduler_schedule_attempts
- etcd_server_leader_changes

日志收集


# 应用日志
kubectl logs -l app=myapp --tail=100

# 系统组件日志
kubectl logs -n kube-system -l k8s-app=kube-proxy
kubectl logs -n kube-system -l k8s-app=kube-apiserver

# 审计日志
# 查看/var/log/kubernetes/audit/目录下日志

调试工具

kubectl插件


# 安装kubectl-debug
curl -Lo kubectl-debug https://github.com/aylei/kubectl-debug/releases/download/v0.2.0/kubectl-debug-v0.2.0-linux-amd64.tar.gz
tar xzf kubectl-debug-v0.2.0-linux-amd64.tar.gz
mv kubectl-debug /usr/local/bin/

# 使用kubectl-debug
kubectl debug  -n 

# 安装kubectl-neat(清理输出)
kubectl neat get pods -A

网络诊断


# 安装krew
curl -fsSL https://krew.xyz/install.sh | bash

# 安装网络诊断工具
kubectl krew install netshoot

# 使用netshoot
kubectl netshoot  -n 

最佳实践

故障恢复


# 1. 回滚Deployment
kubectl rollout undo deployment/

# 2. 扩缩容
kubectl scale deployment/ --replicas=3

# 3. 重启Pod
kubectl delete pod 

# 4. 使用Velero恢复
velero restore create --from-backup 

预防措施

1. 定期备份:Etcd快照、Velero备份 2. 监控告警:设置合理的阈值和告警 3. 容量规划:提前预估资源需求 4. 灰度发布:使用Canary/Rolling Update 5. 演练恢复:定期进行故障演练

发表回复

后才能评论