hexiaodai

流量镜像

Istio 流量镜像将复制每个传入请求,将一个请求发送到主服务,并将一个请求发送到金丝雀服务。来自主节点的响应被发送回用户,来自金丝雀的响应被丢弃。收集两个请求的指标,以便仅当金丝雀指标在阈值范围内时才会继续部署。

请注意,镜像应用于幂等或能够处理两次(一次由主服务器处理,一次由金丝雀处理)的请求。

前置条件:部署 Bookinfo Application

❯ kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.18/samples/bookinfo/platform/kube/bookinfo.yaml
❯ kubectl get pod
NAME                             READY   STATUS    RESTARTS   AGE
details-v1-5ffd6b64f7-vzdhg      2/2     Running   0          6d19h
productpage-v1-8b588bf6d-2w86d   2/2     Running   0          6d19h
ratings-v1-5f9699cfdf-7dcxv      2/2     Running   0          6d19h
reviews-v1-569db879f5-7bszt      2/2     Running   0          6d19h
reviews-v2-65c4dc6fdc-6wn9q      2/2     Running   0          6d19h
reviews-v3-c9c4fb987-nd9rr       2/2     Running   0          6d19h

定义 reviews 服务的网关规则

apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: reviews-gateway
spec:
  selector:
    istio: ingressgateway
  servers:
  - hosts:
    - '*'
    port:
      name: http
      number: 8080
      protocol: HTTP

定义 reviews 服务的流量目标规则

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: reviews
spec:
  host: reviews
  subsets:
  - labels:
      version: v1
    name: v1
  - labels:
      version: v2
    name: v2
  - labels:
      version: v3
    name: v3

100% 的流量镜像到金丝雀服务,进行金丝雀验收测试

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: reviews
spec:
  gateways:
  - reviews-gateway.istio-system.svc.cluster.local
  hosts:
  - '*'
  http:
  - mirror:
      host: reviews
      subset: v2
    mirrorPercentage:
      value: 100
    route:
    - destination:
        host: reviews
        subset: v1
      weight: 100
    - destination:
        host: reviews
        subset: v2
      weight: 0

停止流量镜像,扩大金丝雀接收流量权重

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: reviews
spec:
  gateways:
  - reviews-gateway.istio-system.svc.cluster.local
  hosts:
  - '*'
  http:
  - mirror:
      host: reviews
      subset: v2
    mirrorPercentage:
      value: 0
    route:
    - destination:
        host: reviews
        subset: v1
      weight: 80 # [60, 40, 20, 0]
    - destination:
        host: reviews
        subset: v2
      weight: 20 # [40, 60, 80, 100]

升级主服务,并且等待主服务部署完成

kubectl set image deployment/reviews reviews=reviews:v2

将实时流量切换回主服务器

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: reviews
spec:
  gateways:
  - reviews-gateway.istio-system.svc.cluster.local
  hosts:
  - '*'
  http:
  - route:
    - destination:
        host: reviews
        subset: v2
      weight: 100
    - destination:
        host: reviews
        subset: v1
      weight: 0

流量镜像金丝雀部署最佳实践