目录
使用service实现
金丝雀发布的核心是让 Service 同时指向 v1 和 v2 版本的 Deployment Pod,通过调整两个 Deployment 的副本数来逐步迁移流量。
1、创建当前版本的Deployment v1 和 Service
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-v1
spec:
replicas: 9
selector:
matchLabels:
app: myapp
version: v1
template:
metadata:
labels:
app: myapp
version: v1
spec:
containers:
- name: myapp-container
image: myapp:v1
---
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp
version: v1
ports:
- protocol: TCP
port: 80
targetPort: 8080
2、创建新版本的 Deployment v2
创建一个新的 Deployment 用于金丝雀发布,一开始只部署少量的副本
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-v2
spec:
replicas: 1 # 金丝雀的副本数量较少
selector:
matchLabels:
app: myapp
version: v2
template:
metadata:
labels:
app: myapp
version: v2
spec:
containers:
- name: myapp-container
image: myapp:v2
3、更新 Service 选择器
更新现有的 Service 选择器,让它同时选中两个版本的 Pod,这将导致 Service 将流量路由到两个版本的 Pod。因为 v2 的 Pod 数量较少,所以接收到的流量也会相对较少,这实现了金丝雀发布(v2所分流量接近于10%,v1所分流量接近于90%)。
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp
# 注意,这里不再指定 version 标签
ports:
- protocol: TCP
port: 80
targetPort: 8080
使用ingress特性实现
0.21.0版本引入canary功能,才能通过ingress实现金丝雀发布
1、创建两个 Deployment
首先,创建两个 Deployment,一个用于稳定版本(v1),另一个用于新版本(v2)。
# Deployment for v1
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-v1
spec:
replicas: 5
selector:
matchLabels:
app: myapp
version: v1
template:
metadata:
labels:
app: myapp
version: v1
spec:
containers:
- name: myapp-container
image: myapp:v1
---
# Deployment for v2 (Canary)
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-v2
spec:
replicas: 1
selector:
matchLabels:
app: myapp
version: v2
template:
metadata:
labels:
app: myapp
version: v2
spec:
containers:
- name: myapp-container
image: myapp:v2
2、为 v1 和 v2 版本的 Deployment 分别创建 Service
#v1 版本的 Service
apiVersion: v1
kind: Service
metadata:
name: myapp-v1
spec:
selector:
app: myapp
version: v1
ports:
- protocol: TCP
port: 80
targetPort: 8080
---
#v2 版本的 Service
apiVersion: v1
kind: Service
metadata:
name: myapp-v2
spec:
selector:
app: myapp
version: v2
ports:
- protocol: TCP
port: 80
targetPort: 8080
3、创建2个ingress实现金丝雀发布
在这个配置中,我们使用了 nginx.ingress.kubernetes.io/canary-weight
注解,其值设置为 10
。这意味着约有 10% 的流量会被路由到 myapp-v2
服务,而其他的 90% 仍然会被路由到 myapp-v1
服务。这个权重可以根据需要调整
# 主 Ingress 对象为 v1 版本
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp-ingress
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: myapp-v1
port:
number: 80
---
# 金丝雀 Ingress 对象为 v2 版本
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp-ingress-canary
annotations:
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-weight: "10"
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: myapp-v2
port:
number: 80
使用istio实现
1、过VirtualService配置金丝雀发布
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: app-virtualservice
spec:
hosts:
- "www.app-service.com" #指域名
http:
- route:
- destination:
host: app-service #指对应的service名称
subset: v1 #对应destinationRule中的v1
weight: 90 #90%流量分发到destinationRule中v1对应的pod
- destination:
host: app-service #指对应的service名称
subset: v2 ##对应destinationRule中的v2
weight: 10 #10%流量分发到destinationRule中v2对应的pod
DestinationRule Yaml
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: app-service-destination
spec:
host: app-service #对应service名称
subsets:
- name: v1 #对应上面VirtualService中的v1
labels:
version: v1 #对应标签version=v1的pod
- name: v2 #对应上面VirtualService中的v2
labels:
version: v2 #对应标签version=v2的pod
标签:name,kubernetes,金丝雀,app,v1,v2,version,三种,myapp
From: https://blog.csdn.net/qq_38483331/article/details/136978946