istio实现多版本流量转移

istio实现多版本流量转移

多版本流量分发主要概念:同一个服务部署多个 deployment 共用一个 service 。通过在 deployment 设置 version 然后在 virtual service 以及 destination 配置流量转发。


deploy 配置示例


apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-client
  namespace: rakour-dev
  labels:
    app: hello-client
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-client
  template:
    metadata:
      labels:
        app: hello-client
        version: v1
    spec:
      imagePullSecrets:
        - name: registry-secret
      volumes:
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
      containers:
        - name: hello-client
          image: REGISTRY_URL/PROJECT_NAME/IMAGE_NAME:IMAGE_VERSION
          imagePullPolicy: Always
          ports:
            - containerPort: 9090
          volumeMounts:
            - name: timezone
              mountPath: /etc/localtime
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-client-v2
  namespace: rakour-dev
  labels:
    app: hello-client
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-client
  template:
    metadata:
      labels:
        app: hello-client
        version: v2
    spec:
      imagePullSecrets:
        - name: registry-secret
      volumes:
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
      containers:
        - name: hello-client
          image: REGISTRY_URL/PROJECT_NAME/IMAGE_NAME:IMAGE_VERSION
          imagePullPolicy: Always
          ports:
            - containerPort: 9090
          volumeMounts:
            - name: timezone
              mountPath: /etc/localtime
---
# service
apiVersion: v1
kind: Service
metadata:
  name: hello-client
  namespace: rakour-dev
spec:
  type: ClusterIP
  selector:
    app: hello-client
  ports:
    - port: 9090
      protocol: TCP
      targetPort: 9090

#yaml

gateway 配置示例


apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: rpc
  namespace: rakour-dev
spec:
  selector:
    istio: ingressgateway
  servers:
    - hosts:
        - rpc.api.rakour.com
      port:
        name: http
        number: 80
        protocol: HTTP

#bash

virtual service 配置示例


apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: rpc
  namespace: rakour-dev
spec:
  gateways:
    - rpc
  hosts:
    - rpc.api.rakour.com
  http:
    - route:
        - destination:
            host: hello-client
            subset: v1
          weight: 50
        - destination:
            host: hello-client
            subset: v2
          weight: 50

destination 配置示例


apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: hello-client
  namespace: rakour-dev
spec:
  host: hello-client
  subsets:
    - labels:
        version: v2
      name: v2
    - labels:
        version: v1
      name: v1
  trafficPolicy:
    loadBalancer:
      simple: ROUND_ROBIN
#bash