操作场景
业务规模增长,为提高网站可用性,电商网站业务团队计划在另一可用区的集群也部署一套网站业务,两套相同的网站业务在两个不同集群同时为用户提供服务。地域感知如下图所示:
操作步骤
两套网站业务在正常运行的情况下,ingress gateway 会优先将流量路由至本地域或可用区的 frontend 服务,即使另一集群中也有 frontend 服务;frontend 服务会优先就近访问相同可用区 user,product,order,cart 服务;order 和 cart 服务也会优先就近访问相同可用区的 stock 服务。
在 Kubernetes 中,Pod 的地域是通过在已部署的节点上的 Region 和 Zone 的标签决定的,Demo 应用 yaml 中已为工作负载设置了相应的 zone 标签,首先将网站全套服务也部署至另一可用区的集群(子集群):
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
部署配置完成后,未配置健康检查时,地域感知不生效,两个可用区的某一服务调用另外的服务是随机访问,不会按照就近访问的原则。
order 服务调用不同可用区的 stock 服务如下图所示:
要开启服务访问的地域感知,需要配置所有服务的健康检查功能,通过将以下 yaml 文件提交至主集群实现:
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:
- '*'
健康检查配置完成后,由可用区 A 集群的边缘代理网关访问网站服务,浏览商品页面,添加购物车,下单等操作,可用区 A 的边缘代理网关会将流量路由至相同可用区的前端 frontend 服务,前端服务也会地域感知就近调用同一可用区的 user、cart、order、stock 服务;通过可用区 B 的边缘代理网关访问网站业务,请求路由至可用区 B 的前端服务,可用区 B 服务也会就近调用相同可用区的服务。通过 Demo 页面左下角悬浮窗可以查看当前调用服务的可用区信息。地域感知如下图所示:
本页内容是否解决了您的问题?