You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否通过Istio单负载均衡器按域名路由多TCP服务?

Istio TCP 基于域名路由实现方案及建议

核心问题澄清

TCP本身是无状态的传输层协议,没有内置的域名携带机制,但Istio的Gateway和VirtualService中的hosts字段在TCP场景下的作用和HTTP不同——它并不是直接解析TCP包中的域名,而是用于关联Gateway与VirtualService的匹配关系,结合SNI(Server Name Indication)扩展实现路由。只有当客户端发起TCP连接时携带SNI信息,Istio入口网关才能识别请求域名并完成分流。

正确配置示例

Gateway配置

为3306端口配置支持SNI的TCP监听,明确指定要处理的域名:

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: product-gw
  namespace: product-ns
spec:
  selector:
    istio: ingressgateway
  servers:
  - port:
      number: 3306
      name: tcp-mariadb
      protocol: TCP
    hosts:
    - "A.my.domain"
    - "B.my.domain"

VirtualService配置

针对两个域名分别配置TCP路由规则,通过sniHosts匹配SNI信息:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: mariadb-vs
  namespace: product
spec:
  hosts:
  - "A.my.domain"
  - "B.my.domain"
  gateways:
  - product-ns/product-gw  # 跨命名空间引用需加命名空间前缀
  tcp:
  - match:
    - port: 3306
      sniHosts:
      - "A.my.domain"
    route:
    - destination:
        host: product-db-1.product.svc.cluster.local
        port:
          number: 3306
  - match:
    - port: 3306
      sniHosts:
      - "B.my.domain"
    route:
    - destination:
        host: product-db-2.product.svc.cluster.local
        port:
          number: 3306

关键注意事项

  • 客户端SNI支持:必须确保MariaDB客户端支持SNI,比如使用mysql命令时可通过--ssl-mode=REQUIRED --ssl-verify-server-cert参数触发SNI发送(部分客户端可能需额外配置)。
  • Gateway的hosts字段:此处的hosts用于声明网关要处理的域名范围,需与VirtualService中的域名一致,并非直接解析TCP包内容。
  • 命名空间一致性:若Gateway与VirtualService不在同一命名空间,VirtualService引用Gateway时必须带上命名空间前缀。

替代方案评估:新增负载均衡器

如果客户端不支持SNI,或不想依赖SNI机制,可考虑为每个MariaDB服务单独创建LoadBalancer类型的Service:

  • 优点:配置简单,无需依赖Istio的SNI路由,兼容性更强,适合老旧客户端。
  • 缺点:会额外占用云服务商的负载均衡器资源,增加成本,提升运维管理复杂度。

总结

  • 若客户端支持SNI,优先采用Istio的SNI路由方案,复用现有负载均衡器,节省资源与成本。
  • 若客户端不支持SNI,再考虑新增负载均衡器的方案。

内容的提问来源于stack exchange,提问作者Nukaccino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 05:12:27