Istio: implementing AB testing and path cutting in VS

Time:2021-3-4

  • The content of this article

The main purpose is to summarize the relevant rules of match in VS and how to realize path cutting in istio (current version 1.8.2)

Experiment demo

main.go

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	//1. Create route
	r := gin.Default()
	//2. Binding routing rules and executing functions
	r.GET("/zisefeizhu", func(context *gin.Context) {
		context.String(http.StatusOK, "Hello Zisefeizhu V1!")
//V1 version is context.String ( http.StatusOK , "Hello Zisefeizhu V1!")
//V2 version is context.String ( http.StatusOK , "Hello Zisefeizhu V2!")
	})
	//3. Monitor port, 8080 by default
	r.Run(":8080")
}

Dockerfile

FROM registry.cn-shenzhen.aliyuncs.com/realibox-baseimage/golang:1.15.2-alpine as builder

WORKDIR /app

RUN go env -w GO111MODULE=on \
    && go env -w GOPROXY=https://goproxy.cn,direct

COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN CGO_ENABLED=0 go build -ldflags "-s -w"  -o server main.go


FROM alpine:latest

WORKDIR /app
COPY --from=builder /app/server /app
CMD ["./server"]

deployment.yaml

  • The difference between V1 and V2 is version
apiVersion: apps/v1
kind: Deployment
metadata:
  name: goproject
  namespace: zisefeizhu
  labels:
    app: goproject
    version: v1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: goproject
      version: v1
  template:
    metadata:
      labels:
        app: goproject
        version: v1
    spec:
      containers:
      - image: registry.cn-shenzhen.aliyuncs.com/zisefeizhu/goproject:goproject-zisefeizhu-5425
        imagePullPolicy: IfNotPresent
        livenessProbe:
          failureThreshold: 3
          initialDelaySeconds: 5
          periodSeconds: 30
          successThreshold: 1
          tcpSocket:
            port: 8080
          timeoutSeconds: 2
        name: goproject
        ports:
        - containerPort: 8080
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          initialDelaySeconds: 10
          periodSeconds: 30
          successThreshold: 1
          tcpSocket:
            port: 8080
      imagePullSecrets:
      - name: business-secret

svc.yaml

  • There is no version tag in SVC
apiVersion: v1
kind: Service
metadata:
  labels:
    app: goproject
  name: goproject
  namespace: zisefeizhu
spec:
  ports:
  - name: http
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: goproject
  type: ClusterIP

Istio configuration list

gateway.yaml

  • Under istio system name space
  • Using cert manager to automatically generate and renew certificates
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: www-zisefeizhu-gateway
  namespace: istio-system
spec:
  selector:
    istio: ingressgateway
  servers:
  - hosts:
    - www.zisefeizhu.com
    port:
      name: http
      number: 80
      protocol: HTTP
    tls:
      Httpsredirect: true # 301 jump to HTTPS
  - hosts:
    - www.zisefeizhu.com
    port:
      name: https
      number: 443
      protocol: HTTPS
    tls:
      Credentialname: www-zisefeizhu-com # certificate name of certificate generated by cert Manager
      mode: SIMPLE

dr.yaml

  • Defining subsets
kind: DestinationRule
apiVersion: networking.istio.io/v1alpha3
metadata:
  name: goproject
  namespace: zisefeizhu
spec:
  host: goproject
  subsets:
    - labels:
        version: v12.3
      name: v1
    - labels:
        version: v12.4
      name: v2

vr.yaml

  • Here’s the point
kind: VirtualService
apiVersion: networking.istio.io/v1alpha3
metadata:
  name: goproject
  namespace: zisefeizhu
spec:
  hosts:
    - www.zisefeizhu.com
  gateways:
    -Istio system / www-zisefeizhu-gateway # cross namespace 
  http:
    -Match: # AB test condition matching block the test condition set here is the user agent field, and two customer addresses are set
        -Headers: # currently, istio needs to write multiple headers for multi condition matching
            user-agent:
              exact: Mozilla/5.0   
          uri:
            Prefix: / API / # access www.zisefeizhu.com/api/zisefeizhu  Jump to www.zisefeizhu.com/zisefeizhu 
        -Heads: there's nothing like Kong`` konghq.com/strip -Comment on "path '"
            user-agent:
              exact: Mozilla/6.0
          uri:
            prefix: /api/
      Rewrite: # note here: https://github.com/istio/istio/issues/8076
        uri: /
      route:
        - destination:
            host: goproject
            Subset: V2 # matching to enter V2 version
    -Match: this matching block is the 'cut' routing path matching block
        - uri:
            prefix: /api/
      rewrite:
        uri: /
      route:
        - destination:
            host: goproject
            subset: v1
    -Route: # default route
        - destination:
            host: goproject
            subset: v1

verification

  • The tool is: postman
  • Visit address http:www.zisefeizhu.com/api/zisefeizhu
    user-agent: Mozilla/5.0

user-agent: Mozilla/6.0

user-agent: Mozilla/7.0

👌!

summary

The following points should be noted in this article:
1. Match block

Match the conditions to be met by the rule to be activated. All conditions in a single matching block have and semantics, while the matching block list has or semantics. If any of the matching blocks succeed, the rule is matched.

2. Header operations headers

At present, it seems that the matching of multiple conditions can not be written in a header, so it needs to be written separately

3. Route cutting

Istio doesn’t seem to have a method similar to Kong and nginxde for route cutting

PS: Kong nginx: the bottom layer is the same

Rewriting can be done with rewrite

Recommended Today

Deeply analyze the principle and practice of RSA key

1、 Preface After experiencing many dark moments in life, when you read this article, you will regret and even be angry: why didn’t you write this article earlier?! Your darkest moments include: 1. Your project needs to be connected with the bank, and the other party needs you to provide an encryption certificate. You have […]