| name | troubleshooting |
| description | Kubernetes 故障排查 |
| version | 1.0.0 |
| author | terminal-skills |
| tags | ["kubernetes","troubleshooting","k8s","debug","diagnosis"] |
Kubernetes 故障排查
概述
故障诊断、事件分析、资源调试等技能。
集群状态检查
节点状态
kubectl get nodes
kubectl get nodes -o wide
kubectl describe node node-name
kubectl top nodes
kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.conditions[?(@.type=="Ready")].status}{"\n"}{end}'
组件状态
kubectl get componentstatuses
kubectl get cs
kubectl get pods -n kube-system
kubectl get --raw='/healthz'
kubectl get --raw='/readyz'
Pod 故障排查
Pod 状态分析
kubectl get pods -o wide
kubectl get pods --field-selector status.phase!=Running
kubectl describe pod pod-name
kubectl get events --sort-by='.lastTimestamp'
kubectl get events --field-selector involvedObject.name=pod-name
常见 Pod 状态
Pending
kubectl describe pod pod-name | grep -A 10 Events
kubectl describe pod pod-name | grep -A 5 "Conditions"
kubectl describe nodes | grep -A 5 "Allocated resources"
kubectl get pvc
kubectl describe pvc pvc-name
ImagePullBackOff
kubectl describe pod pod-name | grep -A 5 "Events"
kubectl get pod pod-name -o jsonpath='{.spec.containers[*].image}'
kubectl get pod pod-name -o jsonpath='{.spec.imagePullSecrets}'
docker pull image-name
CrashLoopBackOff
kubectl logs pod-name
kubectl logs pod-name --previous
kubectl describe pod pod-name | grep -A 5 "Last State"
kubectl describe pod pod-name | grep -A 10 "Limits"
kubectl exec -it pod-name -- sh
OOMKilled
kubectl describe pod pod-name | grep -i oom
kubectl top pod pod-name
kubectl set resources deployment/deploy-name --limits=memory=512Mi
网络故障排查
Service 连通性
kubectl get svc service-name
kubectl describe svc service-name
kubectl get endpoints service-name
kubectl run test --rm -it --image=busybox -- sh
wget -qO- http://service-name:port
nslookup service-name
DNS 排查
kubectl get pods -n kube-system -l k8s-app=kube-dns
kubectl logs -n kube-system -l k8s-app=kube-dns
kubectl run test --rm -it --image=busybox -- nslookup kubernetes
kubectl run test --rm -it --image=busybox -- nslookup service-name.namespace.svc.cluster.local
网络策略
kubectl get networkpolicy
kubectl describe networkpolicy policy-name
kubectl run test --rm -it --image=nicolaka/netshoot -- bash
curl -v http://service-name:port
存储故障排查
PV/PVC 问题
kubectl get pv
kubectl get pvc
kubectl describe pvc pvc-name
kubectl get storageclass
kubectl describe storageclass sc-name
挂载问题
kubectl describe pod pod-name | grep -A 10 "Volumes"
kubectl exec pod-name -- df -h
kubectl exec pod-name -- ls -la /mount/path
资源问题排查
资源配额
kubectl get resourcequota -n namespace
kubectl describe resourcequota quota-name
kubectl get limitrange -n namespace
kubectl describe limitrange limit-name
资源使用
kubectl top pods
kubectl top pods --containers
kubectl top nodes
kubectl describe pod pod-name | grep -A 10 "Requests"
日志收集
容器日志
kubectl logs pod-name
kubectl logs pod-name -c container-name
kubectl logs pod-name --previous
kubectl logs -f pod-name --since=1h
kubectl logs -l app=nginx --all-containers
系统日志
journalctl -u kubelet -f
journalctl -u containerd -f
journalctl -u docker -f
kubectl logs -n kube-system kube-apiserver-master
调试工具
kubectl debug
kubectl debug pod-name -it --image=busybox --target=container-name
kubectl debug pod-name -it --copy-to=debug-pod --container=debug --image=nicolaka/netshoot
kubectl debug node/node-name -it --image=busybox
临时调试 Pod
kubectl run netshoot --rm -it --image=nicolaka/netshoot -- bash
kubectl run dnsutils --rm -it --image=gcr.io/kubernetes-e2e-test-images/dnsutils -- sh
kubectl run debug --rm -it --image=busybox -- sh
常见场景
场景 1:应用无法访问
kubectl get pods -l app=myapp
kubectl get svc myapp-service
kubectl get endpoints myapp-service
kubectl get ingress
kubectl describe ingress myapp-ingress
kubectl run test --rm -it --image=busybox -- wget -qO- http://myapp-service
场景 2:Pod 调度失败
kubectl describe pod pod-name | grep -A 20 Events
kubectl describe nodes | grep -A 10 "Allocated resources"
kubectl describe nodes | grep Taints
kubectl get pod pod-name -o yaml | grep -A 5 tolerations
kubectl get pod pod-name -o yaml | grep -A 20 affinity
场景 3:集群证书问题
kubeadm certs check-expiration
kubeadm certs renew all
openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -dates
故障排查清单
| 问题类型 | 排查命令 |
|---|
| Pod 状态 | kubectl describe pod, kubectl logs |
| 网络问题 | kubectl get svc/ep, DNS 测试 |
| 存储问题 | kubectl get pv/pvc, kubectl describe |
| 资源问题 | kubectl top, kubectl describe quota |
| 调度问题 | kubectl describe pod, 检查节点资源 |
| 认证问题 | 检查 ServiceAccount, RBAC |