本章介绍如何在服务网格中,实现流量镜像功能。
操作场景
在预发布过程中,为了尽可能使新版本服务的流量与线上保持一致,且不对线上环境产生影响,可以将线上流量镜像到预发布版本。同时通过流量镜像功能也可以对线上流量进行采集分析。
部署服务
部署 v1 和 v2 版本的 deployment以及对应的service 至集群
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: nginx:1.25-alpine
ports:
- containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: product-v1
namespace: base
spec:
replicas: 1
selector:
matchLabels:
app: product
version: v1
template:
metadata:
labels:
app: product
version: v1
spec:
containers:
- name: product
image: nginx:1.25-alpine
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
labels:
app: product
name: product
namespace: base
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
selector:
app: product
type: ClusterIP
应用服务网格配置
假设v1为线上版本,v2为预发布版本,通过DestinationRule定义subset v1和v2,同时在VirtualService中对流量镜像的策略进行定义,将线上流量复制到预发布版本。将以下yaml配置部署至集群:
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: product-vs
namespace: base
spec:
gateways:
- mesh
hosts:
- product
http:
- route:
- destination:
host: product
port:
number: 80
subset: v1
mirror:
host: product
subset: v2
port:
number: 80
mirrorPercentage: # 可选,默认100.0
value: 50.0
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: product
namespace: base
spec:
host: product
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
上述配置实现了将v1流量的50%镜像到v2。