如何在 Istio 中同时使用 Gateway API 与 VirtualService?

解读

国内云原生面试中,**“Gateway API 与 VirtualService 能否并存”**是高频追问,背后考察两点:

  1. 你是否理解 Istio 1.19+ 已把 Gateway API 作为首选流量模型,而 VirtualService 属于 Istio 自有 API;
  2. 你是否能在 不中断业务 的前提下完成双模型共存、灰度切换与统一治理。
    面试官常通过“并存”场景,验证你对 CRD 优先级、数据面冲突、证书生命周期、可观测性 的落地经验。

知识点

  • Gateway API 资源层级:GatewayClass → Gateway → HTTPRoute/TLSRoute/GRPCRoute,完全取代 了原来的 Ingress 资源,但 并未内置 熔断、重试、故障注入等语义。
  • VirtualService 定位:Istio 独有 CRD,专注 七层流量治理(权重分流、镜像、超时、重试、故障注入、跨域、重写、重定向)。
  • 并存规则:同一 port+host+path 只能被一个模型接管;HTTPRoute 的 parentRefs 字段 若指向同一 Gateway,则 VirtualService 的 selector 必须显式排除 该 host,否则 Pilot 会报“conflict”且以 Gateway API 为准。
  • 证书管理:Gateway API 使用 Secret 的 credentialName 字段,必须 与 Gateway 同 namespace;VirtualService 本身不承载证书,依赖 Gateway 对象或 DR 的 caCertificates。
  • 灰度策略:国内大厂普遍采用 “南北向先切 Gateway API,东西向继续 VirtualService” 的混合模式,利用 istio.io/rev=stable 与 canary 双控制面做 金丝雀发布,再逐步把 VirtualService 的七层规则 下沉到 HTTPRoute 的 filters(需 Istio 1.20+ 支持 HTTPRouteFilter 扩展)。

答案

  1. 环境准备
    在 ACK-TKE-Openshift 任一 1.24+ 集群 中,通过 istioctl install --set profile=ambient 启用 Gateway API CRD(国内镜像站需提前 kubectl kustomize github.com/kubernetes-sigs/gateway-api/config/crd/experimental | sed 's|gcr.io|registry.aliyuncs.com/google_containers|g' | kubectl apply -f -)。

  2. 双模型并存的最小实践
    a. 用 Gateway API 暴露入口:

    apiVersion: gateway.networking.k8s.io/v1beta1
    kind: Gateway
    metadata:
      name: prod-gateway
      namespace: istio-ingress
    spec:
      gatewayClassName: istio
      listeners:
      - name: http
        protocol: HTTP
        port: 80
        hostname: "*.example.cn"
    

    b. 用 HTTPRoute 完成 精准路由(权重 90/10):

    apiVersion: gateway.networking.k8s.io/v1beta1
    kind: HTTPRoute
    metadata:
      name: canary-route
      namespace: default
    spec:
      parentRefs:
      - name: prod-gateway
        namespace: istio-ingress
      hostnames: ["foo.example.cn"]
      rules:
      - matches:
        - path:
            type: PathPrefix
            value: /
        backendRefs:
        - name: foo-v1
          port: 8000
          weight: 90
        - name: foo-v2
          port: 8000
          weight: 10
    

    c. 对 东西向流量 继续使用 VirtualService 做 故障注入(与 HTTPRoute 无冲突):

    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: foo-eastwest
      namespace: default
    spec:
      hosts: ["foo.default.svc.cluster.local"]
      http:
      - fault:
          delay:
            percentage: 100
            fixedDelay: 5s
        route:
        - destination:
            host: foo.default.svc.cluster.local
    

    d. 冲突检查:
    执行 istioctl analyze -n default,确认 “IST0132 Gateway and VirtualService overlapping hosts” 无报错;若出现,在 VirtualService 的 gateways 字段中显式去掉 istio-ingress/prod-gateway,仅保留 mesh。

  3. 证书与 HTTPS
    在 阿里云证书中心 申请通配符证书,创建 Secret(type: kubernetes.io/tls)后,在 Gateway 中引用:

    tls:
      mode: Terminate
      certificateRefs:
      - name: example-cn-tls
        kind: Secret
    

    注意:Secret 必须与 Gateway 同 namespace,否则 Istiod 会报 “MISSING” 事件。

  4. 可观测性
    通过 阿里云 ARMS 或自建 Prometheus,抓取 istio_gateway_api_ 前缀指标,与 istio_request_total{source_app="istio-ingressgateway"} 做 联合告警,确保 双模型流量 100% 可追踪。

  5. 回滚策略
    若发现 HTTPRoute 的 filters 不支持复杂重写,可 秒级回滚:

    kubectl annotate httproute canary-route helm.sh/resource-policy=keep
    kubectl replace -f vs-backup.yaml   # 重新应用原 VirtualService
    

拓展思考

  • 全量迁移时机:等待 **Istio 1.22 将 HTTPRoute 的 extensionRef filters 毕业到 v1,即可把 VirtualService 的 fault、mirror、retry 策略 全部下沉到 Gateway API,届时 VirtualService 仅保留东西向灰度能力,实现 “一套 API 管南北,一套 API 管东西” 的国内合规架构。
  • 多集群联邦:在 华为云 CCE-Stack 或腾讯 TCS 的 多集群联邦 场景下,**Gateway API 的 GatewayClass 可绑定 Istio multi-cluster secret,而 VirtualService 通过 exportTo 字段 实现 跨集群服务级别隔离,二者结合可做到 “入口统一,出口隔离” 的金融级合规方案。