本章节主要介绍在服务网格,如何实现灰度发布能力
操作场景
随着网站流量的增加,网站开始有了广告投放的需求,广告投放需要在商品页面增加广告位。网站的开发人员新开发了 product 服务的 v2版本,以 product v2的 deployment 的形式提供,并希望对 product-v2版本做灰度发布
部署
部署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
部署服务描述
通过 DR 定义服务版本 + 通过 VS 定义权重路由来完成灰度发布的第一步,将部分流量(50%)路由至 product v2 subset 以验证新版本,剩余部分(50%)的流量仍然路由至 product v1版本。将以下 YAML 文件提交至主集群即可完成以上设定
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: product-vs
namespace: base
spec:
hosts:
- "product.base.svc.cluster.local"
http:
- match:
- uri:
exact: /
route:
- destination:
host: product.base.svc.cluster.local
subset: v1
port:
number: 80
weight: 50
- destination:
host: product.base.svc.cluster.local
subset: v2
port:
number: 80
weight: 50
---
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
配置完成后,访问 product 服务的流量将有 50%被路由至 v1 版本,50%被路由至 v2 版本,刷新网站商品页面即可验证。