3.5.2 地域感知
![图3-5-2-1-地域感知](/meshdemotutorial.github.io/highAvailability/failover/serviceLocality/../../../images/netCommunication/3-5-8.svg)
背景:业务规模增长,为提高网站可用性,电商网站业务团队计划在另一可用区的集群也部署一套网站业务,两套相同的网站业务在两个不同集群同时为用户提供服务。
两套网站业务在正常运行的情况下,ingress gateway会优先将流量路由至本地域或可用区的frontend服务,即使另一集群中也有frontend服务;frontend服务会优先就近访问相同可用区user,product,order,cart服务;order和cart服务也会优先就近访问相同可用区的stock服务。
首先将网站全套服务也部署至另一可用区的集群(子集群):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
| kubectl apply -f - <<EOF apiVersion: v1 kind: Namespace metadata: name: base labels: istio.io/rev: 1-6-9 spec: finalizers: - kubernetes --- apiVersion: apps/v1 kind: Deployment metadata: name: frontend namespace: base labels: app: frontend spec: replicas: 1 selector: matchLabels: app: frontend template: metadata: labels: app: frontend spec: containers: - name: frontend image: ccr.ccs.tencentyun.com/chloeyhuang/demo:v202007101540 imagePullPolicy: Always env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: REGION value: "guangzhou-zoneB" ports: - containerPort: 80 ---
apiVersion: v1 kind: Service metadata: name: frontend namespace: base labels: app: frontend spec: ports: - port: 80 name: http selector: app: frontend ---
apiVersion: apps/v1 kind: Deployment metadata: name: product-v1 namespace: base labels: app: product version: v1 spec: replicas: 1 selector: matchLabels: app: product version: v1 template: metadata: labels: app: product version: v1 spec: containers: - name: product image: ccr.ccs.tencentyun.com/zhulei/testproduct1:v1 imagePullPolicy: Always env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: REGION value: "guangzhou-zoneB" ports: - containerPort: 7000 ---
apiVersion: apps/v1 kind: Deployment metadata: name: product-v2 namespace: base labels: app: product version: v2 spec: replicas: 1 selector: matchLabels: app: product version: v2 template: metadata: labels: app: product version: v2 spec: containers: - name: product image: ccr.ccs.tencentyun.com/zhulei/testproduct2:v1 imagePullPolicy: Always env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: REGION value: "guangzhou-zoneB" ports: - containerPort: 7000 ---
apiVersion: v1 kind: Service metadata: name: product namespace: base labels: app: product spec: ports: - port: 7000 name: http selector: app: product ---
apiVersion: apps/v1 kind: Deployment metadata: name: user namespace: base labels: app: user spec: replicas: 1 selector: matchLabels: app: user template: metadata: labels: app: user spec: containers: - name: user image: ccr.ccs.tencentyun.com/zhulei/testuser:v1 imagePullPolicy: Always env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: REGION value: "guangzhou-zoneB" ports: - containerPort: 7000 ---
apiVersion: v1 kind: Service metadata: name: user namespace: base labels: app: user spec: ports: - port: 7000 name: http selector: app: user ---
apiVersion: apps/v1 kind: Deployment metadata: name: stock namespace: base labels: app: stock spec: replicas: 1 selector: matchLabels: app: stock template: metadata: labels: app: stock spec: containers: - name: stock image: ccr.ccs.tencentyun.com/zhulei/teststock:v1 imagePullPolicy: Always env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: REGION value: "guangzhou-zoneB" ports: - containerPort: 7000 ---
apiVersion: v1 kind: Service metadata: name: stock namespace: base labels: app: stock spec: ports: - port: 7000 name: http selector: app: stock ---
apiVersion: apps/v1 kind: Deployment metadata: name: cart namespace: base labels: app: cart spec: replicas: 3 selector: matchLabels: app: cart template: metadata: labels: app: cart spec: containers: - name: cart image: ccr.ccs.tencentyun.com/zhulei/testcart:v1 imagePullPolicy: Always env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: REGION value: "guangzhou-zoneB" ports: - containerPort: 7000 protocol: TCP ---
apiVersion: v1 kind: Service metadata: name: cart namespace: base labels: app: cart spec: ports: - name: http port: 7000 protocol: TCP selector: app: cart type: ClusterIP ---
apiVersion: apps/v1 kind: Deployment metadata: name: order-v1 namespace: base labels: app: order version: v1 spec: replicas: 1 selector: matchLabels: app: order version: v1 template: metadata: labels: app: order version: v1 spec: containers: - name: order image: ccr.ccs.tencentyun.com/zhulei/testorder1:v1 imagePullPolicy: Always env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: REGION value: "guangzhou-zoneB" ports: - containerPort: 7000 protocol: TCP ---
apiVersion: apps/v1 kind: Deployment metadata: name: order-v2 namespace: base labels: app: order version: v2 spec: replicas: 1 selector: matchLabels: app: order version: v2 template: metadata: labels: app: order version: v2 spec: containers: - name: order image: ccr.ccs.tencentyun.com/zhulei/testorder2:v1 imagePullPolicy: Always env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: REGION value: "guangzhou-zoneB" ports: - containerPort: 7000 protocol: TCP ---
apiVersion: v1 kind: Service metadata: name: order namespace: base labels: app: order spec: ports: - name: http port: 7000 protocol: TCP selector: app: order type: ClusterIP EOF
|
部署配置完成后,未配置健康检查时,地域感知不生效,两个可用区的某一服务调用另外的服务是随机访问,不会按照就近访问的原则。
![图3-5-2-2-order服务调用不同可用区的stock服务](/meshdemotutorial.github.io/highAvailability/failover/serviceLocality/../../../images/netCommunication/3-5-7.png)
要开启服务访问的地域感知,需要配置所有服务的健康检查功能,通过将以下yaml文件提交至主集群实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| kubectl apply -f - <<EOF apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: cart namespace: base spec: host: cart trafficPolicy: loadBalancer: consistentHash: httpHeaderName: UserID outlierDetection: consecutiveErrors: 5 interval: 10000ms baseEjectionTime: 30000ms maxEjectionPercent: 10 minHealthPercent: 50 exportTo: - '*'
--- apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: frontend namespace: base spec: host: frontend trafficPolicy: outlierDetection: consecutiveErrors: 5 interval: 10000ms baseEjectionTime: 30000ms maxEjectionPercent: 10 minHealthPercent: 50 exportTo: - '*'
--- apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: order namespace: base spec: host: order trafficPolicy: outlierDetection: consecutiveErrors: 5 interval: 10000ms baseEjectionTime: 30000ms maxEjectionPercent: 10 minHealthPercent: 50 subsets: - name: v1 labels: version: v1 - name: v2 labels: version: v2 exportTo: - '*'
--- apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: product namespace: base spec: host: product trafficPolicy: outlierDetection: consecutiveErrors: 5 interval: 10000ms baseEjectionTime: 30000ms maxEjectionPercent: 10 minHealthPercent: 50 subsets: - name: v1 labels: version: v1 - name: v2 labels: version: v2
--- apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: stock namespace: base spec: host: stock trafficPolicy: outlierDetection: consecutiveErrors: 5 interval: 10000ms baseEjectionTime: 30000ms maxEjectionPercent: 10 minHealthPercent: 50 exportTo: - '*'
--- apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: user namespace: base spec: host: user trafficPolicy: outlierDetection: consecutiveErrors: 5 interval: 10000ms baseEjectionTime: 30000ms maxEjectionPercent: 10 minHealthPercent: 50 exportTo: - '*' EOF
|
健康检查配置完成后,由可用区A集群的边缘代理网关访问网站服务,浏览商品页面,添加购物车,下单等操作,可用区A的边缘代理网关会将流量路由至相同可用区的前端frontend服务,前端服务也会地域感知就近调用同一可用区的user、cart、order、stock服务;通过可用区B的边缘代理网关访问网站业务,请求路由至可用区B的前端服务,可用区B服务也会就近调用相同可用区的服务。通过Demo页面左下角悬浮窗可以查看当前调用服务的可用区信息。
![图3-5-2-3-地域感知](/meshdemotutorial.github.io/highAvailability/failover/serviceLocality/../../../images/netCommunication/3-5-3.png)