金丝雀发布

分享到:

金丝雀发布

金丝雀发布

  • 部署第一个版本
 1apiVersion: apps/v1
 2kind: Deployment
 3metadata:
 4  name: nginx-deployment
 5  labels:
 6    app: nginx
 7spec:
 8  replicas: 3
 9  selector:
10    matchLabels:
11      app: nginx
12  template:
13    metadata:
14      labels:
15        app: nginx
16    spec:
17      containers:
18      - name: nginx
19        image: nginx:1.7.9
20---
21apiVersion: v1
22kind: Service
23metadata:
24  name: nginx-service
25  labels:
26    app: nginx
27spec:
28  selector:
29    app: nginx
30  ports:
31  - name: nginx-port
32    protocol: TCP
33    port: 80
34    nodePort: 32600
35    targetPort: 80
36  type: NodePort
  • 假设此时想要发布新的版本 nginx 1.8.0,可以创建第二个 Deployment:
 1apiVersion: apps/v1
 2kind: Deployment
 3metadata:
 4  name: nginx-deployment-canary
 5  labels:
 6    app: nginx
 7    track: canary
 8spec:
 9  replicas: 1
10  selector:
11    matchLabels:
12      app: nginx
13      track: canary
14  template:
15    metadata:
16      labels:
17        app: nginx
18        track: canary
19    spec:
20      containers:
21      - name: nginx
22        image: nginx:1.8.0

因为 Service 的LabelSelector 是 app: nginx,由 nginx-deployment 和 nginx-deployment-canary 创建的 Pod 都带有标签 app: nginx,所以,Service 的流量将会在两个 release 之间分配 在新旧版本之间,流量分配的比例为两个版本副本数的比例,此处为 1:3

当您确定新的版本没有问题之后,可以将 nginx-deployment 的镜像标签修改为新版本的镜像标签,并在完成对 nginx-deployment 的滚动更新之后,删除 nginx-deployment-canary 这个 Deployment

局限性 按照 Kubernetes 默认支持的这种方式进行金丝雀发布,有一定的局限性:

  • 不能根据用户注册时间、地区等请求中的内容属性进行流量分配
  • 同一个用户如果多次调用该 Service,有可能第一次请求到了旧版本的 Pod,第二次请求到了新版本的 Pod

可以使用 Flagger