设置多集群网格故障切换
本页面介绍了如何在多集群环境中使用 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 注入。
本实验使用以下区域:
- 集群 A:
europe-west1 - 集群 B:
us-central1
设置多集群网格故障切换
使用 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服务。在集群 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 请求转发到服务。
配置并应用
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:在达到最低错误阈值后移除失败的端点。
验证
您现在可以通过以下方式验证此行为:
- 通过集群 A 中的 Ingress 网关发送请求。
- 将
europe-west1中的hello-worldpod 缩减为 0。 - 观察流量故障切换到
us-central1。 - 在
europe-west1中将 Pod 重新扩缩,并验证流量分配是否恢复。