|
| 1 | +--- |
| 2 | +title: 在多集群 Ambient 安装中配置故障转移行为 |
| 3 | +description: 使用 waypoint 配置 Ambient 多集群网格中的异常值检测和故障转移行为。 |
| 4 | +weight: 70 |
| 5 | +keywords: [kubernetes,multicluster,ambient] |
| 6 | +test: yes |
| 7 | +owner: istio/wg-environments-maintainers |
| 8 | +prev: /zh/docs/ambient/install/multicluster/verify |
| 9 | +--- |
| 10 | +按照本指南,使用 waypoint 代理自定义 Ambient 多集群 Istio 安装中的故障转移行为。 |
| 11 | + |
| 12 | +在继续操作之前,请务必按照[多集群安装指南](/zh/docs/ambient/install/multicluster)之一完成 |
| 13 | +Ambient 多集群 Istio 安装,并验证安装是否正常工作。 |
| 14 | + |
| 15 | +在本指南中,我们将基于用于验证多集群安装的 `HelloWorld` 应用程序进行构建。 |
| 16 | +我们将为 `HelloWorld` 服务配置本地故障转移, |
| 17 | +使其优先使用客户端所在集群中的端点(使用 `DestinationRule`),并部署一个 waypoint 代理来强制执行此配置。 |
| 18 | + |
| 19 | +## 部署 waypoint 代理 {#deploy-waypoint-proxy} |
| 20 | + |
| 21 | +为了配置异常值检测并自定义服务的故障转移行为,我们需要一个 waypoint 代理。 |
| 22 | +首先,将 waypoint 代理部署到网格中的每个集群: |
| 23 | + |
| 24 | +{{< text bash >}} |
| 25 | +$ istioctl --context "${CTX_CLUSTER1}" waypoint apply --name waypoint --for service -n sample --wait |
| 26 | +$ istioctl --context "${CTX_CLUSTER2}" waypoint apply --name waypoint --for service -n sample --wait |
| 27 | +{{< /text >}} |
| 28 | + |
| 29 | +确认集群 `cluster1` 上的 waypoint 代理部署状态: |
| 30 | + |
| 31 | +{{< text bash >}} |
| 32 | +$ kubectl --context "${CTX_CLUSTER1}" get deployment waypoint --namespace sample |
| 33 | +NAME READY UP-TO-DATE AVAILABLE AGE |
| 34 | +waypoint 1/1 1 1 137m |
| 35 | +{{< /text >}} |
| 36 | + |
| 37 | +确认集群 `cluster2` 上的 waypoint 代理部署状态: |
| 38 | + |
| 39 | +{{< text bash >}} |
| 40 | +$ kubectl --context "${CTX_CLUSTER2}" get deployment waypoint --namespace sample |
| 41 | +NAME READY UP-TO-DATE AVAILABLE AGE |
| 42 | +waypoint 1/1 1 1 138m |
| 43 | +{{< /text >}} |
| 44 | + |
| 45 | +请等待所有 waypoint 代理准备就绪。 |
| 46 | + |
| 47 | +在每个集群中配置 `HelloWorld` 服务以使用 waypoint 代理: |
| 48 | + |
| 49 | +{{< text bash >}} |
| 50 | +$ kubectl --context "${CTX_CLUSTER1}" label svc helloworld -n sample istio.io/use-waypoint=waypoint |
| 51 | +$ kubectl --context "${CTX_CLUSTER2}" label svc helloworld -n sample istio.io/use-waypoint=waypoint |
| 52 | +{{< /text >}} |
| 53 | + |
| 54 | +最后,这一步专门针对 waypoint 代理的多集群部署,将每个集群中的 waypoint |
| 55 | +代理服务标记为全局服务,就像之前对 `HelloWorld` 服务所做的那样: |
| 56 | + |
| 57 | +{{< text bash >}} |
| 58 | +$ kubectl --context "${CTX_CLUSTER1}" label svc waypoint -n sample istio.io/global=true |
| 59 | +$ kubectl --context "${CTX_CLUSTER2}" label svc waypoint -n sample istio.io/global=true |
| 60 | +{{< /text >}} |
| 61 | + |
| 62 | +两个集群中的 `HelloWorld` 服务现在都配置为使用 waypoint 代理, |
| 63 | +但 waypoint 代理目前还没有任何实际作用。 |
| 64 | + |
| 65 | +## 配置本地故障转移 {#configure-locality-failover} |
| 66 | + |
| 67 | +要配置本地故障转移,请在 `cluster1` 中创建并应用 `DestinationRule`: |
| 68 | + |
| 69 | +{{< text bash >}} |
| 70 | +$ kubectl --context "${CTX_CLUSTER1}" apply -n sample -f - <<EOF |
| 71 | +apiVersion: networking.istio.io/v1 |
| 72 | +kind: DestinationRule |
| 73 | +metadata: |
| 74 | + name: helloworld |
| 75 | +spec: |
| 76 | + host: helloworld.sample.svc.cluster.local |
| 77 | + trafficPolicy: |
| 78 | + outlierDetection: |
| 79 | + consecutive5xxErrors: 1 |
| 80 | + interval: 1s |
| 81 | + baseEjectionTime: 1m |
| 82 | + loadBalancer: |
| 83 | + simple: ROUND_ROBIN |
| 84 | + localityLbSetting: |
| 85 | + enabled: true |
| 86 | + failoverPriority: |
| 87 | + - topology.istio.io/cluster |
| 88 | +EOF |
| 89 | +{{< /text >}} |
| 90 | + |
| 91 | +在 `cluster2` 中也应用相同的 `DestinationRule`: |
| 92 | + |
| 93 | +{{< text bash >}} |
| 94 | +$ kubectl --context "${CTX_CLUSTER2}" apply -n sample -f - <<EOF |
| 95 | +apiVersion: networking.istio.io/v1 |
| 96 | +kind: DestinationRule |
| 97 | +metadata: |
| 98 | + name: helloworld |
| 99 | +spec: |
| 100 | + host: helloworld.sample.svc.cluster.local |
| 101 | + trafficPolicy: |
| 102 | + outlierDetection: |
| 103 | + consecutive5xxErrors: 1 |
| 104 | + interval: 1s |
| 105 | + baseEjectionTime: 1m |
| 106 | + loadBalancer: |
| 107 | + simple: ROUND_ROBIN |
| 108 | + localityLbSetting: |
| 109 | + enabled: true |
| 110 | + failoverPriority: |
| 111 | + - topology.istio.io/cluster |
| 112 | +EOF |
| 113 | +{{< /text >}} |
| 114 | + |
| 115 | +此 `DestinationRule` 配置以下内容: |
| 116 | + |
| 117 | +- 为 `HelloWorld` 服务配置[异常值检测](/zh/docs/reference/config/networking/destination-rule/#OutlierDetection)。 |
| 118 | + 此规则指示 waypoint 代理如何识别服务的端点何时出现异常。这是故障转移正常运行所必需的。 |
| 119 | + |
| 120 | +- [故障转移优先级](/zh/docs/reference/config/networking/destination-rule/#LocalityLoadBalancerSetting)指示 |
| 121 | + waypoint 代理在路由请求时如何确定端点的优先级。在本例中, |
| 122 | + waypoint 代理将优先处理同一集群中的端点,而不是其他集群中的端点。 |
| 123 | + |
| 124 | +有了这些策略,当端点与 waypoint 代理位于同一集群中且根据异常值检测配置被认为运行正常时, |
| 125 | +waypoint 代理将优先选择这些端点。 |
| 126 | + |
| 127 | +## 验证流量是否保持在本地集群内 {#verify-traffic-stays-in-local-cluster} |
| 128 | + |
| 129 | +从 `cluster1` 上的 `curl` Pod 向 `HelloWorld` 服务发送请求: |
| 130 | + |
| 131 | +{{< text bash >}} |
| 132 | +$ kubectl exec --context "${CTX_CLUSTER1}" -n sample -c curl \ |
| 133 | + "$(kubectl get pod --context "${CTX_CLUSTER1}" -n sample -l \ |
| 134 | + app=curl -o jsonpath='{.items[0].metadata.name}')" \ |
| 135 | + -- curl -sS helloworld.sample:5000/hello |
| 136 | +{{< /text >}} |
| 137 | + |
| 138 | +现在,如果您多次重复此请求并验证 `HelloWorld` 版本应始终为 `v1`, |
| 139 | +因为流量始终位于 `cluster1` 中: |
| 140 | + |
| 141 | +{{< text plain >}} |
| 142 | +Hello version: v1, instance: helloworld-v1-954745fd-z6qcn |
| 143 | +Hello version: v1, instance: helloworld-v1-954745fd-z6qcn |
| 144 | +... |
| 145 | +{{< /text >}} |
| 146 | + |
| 147 | +同样地,从 `cluster2` 上的 `curl` Pod 多次发送请求: |
| 148 | + |
| 149 | +{{< text bash >}} |
| 150 | +$ kubectl exec --context "${CTX_CLUSTER2}" -n sample -c curl \ |
| 151 | + "$(kubectl get pod --context "${CTX_CLUSTER2}" -n sample -l \ |
| 152 | + app=curl -o jsonpath='{.items[0].metadata.name}')" \ |
| 153 | + -- curl -sS helloworld.sample:5000/hello |
| 154 | +{{< /text >}} |
| 155 | + |
| 156 | +通过查看响应中的版本信息,您应该可以看到所有请求都在 `cluster2` 中处理: |
| 157 | + |
| 158 | +{{< text plain >}} |
| 159 | +Hello version: v2, instance: helloworld-v2-7b768b9bbd-7zftm |
| 160 | +Hello version: v2, instance: helloworld-v2-7b768b9bbd-7zftm |
| 161 | +... |
| 162 | +{{< /text >}} |
| 163 | + |
| 164 | +## 验证故障转移到另一个集群 {#verify-failover-to-another-cluster} |
| 165 | + |
| 166 | +为了验证故障转移到从集群是否有效,通过缩减部署规模来模拟 |
| 167 | +`cluster1` 中的 `HelloWorld` 服务中断: |
| 168 | + |
| 169 | +{{< text bash >}} |
| 170 | +$ kubectl --context "${CTX_CLUSTER1}" scale --replicas=0 deployment/helloworld-v1 -n sample |
| 171 | +{{< /text >}} |
| 172 | + |
| 173 | +再次从 `cluster1` 上的 `curl` Pod 向 `HelloWorld` 服务发送请求: |
| 174 | + |
| 175 | +{{< text bash >}} |
| 176 | +$ kubectl exec --context "${CTX_CLUSTER1}" -n sample -c curl \ |
| 177 | + "$(kubectl get pod --context "${CTX_CLUSTER1}" -n sample -l \ |
| 178 | + app=curl -o jsonpath='{.items[0].metadata.name}')" \ |
| 179 | + -- curl -sS helloworld.sample:5000/hello |
| 180 | +{{< /text >}} |
| 181 | + |
| 182 | +这次您应该会看到请求是由 `cluster2` 中的 `HelloWorld` 服务处理的, |
| 183 | +因为 `cluster1` 中没有可用的端点: |
| 184 | + |
| 185 | +{{< text plain >}} |
| 186 | +Hello version: v2, instance: helloworld-v2-7b768b9bbd-7zftm |
| 187 | +Hello version: v2, instance: helloworld-v2-7b768b9bbd-7zftm |
| 188 | +... |
| 189 | +{{< /text >}} |
| 190 | + |
| 191 | +**恭喜!**您已成功的在 Istio Ambient 多集群部署中配置本地故障转移! |
0 commit comments