Kubernetes滚动更新(无中断平滑发布)
Kubernetes支持名为Rolling Update的功能,允许您不间断地,
接近几乎无缝地平滑升级部署应用程序 ,即在不停止对外服务的前提下完成应用的更新。
什么是滚动更新?
为了应用升级部署时候k8s不停服达到用户无感知,Kubernetes支持称为滚动更新的功能。此功能允许您按顺序更新pod,一次更新一个(按照配置比例),而不是一次停止/更新整个pod。使发布版本更新和回滚而不会中断服务
kubectl rolling-update仅在使用Replication Controllers 部署应用程序时才使用该命令。最新版本的Kubernetes 建议使用Deployment部署应用程序。
滚动更新
创建deployment部署nginx:v1
[root@k8s-master ~]# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-dm
labels:
app: nginx-dm
spec:
replicas: 3
minReadySeconds: 10
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: nginx-dm
template:
metadata:
labels:
app: nginx-dm
spec:
containers:
- name: nginx-dm
image: registry.cn-hangzhou.aliyuncs.com/k8simages_wt/nginx:v1
imagePullPolicy: Always
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-dm-service
spec:
ports:
- name: http
port: 80
targetPort: 80
selector:
app: nginx-dm
type: NodePort
kubectl apply -f deployment.yaml
[root@k8s-master ~]# kubectl get pod -l app=nginx-dm
NAME READY STATUS RESTARTS AGE
nginx-deployment-7597c9f695-b8qvt 2/2 Running 0 19s
nginx-deployment-7597c9f695-l4x6g 2/2 Running 0 19s
nginx-deployment-7597c9f695-nr724 2/2 Running 0 19s
主要部分
replicas: 3
minReadySeconds: 10
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
spec.replicas
表示Pod的副本数量。我已经设置了初始配置来复制三个Pod以进行滚动更新测试
spec.minReadySeconds
这是从pod变为Ready阶段到变为Available阶段的时间。滚动升级时10s后认为该pod就绪可用
,建议设置适当的时间minReadySeconds以考虑pod容器初始化的时间
spec.strategy
为RollingUpdate进行详细设置 定义升级的策略
spec.strategy.type
可以是”Recreate”或者是 “RollingUpdate”。”RollingUpdate”是默认值。Recreate时,在创建出新的Pod之前会先杀掉所有已存在的Pod。 RollingUpdate
时,Deployment使用rolling update 的方式更新Pod 。你可以指定maxUnavailable
和maxSurge
来控制 rolling update 进程。
spec.strategy.rollingUpdate
如果在spec.strategy.type中设置“RollingUpdate”,请对RollingUpdate进行详细设置。
spec.strategy.rollingUpdate.maxSurge
滚动更新期间可以创建的pod的最大数量超过指定数量的pod。1表示当一个新的pod被创建才会删除一个pod,以此类推。可以是具体的整数,也可以是百分百 默认值为25%
更新过程中,最多有一个 Pod 不可用。在rollgin更新期间无法使用的最大pod数。该值可以设置为具有大于0的整数的pod的绝对数量,并且百分比表示也是可能的,例如“25%”。
maxUnavailable中的百分比计算向下舍入,默认值为25%。
maxSurge和maxUnavailable值不能同时为零。
滚动更新和验证
在使用Deployment进行应用程序部署的情况下,kubectl set image
命令用于更新镜像版本。
滚动更新进度监控查看
kubectl get pod -w
^C[root@k8s-master ~]# kubectl get pod -w | grep nginx
nginx-deployment-7597c9f695-b8qvt 2/2 Running 0 11h
nginx-deployment-7597c9f695-l4x6g 2/2 Running 0 11h
nginx-deployment-7597c9f695-nr724 2/2 Running 0 11h
通过kubectl set image 命令更新nginx:v2版本
[root@k8s-master ~]# kubectl set image deployment/nginx-deployment nginx-dm=registry.cn-hangzhou.aliyuncs.com/k8simages_wt/nginx:v2
deployment.extensions/nginx-deployment image updated
查看滚动更新
^C[root@k8s-master ~]# kubectl get pod -w | grep nginx
nginx-deployment-7597c9f695-b8qvt 2/2 Running 0 11h
nginx-deployment-7597c9f695-l4x6g 2/2 Running 0 11h
nginx-deployment-7597c9f695-nr724 2/2 Running 0 11h
nginx-deployment-5f948bdcb8-2s28z 0/2 Pending 0 0s
nginx-deployment-5f948bdcb8-2s28z 0/2 Pending 0 0s
nginx-deployment-5f948bdcb8-2s28z 0/2 Init:0/1 0 0s
nginx-deployment-5f948bdcb8-2s28z 0/2 PodInitializing 0 1s
nginx-deployment-5f948bdcb8-2s28z 2/2 Running 0 3s
nginx-deployment-7597c9f695-l4x6g 2/2 Terminating 0 11h
nginx-deployment-5f948bdcb8-ljdqz 0/2 Pending 0 0s
nginx-deployment-5f948bdcb8-ljdqz 0/2 Pending 0 0s
nginx-deployment-5f948bdcb8-ljdqz 0/2 Init:0/1 0 1s
nginx-deployment-7597c9f695-l4x6g 0/2 Terminating 0 11h
nginx-deployment-7597c9f695-l4x6g 0/2 Terminating 0 11h
nginx-deployment-5f948bdcb8-ljdqz 0/2 PodInitializing 0 8s
nginx-deployment-5f948bdcb8-ljdqz 2/2 Running 0 10s
nginx-deployment-7597c9f695-l4x6g 0/2 Terminating 0 11h
nginx-deployment-7597c9f695-l4x6g 0/2 Terminating 0 11h
nginx-deployment-7597c9f695-b8qvt 2/2 Terminating 0 11h
nginx-deployment-5f948bdcb8-ksk8w 0/2 Pending 0 0s
nginx-deployment-5f948bdcb8-ksk8w 0/2 Pending 0 0s
nginx-deployment-5f948bdcb8-ksk8w 0/2 Init:0/1 0 0s
nginx-deployment-7597c9f695-b8qvt 0/2 Terminating 0 11h
nginx-deployment-7597c9f695-b8qvt 0/2 Terminating 0 11h
nginx-deployment-7597c9f695-b8qvt 0/2 Terminating 0 11h
nginx-deployment-5f948bdcb8-ksk8w 0/2 PodInitializing 0 2s
nginx-deployment-5f948bdcb8-ksk8w 2/2 Running 0 4s