k8s之Deployment滚动更新

实验环境

k8s环境参考:二进制部署指导文档

k8s之Deployment滚动更新

deployment是一个三级结构,deployment管理replicaset,replicaset管理pod

busybox-red/blue

[root@k8s-master-1 autoupdate]# cat busybox-red-blue.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox-red-blue
spec:
  minReadySeconds: 20
  paused: false
  progressDeadlineSeconds: 600
  replicas: 5
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      autoupdate: busybox-red-blue
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      name: busybox-red-blue
      namespace: default
      labels:
        autoupdate: busybox-red-blue
    spec:
      restartPolicy: Always
      tolerations:
      - key: node-role.kubernetes.io/master
        operator: Exists
      containers:
      - name: busybox
        image: busybox:red
        imagePullPolicy: IfNotPresent
        command:
        - "sleep"
        - "86400"


# kubectl apply:表示声明式的定义,既可以创建资源,也可以动态更新资源
[root@k8s-master-1 autoupdate]# kubectl apply -f busybox-red-blue.yaml

# 查看deployment信息
[root@k8s-master-1 autoupdate]# kubectl get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
busybox-red-blue   5/5     5            0           23s

# 查看replicaset,replicaset = deployment + 随机串
[root@k8s-master-1 autoupdate]# kubectl get rs
NAME                          DESIRED   CURRENT   READY   AGE
busybox-red-blue-5b595cf6cd   5         5         5       73s

# 查看pod pod name = replicaset + 随机串
[root@k8s-master-1 autoupdate]# kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
busybox-red-blue-5b595cf6cd-2rzrb   1/1     Running   0          2m1s
busybox-red-blue-5b595cf6cd-cbtk2   1/1     Running   0          2m1s
busybox-red-blue-5b595cf6cd-d2jf7   1/1     Running   0          2m1s
busybox-red-blue-5b595cf6cd-p67pc   1/1     Running   0          2m1s
busybox-red-blue-5b595cf6cd-tk4zn   1/1     Running   0          2m1s

参数介绍

# 参数
	不应直接通过创建另一个Deployment或创建另一个控制器(例如ReplicaSet或ReplicationController)来创建其标签与该选择器匹配的其他Pod。如果这样做,则第一个Deployment会认为它创建了其他Pod。如果你有多个具有重叠选择器的控制器,则这些控制器将相互竞争,并且无法正常运行
	
#strategy
	.spec.strategy策略指定用新Pods替换旧Pods的策略。.spec.strategy.type可以是“Recreate”或“RollingUpdate”。“RollingUpdate”是默认值。

Recreate Deployment
	当.spec.strategy.type=Recreate  #删除所有的pod,然后更新新的pod

#滚动更新 Deployment
	Deployment 会在 .spec.strategy.type==RollingUpdate时,采取 滚动更新的方式更新Pods。可以指定maxUnavailable和maxSurge来控制滚动更新操作。


# maxSurge和maxUnavailable用来控制滚动更新的更新策略
	数值
		1. maxUnavailable: [0, 副本数]
		2. maxSurge: [0, 副本数]
	注意:两者不能同时为0。

	1. maxUnavailable: [0%, 100%] 向下取整,比如10个副本,5%的话==0.5个,但计算按照0个;
	2. maxSurge: [0%, 100%] 向上取整,比如10个副本,5%的话==0.5个,但计算按照1个;
	注意:两者不能同时为0。
	
# 建议配置
	1. maxUnavailable == 0
	2. maxSurge == 1
	这是我们生产环境提供给用户的默认配置。即“一上一下,先上后下”最平滑原则:
1个新版本pod ready(结合readiness)后,才销毁旧版本pod。此配置适用场景是平滑更新、保证服务平稳,但也有缺点,就是“太慢”了
	例如:replicas=5,maxSurge=1,maxUnavailable=0,更新时最大的pod数是replicas+ maxSurge = 5+1 =6,最大的个数是6,最小pod数是 replicas - maxUnavailable = 5-0 = 5,最小pod数是5,所以只能先启动一个pod,再删除一个pod。---最开始需要满足6个可用,即会生成1个,然后再替换1个

# 总结:
	maxUnavailable:和期望的副本数比,不可用副本数最大比例(或最大值),这个值越小,越能保证服务稳定,更新越平滑;
	maxSurge:和期望的副本数比,超过期望副本数最大比例(或最大值),这个值调的越大,副本更新速度越快。

	
#Progress Deadline Seconds
	.spec.progressDeadlineSeconds是一个可选字段,它指定在系统报告部署失败之前你要等待部署进行的秒数,默认值为600。将来,一旦实现自动回滚,Deployment控制器将在观察到这种情况后立即回滚Deployment。如果指定,则此字段必须大于.spec.minReadySeconds。
	表示 Deployment controller 等待多少秒才能确定(通过 Deployment status)Deployment进程是卡住的,当超过截止时间后,Deployment controller 会在 Deployment 的 status.conditions中增加一条DeploymentCondition,它包括如下属性:
	Type=Progressing
	Status=False
	Reason=ProgressDeadlineExceeded
	
# Min Ready Seconds
	.spec.minReadySeconds是一个可选字段,用于指定新创建的Pod在不使其任何容器崩溃的情况下应准备就绪的最小秒数,以便将其视为可用。默认值为0(准备就绪后,Pod将被视为可用)。要了解有关何时将Pod准备就绪的更多信息,可参考https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#container-probes


# Revision History Limit
	deployment的修订历史记录存储在它控制的ReplicaSets中。
	.spec.revisionHistoryLimit是一个可选字段,用于指定要保留的可用于回滚的旧的replicaset的数量。这些旧的ReplicaSets在etcd中消耗资源,将kubectl get rs的输出结果聚集起来。每个Deployment版本的配置都存储在ReplicaSets中。因此,一旦删除了旧的ReplicaSet,你将无法回滚到该版本的Deployment。默认情况下,将保留10个旧的ReplicaSet,但是其理想值取决于新部署的频率和稳定性。更具体地说,将此字段设置为零意味着将清除所有具有0个副本的旧ReplicaSet。在这种情况下,由于已清除其修订历史记录,新的部署将不能被撤销。

# Paused
	.spec.paused是一个可选的布尔字段,用于暂停和恢复部署。暂停的Deployment和未暂停的Deployment之间的唯一区别是,只要暂停,对暂停的Deployment的PodTemplateSpec所做的任何更改都不会触发新的rollouts。当创建时,默认情况下不会暂停deployment。

查看滚动升级流程

# 通过deployment管理应用,实现扩容,把副本数变成10,apply不同于create,apply可以执行多次;create执行一次,再执行就会报错复
[root@k8s-master-1 autoupdate]# kubectl apply -f busybox-red-blue.yaml --record

# 查看pod
[root@k8s-master-1 autoupdate]# kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
busybox-red-blue-5b595cf6cd-266mj   1/1     Running   0          9s
busybox-red-blue-5b595cf6cd-2rzrb   1/1     Running   0          5m8s
busybox-red-blue-5b595cf6cd-524jh   1/1     Running   0          9s
busybox-red-blue-5b595cf6cd-6khd9   1/1     Running   0          9s
busybox-red-blue-5b595cf6cd-cbtk2   1/1     Running   0          5m8s
busybox-red-blue-5b595cf6cd-d2jf7   1/1     Running   0          5m8s
busybox-red-blue-5b595cf6cd-lng4p   1/1     Running   0          9s
busybox-red-blue-5b595cf6cd-p67pc   1/1     Running   0          5m8s
busybox-red-blue-5b595cf6cd-tk4zn   1/1     Running   0          5m8s
busybox-red-blue-5b595cf6cd-tsx4q   1/1     Running   0          9s

# 将image改为: busybox:blue 默认是滚动升级,kubectl describe pods xx可以查看
[root@k8s-master-1 autoupdate]# kubectl set image deployment/busybox-red-blue busybox=busybox:blue --record

# 查看滚动更新记录,可以发现总体更新遵循先增加一个满足最多11个可用,然后减少一个满足最少可用
[root@k8s-master-1 autoupdate]# kubectl get pods -w
NAME                                READY   STATUS    RESTARTS   AGE
busybox-red-blue-5b595cf6cd-47kdc   1/1     Running   0          24s
busybox-red-blue-5b595cf6cd-8cbwm   1/1     Running   0          24s
busybox-red-blue-5b595cf6cd-8snd7   1/1     Running   0          45s
busybox-red-blue-5b595cf6cd-c7slz   1/1     Running   0          45s
busybox-red-blue-5b595cf6cd-hz6f8   1/1     Running   0          45s
busybox-red-blue-5b595cf6cd-jtvfk   1/1     Running   0          24s
busybox-red-blue-5b595cf6cd-ndm55   1/1     Running   0          45s
busybox-red-blue-5b595cf6cd-trpls   1/1     Running   0          24s
busybox-red-blue-5b595cf6cd-x46ts   1/1     Running   0          24s
busybox-red-blue-5b595cf6cd-xrrk9   1/1     Running   0          45s
busybox-red-blue-66cbffb449-fhxd9   0/1     Pending   0          0s
busybox-red-blue-66cbffb449-fhxd9   0/1     Pending   0          0s
busybox-red-blue-66cbffb449-fhxd9   0/1     ContainerCreating   0          0s
busybox-red-blue-66cbffb449-fhxd9   0/1     ContainerCreating   0          1s
busybox-red-blue-66cbffb449-fhxd9   1/1     Running             0          2s
busybox-red-blue-5b595cf6cd-47kdc   1/1     Terminating         0          70s
busybox-red-blue-66cbffb449-d88h7   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-d88h7   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-d88h7   0/1     ContainerCreating   0          0s
busybox-red-blue-66cbffb449-d88h7   0/1     ContainerCreating   0          1s
busybox-red-blue-66cbffb449-d88h7   1/1     Running             0          1s
busybox-red-blue-5b595cf6cd-jtvfk   1/1     Terminating         0          91s
busybox-red-blue-66cbffb449-dchcw   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-dchcw   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-dchcw   0/1     ContainerCreating   0          0s
busybox-red-blue-66cbffb449-dchcw   0/1     ContainerCreating   0          1s
busybox-red-blue-66cbffb449-dchcw   1/1     Running             0          3s
busybox-red-blue-5b595cf6cd-47kdc   1/1     Terminating         0          101s
busybox-red-blue-5b595cf6cd-47kdc   0/1     Terminating         0          102s
busybox-red-blue-5b595cf6cd-47kdc   0/1     Terminating         0          103s
busybox-red-blue-5b595cf6cd-47kdc   0/1     Terminating         0          103s
busybox-red-blue-5b595cf6cd-8cbwm   1/1     Terminating         0          114s
busybox-red-blue-66cbffb449-qrdwp   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-qrdwp   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-qrdwp   0/1     ContainerCreating   0          0s
busybox-red-blue-66cbffb449-qrdwp   0/1     ContainerCreating   0          1s
busybox-red-blue-66cbffb449-qrdwp   1/1     Running             0          1s
busybox-red-blue-5b595cf6cd-jtvfk   1/1     Terminating         0          2m2s
busybox-red-blue-5b595cf6cd-jtvfk   0/1     Terminating         0          2m3s
busybox-red-blue-5b595cf6cd-jtvfk   0/1     Terminating         0          2m4s
busybox-red-blue-5b595cf6cd-jtvfk   0/1     Terminating         0          2m4s
busybox-red-blue-5b595cf6cd-x46ts   1/1     Terminating         0          2m15s
busybox-red-blue-66cbffb449-gp8p9   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-gp8p9   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-gp8p9   0/1     ContainerCreating   0          0s
busybox-red-blue-66cbffb449-gp8p9   0/1     ContainerCreating   0          1s
busybox-red-blue-66cbffb449-gp8p9   1/1     Running             0          2s
busybox-red-blue-5b595cf6cd-8cbwm   1/1     Terminating         0          2m24s
busybox-red-blue-5b595cf6cd-8cbwm   0/1     Terminating         0          2m25s
busybox-red-blue-5b595cf6cd-8cbwm   0/1     Terminating         0          2m30s
busybox-red-blue-5b595cf6cd-8cbwm   0/1     Terminating         0          2m30s
busybox-red-blue-5b595cf6cd-trpls   1/1     Terminating         0          2m37s
busybox-red-blue-66cbffb449-f4rrk   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-f4rrk   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-f4rrk   0/1     ContainerCreating   0          0s
busybox-red-blue-66cbffb449-f4rrk   0/1     ContainerCreating   0          1s
busybox-red-blue-66cbffb449-f4rrk   1/1     Running             0          1s
busybox-red-blue-5b595cf6cd-x46ts   1/1     Terminating         0          2m45s
busybox-red-blue-5b595cf6cd-x46ts   0/1     Terminating         0          2m46s
busybox-red-blue-5b595cf6cd-x46ts   0/1     Terminating         0          2m50s
busybox-red-blue-5b595cf6cd-x46ts   0/1     Terminating         0          2m50s
busybox-red-blue-5b595cf6cd-hz6f8   1/1     Terminating         0          3m19s
busybox-red-blue-66cbffb449-mshx4   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-mshx4   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-mshx4   0/1     ContainerCreating   0          0s
busybox-red-blue-66cbffb449-mshx4   0/1     ContainerCreating   0          1s
busybox-red-blue-66cbffb449-mshx4   1/1     Running             0          1s
busybox-red-blue-5b595cf6cd-trpls   1/1     Terminating         0          3m7s
busybox-red-blue-5b595cf6cd-trpls   0/1     Terminating         0          3m8s
busybox-red-blue-5b595cf6cd-trpls   0/1     Terminating         0          3m12s
busybox-red-blue-5b595cf6cd-trpls   0/1     Terminating         0          3m12s
busybox-red-blue-5b595cf6cd-8snd7   1/1     Terminating         0          3m40s
busybox-red-blue-66cbffb449-vbm2r   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-vbm2r   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-vbm2r   0/1     ContainerCreating   0          0s
busybox-red-blue-66cbffb449-vbm2r   0/1     ContainerCreating   0          0s
busybox-red-blue-66cbffb449-vbm2r   1/1     Running             0          1s
busybox-red-blue-5b595cf6cd-hz6f8   1/1     Terminating         0          3m49s
busybox-red-blue-5b595cf6cd-hz6f8   0/1     Terminating         0          3m50s
busybox-red-blue-5b595cf6cd-hz6f8   0/1     Terminating         0          4m1s
busybox-red-blue-5b595cf6cd-hz6f8   0/1     Terminating         0          4m1s
busybox-red-blue-5b595cf6cd-c7slz   1/1     Terminating         0          4m2s
busybox-red-blue-66cbffb449-2cn4d   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-2cn4d   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-2cn4d   0/1     ContainerCreating   0          0s
busybox-red-blue-66cbffb449-2cn4d   0/1     ContainerCreating   0          1s
busybox-red-blue-66cbffb449-2cn4d   1/1     Running             0          2s
busybox-red-blue-5b595cf6cd-8snd7   1/1     Terminating         0          4m11s
busybox-red-blue-5b595cf6cd-8snd7   0/1     Terminating         0          4m11s
busybox-red-blue-5b595cf6cd-8snd7   0/1     Terminating         0          4m12s
busybox-red-blue-5b595cf6cd-8snd7   0/1     Terminating         0          4m12s
busybox-red-blue-5b595cf6cd-ndm55   1/1     Terminating         0          4m24s
busybox-red-blue-66cbffb449-nwwfb   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-nwwfb   0/1     Pending             0          0s
busybox-red-blue-66cbffb449-nwwfb   0/1     ContainerCreating   0          0s
busybox-red-blue-66cbffb449-nwwfb   0/1     ContainerCreating   0          1s
busybox-red-blue-66cbffb449-nwwfb   1/1     Running             0          1s
busybox-red-blue-5b595cf6cd-c7slz   1/1     Terminating         0          4m32s
busybox-red-blue-5b595cf6cd-c7slz   0/1     Terminating         0          4m34s
busybox-red-blue-5b595cf6cd-c7slz   0/1     Terminating         0          4m43s
busybox-red-blue-5b595cf6cd-c7slz   0/1     Terminating         0          4m43s
busybox-red-blue-5b595cf6cd-xrrk9   1/1     Terminating         0          4m45s
busybox-red-blue-5b595cf6cd-ndm55   1/1     Terminating         0          4m54s
busybox-red-blue-5b595cf6cd-ndm55   0/1     Terminating         0          4m55s
busybox-red-blue-5b595cf6cd-ndm55   0/1     Terminating         0          5m3s
busybox-red-blue-5b595cf6cd-ndm55   0/1     Terminating         0          5m3s
busybox-red-blue-5b595cf6cd-xrrk9   1/1     Terminating         0          5m15s
busybox-red-blue-5b595cf6cd-xrrk9   0/1     Terminating         0          5m16s
busybox-red-blue-5b595cf6cd-xrrk9   0/1     Terminating         0          5m17s
busybox-red-blue-5b595cf6cd-xrrk9   0/1     Terminating         0          5m17s

回滚

# 查看历史版本
[root@k8s-master-1 autoupdate]# kubectl rollout history deployment/busybox-red-blue
deployment.apps/busybox-red-blue 
REVISION  CHANGE-CAUSE
1         kubectl apply --filename=busybox-red-blue.yaml --record=true
2         kubectl apply --filename=busybox-red-blue.yaml --record=true

# 查看当前状态
[root@k8s-master-1 autoupdate]# kubectl rollout status deployment/my-nginx
# 回滚到指定版本
[root@k8s-master-1 autoupdate]# kubectl rollout undo deployment/busybox-red-blue --to-revision=1
deployment.apps/busybox-red-blue rolled back

升级/暂停

# 暂停,对暂停的Deployment的PodTemplateSpec所做的任何更改都不会触发新的rollouts。
[root@k8s-master-1 autoupdate]# kubectl rollout pause deployment/busybox-red-blue

# 恢复
[root@k8s-master-1 autoupdate]# kubectl rollout resume deployment/busybox-red-blue

# 创建或升级,如果该资源还不存在,将创建它
[root@k8s-master-1 autoupdate]# kubectl apply -f xxx.yaml --record

缩放

#弹性伸缩Pod数量
[root@k8s-master-1 autoupdate]# kubectl scale deployment/busybox-red-blue --replicas 1
[root@k8s-master-1 autoupdate]# kubectl autoscale deployment/busybox-red-blue --min=10 --max=15 --cpu-percent=80

你可能感兴趣的:(Kubernetes,kubernetes,docker,linux)