文章目录
- 应用的蓝绿、金丝雀发布笔记
- 应用升级策略
- 停机升级
- 滚动更新
- 蓝绿发布
- 金丝雀发布
应用的蓝绿、金丝雀发布笔记
应用升级策略
-
Deployment.spec.strategy
设置 -
Recreate:同时删除所有副本,停机升级策略
-
不存在新老版本共存
-
存在某个时间段服务不可用
-
副本集一起Terminating,一起Pending,一起ContainerCreating,一起Running
-
-
RollingUpdate:默认滚动升级,不停机升级策略
- 不存在某个时间段服务不可用
- 切换过程,新老版本会共存
- 滚动更新核心参数
- maxUnavailable:滚动升级时允许的最大unavailable的pod数量,可以是整数或者百分比(默认25%),这个值越小,越能保证服务稳定,更新越平滑;
- maxSurge:滚动升级时先启动的pod数量,可以是整数或者百分比(默认25%),这个值调的越大,副本更新速度越快;
停机升级
示例
---
apiVersion: v1
kind: Service
metadata:name: nginx-service
spec:selector:app: nginx-v1ports:- protocol: TCPport: 80targetPort: 80nodePort: 30179type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-v1namespace: defaultlabels:app: nginx-v1
spec:selector:matchLabels:app: nginx-v1replicas: 3strategy:type: Recreate ## 需要显式指明停机升级策略,因为默认是滚动升级template:metadata:labels:app: nginx-v1spec:containers:- name: nginx-v1image: docker.m.daocloud.io/nginx:1.23.1imagePullPolicy: IfNotPresent
接下来我们修改一下镜像版本,进行停机更新升级
image: docker.m.daocloud.io/nginx
滚动更新
默认就是滚动更新策略
---
apiVersion: v1
kind: Service
metadata:name: nginx-service
spec:selector:app: nginx-v1ports:- protocol: TCPport: 80targetPort: 80nodePort: 30179type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-v1namespace: defaultlabels:app: nginx-v1
spec:selector:matchLabels:app: nginx-v1replicas: 3
# strategy:
# type: Recreate ## 需要显式指明停机升级策略,因为默认是滚动升级template:metadata:labels:app: nginx-v1spec:containers:- name: nginx-v1image: docker.m.daocloud.io/nginx:1.23.1imagePullPolicy: IfNotPresent
- 可以通过更改以下字段,来变更每次被嘎的数量和启动新的版本的数量
strategy:type: RollingUpdaterollingUpdate:maxUnavailable: 25% ###最大被嘎的数量,越小,更新越平滑maxSurge: 25% ### 启动新的pod数量,越大,副本更新越快
- 滚动更新:会有新旧版本同时存在的问题
蓝绿发布
发布原理
- svc的selector选择器
- v1版本和v2版本同时启动,然后通过修改svc一键切换
- 无缝升级,无缝回退,不存在两个版本共存的现象
- 生产环境推荐,只不过会增加资源开销
v1版本
---
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-v1namespace: defaultlabels:app: nginx-v1
spec:selector:matchLabels:app: nginx-v1replicas: 3template:metadata:labels:app: nginx-v1spec:containers:- name: nginx-v1image: docker.m.daocloud.io/nginx:1.23.1imagePullPolicy: IfNotPresent
v2版本
---
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-v2namespace: defaultlabels:app: nginx-v2
spec:selector:matchLabels:app: nginx-v2replicas: 3template:metadata:labels:app: nginx-v2spec:containers:- name: nginx-v2image: docker.m.daocloud.io/nginximagePullPolicy: IfNotPresent
svc
---
apiVersion: v1
kind: Service
metadata:name: nginx-service
spec:selector:app: nginx-v1ports:- protocol: TCPport: 80targetPort: 80nodePort: 30179type: NodePort
当前访问情况
修改svc的selector信息为v2然后观察
spec:selector:app: nginx-v2
while true;do curl -sI 192.168.1.225:30179 | grep -E 'Server|HTTP';done
金丝雀发布
金丝雀发布就是先发一个新版应用,但是是一个小比例范围,也叫灰度测试,如结果灰度测试通过,就把其余的v1版本全部替换程v2版本。如果测试没通过,就直接回退
- 新功能验证,适当的引流,适合金丝雀发布
v1
---
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-v1namespace: defaultlabels:app: nginx-v1
spec:selector:matchLabels:app: nginxreplicas: 3template:metadata:labels:app: nginxspec:containers:- name: nginximage: docker.m.daocloud.io/nginx:1.23.1imagePullPolicy: IfNotPresent
v2
---
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-v2namespace: defaultlabels:app: nginx-v2
spec:selector:matchLabels:app: nginxreplicas: 1template:metadata:labels:app: nginxspec:containers:- name: nginximage: docker.m.daocloud.io/nginximagePullPolicy: IfNotPresent
svc
---
apiVersion: v1
kind: Service
metadata:name: nginx-service
spec:selector:app: nginxports:- protocol: TCPport: 80targetPort: 80nodePort: 30179type: NodePort
灰度测试成功,我们逐渐将流量切入过去即可
### 修改副本数即可