Knative 基于平台中 Istio 的性能调优与最佳实践
Knative 与 Istio
- Istio 作为 Knative 的默认网络层解决方案:Istio 用于流量管理、可观察性、安全性等,Knative 提供事件驱动和 serverless 能力,支持代码聚焦、快速扩展至零、快速入门 serverless 等。
- Istio 在 Knative 平台中的应用:
- Ingress Gateway:Knative 默认使用 Istio 作为 Ingress Gateway,通过 Secret Discovery Service (SDS) 监控和挂载
istio-system 下的秘密,支持多租户 HTTPS。
- 流量拆分与蓝绿部署:通过 Istio VirtualService 实现 90/10 的流量分配,支持蓝绿部署。
- 安全:启用 mutual TLS 保障端到端安全,通过 Istio 认证策略控制对 Knative 服务的访问。
性能瓶颈分析与调优
- 性能指标:多 shard Kubernetes 集群,每个集群需支持 1000 个间隔 5 秒的 Knative 服务创建,路由就绪时间 <= 30 秒。
- 基准测试工具:Kperf,用于生成 Knative 服务创建工作负载并聚合服务就绪时长。
- 性能优化:
- 内存使用优化:
- Ingress gateway 内存线性增长,Istio 1.6.0+ 的 envoy mem release fix 解决了内存消耗问题。
- Istiod 内存因大量 Knative 服务增加,通过
pilot 内存优化解决。
- 资源调优:通过 Metrics 监控 Istio & Knative 组件的 CPU 和内存,避免 CPU 限流和 OOM。
- Istio 可扩展性优化:
- 问题检测:
ingress_lb_ready 持续时间过长(30 秒)。
ingress_ready 存在随机峰值和 500+ 服务时长达 800 秒的问题。
- Envoy 日志显示 transient 503 UH "no healthy upstream" 错误,Grafana 显示 Pilot 推送延迟高。
- 解决方案:
- Istio 1.7.1 修复随机峰值问题。
- 启用
PILOT_DEBOUNCE_AFTER=1s 减轻 Envoy 负载。
- Istio 1.9.x 支持背压(flow control),通过
PILOT_ENABLE_FLOW_CONTROLEnvironment variable 启用,最终通过 envoydelta -XDS 推送解决。
充分利用 Istio 特性提升 Knative 可扩展性
- 启用 Istio Mesh:
- 数据流:通过 mTLS 保障安全。
- 影响:
- 初始容器增加 5 秒启动时间。
- Sidecar 默认需要全 mesh 信息,影响可扩展性。
- Activator 需通过 IP 探测服务端点,Istiod 发现端点后推送耗时。
- Envoy sidecar 冷启动耗时约 2 秒。
- 优化措施:
- Istio CNI 插件:
- 将 iptables 配置移至 CNI,但引入
istio-validation 容器,可通过修改注入模板移除。
- 确保 worker node 上 daemonset pod 先于 Knative pod 调度,或通过 crontab 检测并重启 pod。
- 减少 mesh 大小:
- 限制 mesh 至 namespace 级别,仅限用户 namespace 内的 mesh。
- 直接访问 Pod IP:
- Knative 需直接通过 IP 访问 Pod,当前通过 Kubeservice 管理,社区正在通过 Destination rules 支持 Pod IP 直接访问(Knative issue: https://github.com/istio/istio/issues/23494)。
参考文献
- IBM Cloud Code Engine:https://www.ibm.com/cloud/code-engine
- Kperf:https://github.com/knative-sandbox/kperf
- Istio 性能分析:https://github.com/istio/istio/wiki/Analyzing-Istio-Performance
- Envoy 和 Istiod 调试:https://istio.io/latest/docs/ops/diagnostic-tools/proxy-cmd/
- Pilot agent 配置:https://istio.io/latest/docs/reference/commands/pilot-agent/
- Istio Sidecar 配置:https://istio.io/latest/docs/reference/config/networking/sidecar/
- Istio CNI 插件:https://istio.io/latest/docs/setup/additional-setup/cni/