跳转至

10. 测试验证

本章提供一套完整的验证流程,确保集群功能正常。

10.1 自动化验证脚本

我们提供了一个脚本 scripts/verify_cluster.sh,用于快速检查集群健康度。

10.1.1 脚本功能

  1. 检查所有节点 Ready 状态。
  2. 检查 kube-system 核心组件(CoreDNS, Traefik, Metrics-server)是否运行。
  3. 创建一个测试 Nginx Deployment 并验证连通性。

10.1.2 运行脚本

chmod +x scripts/verify_cluster.sh
./scripts/verify_cluster.sh

10.2 Sonobuoy 合规性测试

Sonobuoy 是 CNCF 官方的 Kubernetes 一致性测试工具。

10.2.1 安装

# 下载二进制
wget https://github.com/vmware-tanzu/sonobuoy/releases/download/v0.56.17/sonobuoy_0.56.17_linux_amd64.tar.gz
tar -xvf sonobuoy_0.56.17_linux_amd64.tar.gz
mv sonobuoy /usr/local/bin/

10.2.2 运行测试

# 运行快速模式(约 10 分钟)
sonobuoy run --mode quick

# 查看状态
sonobuoy status

# 获取报告
sonobuoy retrieve

10.3 故障排查手册 (Cheat Sheet)

现象 可能原因 排查命令
Node NotReady Agent 挂了 / 网络中断 systemctl status k3s-agent, journalctl -u k3s-agent -f
Pod Pending 资源不足 / 污点不匹配 kubectl describe pod <pod-name>
Service 无法访问 Endpoints 为空 / 标签错误 kubectl get ep <svc-name>
DNS 解析失败 CoreDNS 异常 / 网络策略拦截 kubectl logs -n kube-system -l k8s-app=kube-dns

10.4 常用命令速查

# 强制删除 Terminating 的 Pod
kubectl delete pod <name> --grace-period=0 --force

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

# 临时启动一个工具 Pod
kubectl run -it --rm debug --image=busybox -- sh

# 导出集群所有资源 YAML
kubectl get all -A -o yaml > cluster-backup.yaml