Kubernetes在Ubuntu上的调试方法
在Ubuntu上调试Kubernetes集群前,需先确认系统环境与核心组件的健康状态:
sudo apt update && sudo apt upgrade确保Ubuntu系统为最新版本;检查kubeadm、kubelet、kubectl版本兼容性(如kubeadm version、kubelet --version、kubectl version),避免因版本冲突导致问题。kubectl get nodes查看所有节点状态,若节点显示为NotReady,则执行kubectl describe node <node-name>获取详细事件(如网络插件未就绪、kubelet未运行等原因)。sudo systemctl status kubelet确认其运行状态;若未启动,使用sudo systemctl start kubelet启动;若存在故障,用sudo journalctl -u kubelet -f查看实时日志,定位具体错误(如证书过期、配置文件错误)。容器是Kubernetes应用运行的基本单元,调试需从Pod状态与内部环境入手:
kubectl get pods --all-namespaces列出所有命名空间的Pod,重点关注STATUS为Error、CrashLoopBackOff或Pending的Pod(如镜像拉取失败、资源不足)。kubectl logs <pod-name> -n <namespace>获取Pod内应用的日志(如启动错误、运行时异常);若需实时跟踪日志,添加-f参数(如kubectl logs -f <pod-name>)。kubectl exec -it <pod-name> -- /bin/bash进入Pod的交互式终端(若Pod内无bash,可替换为sh);若Pod基于distroless等精简镜像(无shell),需使用调试容器:kubectl debug -it <pod-name> --image=busybox --target=<container-name>(--target指定需调试的容器)。kubectl cp <namespace>/<pod-name>:/path/to/app /local/path将应用复制到本地,再用Docker运行(如docker run -it --rm -v /local/path:/app your-image),方便使用本地工具(如gdb、vim)调试。若Pod级别调试无法解决问题(如节点网络异常、kubelet故障),需通过节点调试工具深入检查:
kubectl debug node:Kubernetes提供的节点调试功能,可创建临时调试Pod访问节点文件系统。命令示例:kubectl debug node/<node-name> -it --image=ubuntu(--image指定调试镜像,如Ubuntu);调试Pod会将节点根文件系统挂载到/host目录,可通过/host/var/log/kubelet.log(kubelet日志)、/host/var/log/syslog(系统日志)、/host/var/log/containerd.log(容器运行时日志)查看关键日志。--profile=sysadmin参数:kubectl debug node/<node-name> -it --image=ubuntu --profile=sysadmin(默认调试Pod为非特权模式,无法执行mount、iptables等命令)。tcpdump、nslookup),可使用自定义镜像(如包含ubuntu+调试工具的镜像):kubectl debug node/<node-name> -it --image=ubuntu-debug(需提前构建或拉取包含所需工具的镜像)。Ubuntu作为节点操作系统,需关注以下常见问题:
/etc/kubernetes/kubelet.conf或/var/lib/kubelet/config.yaml,确保clusterDNS、clusterIP、runtimeEndpoint(如unix:///var/run/containerd/containerd.sock)等配置正确。df -h检查磁盘使用情况,若/分区使用率超过80%,需清理旧日志(如/var/log/syslog)、未使用的镜像(docker system prune)或停止的容器(docker rm $(docker ps -aq)),避免因磁盘空间不足导致kubelet故障。sudo swapoff -a,永久禁用需编辑/etc/fstab,注释掉包含swap的行(如/dev/sdaX none swap sw 0 0)。