This page looks best with JavaScript enabled

采用istio实现灰度发布 金丝雀发布

使用istio在kubernetes下平滑升级服务

 ·  ☕ 2 min read

This is a test post.

灰度发布的介绍

istio实现灰度发布的原理

具体实现步骤

发布第一版程序

第一版程序正常发布,注意deployment 的label包含app: demoapp和version: 1.0.0,,svc只包含app: demoapp即可。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: demoapp-1.0.0
  namespace: deply-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demoapp
  template:
    metadata:
      labels:
        app: demoapp
        version: 1.0.0
    spec:
      containers:
        - image: zouhl/demoapp:1.0.0
          imagePullPolicy: Always
          name: demoweb
          ports:
            - containerPort: 80
              protocol: TCP

---
apiVersion: v1
kind: Service
metadata:
  name: demoapp
  namespace: deply-test
  labels:
    app: demoapp
spec:
  ports:
    - port: 80
      protocol: TCP
      targetPort: 80
  selector:
    app: demoapp

然后使用kuberctl检查部署结果,再部署istio相关的yaml

部署istio相关yaml

1.部署DestinationRule

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: demoapp
  namespace: deply-test
spec:
  host: demoapp
  trafficPolicy:
    loadBalancer:
      simple: RANDOM
  subsets:
  - name: v1
    labels:
      version: 1.0.0

2.部署gateway + virtualservice

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: demoapp-gateway
  namespace: deply-test
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: demoapp
  namespace: deply-test
spec:
  hosts:
  - "*"
  gateways:
  - demoapp-gateway
  http:
  - match:
    - uri:
        prefix: /demoapp/
    route:
    - destination:
        host: demoapp
        port:
          number: 80
        subset: v1

到目前为止,所有的请求都会路由到demeapp的v1版本。

灰度发布demoapp

现在开始升级demoapp,实现灰度发布。

1.部署第二版demoapp的deployment

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: demoapp-2.0.0
 namespace: deply-test
spec:
 replicas: 1
 selector:
   matchLabels:
     app: demoapp
 template:
   metadata:
     labels:
       app: demoapp
       version: 2.0.0
   spec:
     containers:
       - image: zouhl/demoapp:2.0.0
         imagePullPolicy: IfNotPresent
         name: demoweb
         ports:
           - containerPort: 80
             protocol: TCP
     

部署好过后,istio的流量不会路由到2.0.0的版本。

2.更新或部署istio的destination,添加v2版本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: demoapp
  namespace: deply-test
spec:
  host: demoapp
  trafficPolicy:
    loadBalancer:
      simple: RANDOM
  subsets:
  - name: v1
    labels:
      version: 1.0.0
  - name: v2
    labels:
      version: 2.0.0

3.更新或部署istio的VirtualService,将20%的流量转发到v2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: demoapp
  namespace: deply-test
spec:
  hosts:
  - "*"
  gateways:
  - demoapp-gateway
  http:
  - match:
    - uri:
        prefix: /demoapp/
    route:
    - destination:
        host: demoapp
        port:
          number: 80
        subset: v1
      weight: 80
    - destination:
        host: demoapp
        port:
          number: 80
        subset: v2
      weight: 20
         

4.第三步也可以使用下面的yaml,通过对headers的匹配进行分流,cookie中有user=jason的用户,90%会转发的v2版的程序,其余的用户只会转发到v1版本。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: demoapp
  namespace: deply-test
spec:
  hosts:
  - "*"
  gateways:
  - demoapp-gateway
  http:
  - match:
    - uri:
        prefix: /demoapp/
      headers:
        cookie:
          regex: "^(.*?)?(user=jason)(;.*)?$"
    route:
    - destination:
        host: demoapp
        port:
          number: 80
        subset: v1
      weight: 10
    - destination:
        host: demoapp
        port:
          number: 80
        subset: v2
      weight: 90
  - match:
    - uri:
        prefix: /demoapp/      
    route:
    - destination:
        host: demoapp
        subset: v1
Share on

tux
WRITTEN BY
tux
devops

What's on this Page