K8s 容器化安全产品性能问题排查指南
一、基础性能监控与诊断
1. 资源使用情况快速检查
# 查看节点资源使用情况
kubectl top nodes
# 查看 Pod 资源使用情况(指定命名空间)
kubectl top pods -n security-namespace
# 查看容器级资源使用(精确到容器)
kubectl top pods -n security-namespace --containers
2. 容器日志分析
# 获取安全产品 Pod 名称
kubectl get pods -n security-namespace
# 查看容器日志(实时流)
kubectl logs -f <pod-name> -n security-namespace -c <container-name>
# 查看过去一段时间的日志(如最近10分钟)
kubectl logs --since=10m <pod-name> -n security-namespace
# 多容器 Pod 查看特定容器日志
kubectl logs <pod-name> -n security-namespace -c <container-name>
3. 事件与状态检查
# 查看集群事件(过滤异常事件)
kubectl get events --sort-by=.metadata.creationTimestamp | grep -i "warning\|error"
# 查看 Pod 详细状态
kubectl describe pod <pod-name> -n security-namespace
二、深入性能分析工具
1. 使用 kubectl debug
创建诊断容器
# 创建与故障容器共享网络和卷的诊断容器
kubectl debug -it <pod-name> -n security-namespace --image=busybox --target=<container-name>
# 在诊断容器中执行命令(如网络连通性测试)
ping <service-name>
curl http://localhost:8080/healthz
2. 使用 kubectl exec
进入容器
# 交互式进入容器 shell
kubectl exec -it <pod-name> -n security-namespace -c <container-name> -- /bin/bash
# 在容器内执行性能分析命令
top # 查看进程 CPU/内存占用
ps aux # 查看所有进程
df -h # 检查磁盘空间
netstat -anp # 查看网络连接
3. 资源限制与请求配置检查
# 查看 Pod 资源限制配置
kubectl get pod <pod-name> -n security-namespace -o yaml | grep -A 10 resources:
# 示例输出:
resources:
requests:
cpu: "200m"
memory: "512Mi"
limits:
cpu: "1"
memory: "1Gi"
三、高级性能分析工具
1. 使用 kubectl cp
导出性能数据
# 从容器导出性能分析文件
kubectl cp <pod-name>:/path/to/profile.out -n security-namespace ./profile.out
# 示例:导出 Go 程序的 pprof 数据
kubectl cp <security-pod>:/tmp/cpu.prof -n security-namespace ./cpu.prof
go tool pprof http://localhost:8080/debug/pprof/profile
2. 使用 Metrics Server 和 Custom Metrics
# 确保 Metrics Server 已安装
kubectl get deployments metrics-server -n kube-system
# 查询自定义指标(如安全产品特有指标)
kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq .
3. 使用 Prometheus 和 Grafana 监控
# 部署 Prometheus 和 Grafana(示例使用 helm)
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install prometheus prometheus-community/kube-prometheus-stack
# 导入预定义的安全产品监控仪表盘
kubectl apply -f https://raw.githubusercontent.com/prometheus-operator/kube-prometheus/main/manifests/grafana-dashboardDefinitions.yaml
四、网络性能诊断
1. 网络策略验证
# 查看应用于安全产品 Pod 的网络策略
kubectl get networkpolicies --all-namespaces
# 测试网络连通性(使用临时 Pod)
kubectl run test-pod --image=busybox --restart=Never -- sleep 3600
kubectl exec test-pod -- wget -qO- http://<security-service>:<port>
2. 服务与端点检查
# 查看服务配置
kubectl get service <security-service> -n security-namespace -o yaml
# 检查端点是否正常
kubectl get endpoints <security-service> -n security-namespace
五、自动恢复与弹性配置
1. 探针配置检查与优化
# 查看 livenessProbe 和 readinessProbe 配置
kubectl get pod <pod-name> -n security-namespace -o yaml | grep -A 20 probes:
# 示例优化配置:
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 3
2. HPA(水平 Pod 自动扩缩)配置
# 创建基于 CPU 的 HPA
kubectl autoscale deployment <security-deployment> -n security-namespace --min=2 --max=5 --cpu-percent=70
# 查看 HPA 状态
kubectl get hpa -n security-namespace
六、常见问题排查思路
1. CPU 使用率过高
- 排查步骤:
- 使用
kubectl top pods
确定高 CPU 容器 - 进入容器执行
top
命令定位具体进程 - 导出进程 profile 数据(如 Go 程序的 pprof)
- 分析代码或配置是否存在无限循环、资源泄漏
- 使用
2. 内存泄漏排查
- 排查步骤:
- 监控内存使用趋势(使用 Prometheus/Grafana)
- 设置合理的内存 limit 和 request
- 使用工具(如 Heapster、cadvisor)分析内存增长模式
- 导出堆转储文件(heap dump)进行详细分析
3. 网络连接问题
- 排查步骤:
- 检查 Service 和 Endpoint 是否正常
- 使用
kubectl exec
在容器内测试网络连通性 - 检查网络策略是否限制流量
- 使用
tcpdump
或netstat
分析网络流量
总结
通过 K8s 原生工具(如 top、logs、describe)结合第三方监控系统(Prometheus、Grafana),可以构建从宏观到微观的全方位诊断体系。建议为安全产品配置合理的资源限制、探针和自动扩缩策略,同时建立标准化的故障排查流程,确保快速定位并解决性能问题。