前提條件
- 已開通云容器引擎,至少有一個云容器引擎實例。產品入口:云容器引擎。
- 開通天翼云應用服務網格實例。
- 開通天翼云微服務引擎,并在服務網格控制面集群同VPC內創建云原生網關實例。產品入口:。
操作步驟
部署多版本reviews服務
這里以bookinfo應用里面的reviews服務為例,使用istio資源實現對reviews服務的多版本路由,首先到云容器引擎控制臺部署reviews服務的v2和v3版本,yaml如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: reviews-v2
labels:
withServiceMesh: "true"
spec:
replicas: 1
selector:
matchLabels:
name: reviews-v2
template:
metadata:
labels:
app: reviews
version: v2
name: reviews-v2
source: CCSE
"sidecar.istio.io/inject": "true"
csmAutoEnable: "on"
annotations:
"sidecar.istio.io/inject": "true"
spec:
containers:
- name: reviews
image: 'registry-vpc-crs-huadong1.cnsp-internal.daliqc.cn/library/istio-examples-bookinfo-reviews-v2:1.16.2'
imagePullPolicy: IfNotPresent
env:
- name: LOG_DIR
value: "/tmp/logs"
volumeMounts:
- name: tmp
mountPath: /tmp
- name: wlp-output
mountPath: /opt/ibm/wlp/output
volumes:
- name: wlp-output
emptyDir: {}
- name: tmp
emptyDir: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: reviews-v3
labels:
withServiceMesh: "true"
spec:
replicas: 1
selector:
matchLabels:
name: reviews-v3
template:
metadata:
labels:
app: reviews
version: v3
name: reviews-v3
source: CCSE
"sidecar.istio.io/inject": "true"
csmAutoEnable: "on"
annotations:
"sidecar.istio.io/inject": "true"
spec:
containers:
- name: reviews
image: 'registry-vpc-crs-huadong1.cnsp-internal.daliqc.cn/library/istio-examples-bookinfo-reviews-v3:1.16.2'
imagePullPolicy: IfNotPresent
env:
- name: LOG_DIR
value: "/tmp/logs"
volumeMounts:
- name: tmp
mountPath: /tmp
- name: wlp-output
mountPath: /opt/ibm/wlp/output
volumes:
- name: wlp-output
emptyDir: {}
- name: tmp
emptyDir: {}
通過云原生網關持續訪問bookinfo應用可以看到前端頁面在三種效果內跳變,分別對應reviews服務的三個版本:
(1)Review內沒有評分。
(2)Review內有評分信息,評分的星星顏色是黑色。
(3)Review內有評分信息,評分的星星顏色是紅色。
定義目標規則
通過定義目標規則(DestinationRule)為reviews服務在服務網格內定義多個版本;在網格控制臺進入流量管理中心->目標規則,選擇sample命名空間,使用yaml創建,選擇版本負載均衡模板,基于reviews pod的version標簽為reviews服務定義三個版本,配置如下:
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
trafficPolicy:
loadBalancer:
simple: RANDOM
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v3
labels:
version: v3
部分字段說明如下:
| 字段 | 說明 |
|---|---|
| Metadata.name | 目標規則的名稱,namespace內唯一。 |
| Spec.host | 該目標規則匹配的服務名。 |
| Spec.subsets.name | 目標服務子集的名稱。 |
| Spec.subsets.labels | 目標服務子集匹配的pod標簽。 |
定義虛擬服務規則
通過虛擬服務(VirtualService)為reviews服務定義路由規則,比如定義只訪問v3版本的reviews服務,可以在流量管理中心->虛擬服務,選擇sample命名空間,使用yaml創建,基于DestinationRule定義的三個subset,分配100%流量到v3版本,具體定義如下:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
weight: 0
- destination:
host: reviews
subset: v3
weight: 100
多次訪問bookinfo應用可以看到前端樣式無變化,reviews部分是帶打分,且是紅色的。
除了按比例在多個版本隨機分配流量之外,還可以按照http匹配規則實現自定義的流量路由策略,比如匹配指定頭部的訪問路由到v2,其他請求路由到v3,可以按照如下方式修改虛擬服務定義:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- match:
- headers:
end-user:
exact: jason
route:
- destination:
host: reviews
subset: v2
- route:
- destination:
host: reviews
subset: v3
訪問bookinfo頁面,當前登錄用戶為jason時,看到的reviews是v2版本,切換到另外一個登錄用戶jack時,看到的reviews是v3版本。