设置多集群网格故障切换

本页面介绍了如何在多集群环境中使用 Cloud Service Mesh 设计和实现高可用性流量路由策略。下表介绍了预期行为:

集群状态 流量行为
两个集群的健康状况均良好 50% 的流量流向集群 A,50% 的流量流向集群 B
集群 A 变得不可用 100% 的流量流向集群 B
集群 A 恢复 自动恢复 50/50 拆分

前提条件

首先,本指南假定您已完成以下操作:

  • 创建了两个 GKE 集群,这两个集群已注册到同一舰队宿主项目,位于两个不同的区域,并已配置为使用 Cloud Service Mesh。
  • 在 Cloud Service Mesh 上设置多集群网格
  • Istio 控制平面已在这两个集群中安装和配置。
  • istio-ingressgateway 已部署并公开在至少一个集群(集群 A)中。
  • 在两个集群中部署了 hello-world 应用,并启用了 Sidecar 注入。

本实验使用以下区域:

  • 集群 Aeurope-west1
  • 集群 Bus-central1

设置多集群网格故障切换

  1. 使用 Cloud Service Mesh 代码库中的示例清单部署并应用公共入站流量网关:

    cat <<EOF> istio-ingressgateway.yaml
    apiVersion: networking.istio.io/v1alpha3
    kind: Gateway
    metadata:
      name: public-gateway
      namespace: default
    spec:
      selector:
        istio: ingressgateway
      servers:
      - port:
          number: 80
          name: http
          protocol: HTTP
        hosts:
        - '*'
    EOF
    
    kubectl apply -f istio-ingressgateway.yaml
    

    此网关会对外公开 hello-world 服务。

  2. 在集群 A 中创建并应用 VirtualService,以将流量路由到 hello-world 服务:

    cat <<EOF> virtual-service.yaml
    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: hello-world
      namespace: default
    spec:
      hosts:
      - '*'
      gateways:
      - public-gateway
      http:
      - route:
        - destination:
            host: hello-world.default.svc.cluster.local
    EOF
    
    kubectl apply -f virtual-service.yaml
    

    此配置会将来自网关的 HTTP 请求转发到服务。

  3. 配置并应用 DestinationRule 以实现基于地理位置的故障切换

    cat <<EOF> destination-rule.yaml
    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
      name: hello-world
      namespace: default
    spec:
      host: hello-world.default.svc.cluster.local
      trafficPolicy:
        connectionPool:
          http:
            http2MaxRequests: 100
        outlierDetection:
          consecutive5xxErrors: 1
          interval: 1s
          baseEjectionTime: 30s
          maxEjectionPercent: 100
        loadBalancer:
          localityLbSetting:
            enabled: true
            distribute:
            - from: europe-west1
              to:
                europe-west1: 50
                us-central1: 50
            - from: us-central1
              to:
                us-central1: 50
                europe-west1: 50
    EOF
    
    kubectl apply -f destination-rule.yaml
    

请注意以下几点:

  • DestinationRule 下的 localityLbSetting 可实现均匀的流量分配和自动故障切换。
  • 如果某个地理位置中的每个端点都不健康,maxEjectionPercent 允许 Istio 故障切换所有流量。
  • distribute:确保根据源集群的区域在集群之间实现 50/50 的平均分配。
  • 故障切换:当一个区域不可用时,系统会隐式处理故障切换,将 100% 的流量路由到运行状况良好的区域。
  • outlierDetection:在达到最低错误阈值后移除失败的端点。

验证

您现在可以通过以下方式验证此行为:

  1. 通过集群 A 中的 Ingress 网关发送请求。
  2. europe-west1 中的 hello-world pod 缩减为 0。
  3. 观察流量故障切换到 us-central1
  4. europe-west1 中将 Pod 重新扩缩,并验证流量分配是否恢复。