一、环境准备

我们紧接上一节的环境,进行下面的操作,如果不清楚的,可以先查看上一篇博文。

滚动更新是一次只更新一小部分副本,成功后,再更新更多的副本,最终完成所有副本的更新。滚动更新的最大的好处是零停机,整个更新过程始终有副本在运行,从而保证了业务的连续性。

二、更新

我们查看一下上一节的配置文件mytest-deploy.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mytest
spec:
  replicas: 3
  template:
    metadata:
      labels:
        run: mytest
    spec:
      containers:
      - name: mytest
        image: wangzan18/mytest:v1
        ports:
        - containerPort: 80

我们看到设定的镜像版本为v1,我们查看一下创建的ReplicaSet

[root@master ~]# kubectl get rs -o wide
NAME               DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                SELECTOR
mytest-88d46bf99   3         3         3       68m   mytest       wangzan18/mytest:v1   pod-template-hash=88d46bf99,run=mytest

目前运行的 Pod 由 ReplicaSet mytest-88d46bf99进行控制,我们将配置文件中的 v1 替换为 v2,再次应用。

[root@master ~]# kubectl apply -f mytest-deploy.yaml 
deployment.extensions/mytest configured
[root@master ~]# kubectl get pods
NAME                     READY   STATUS        RESTARTS   AGE
mytest-56c55b4c6-6gjxc   1/1     Running       0          24s
mytest-56c55b4c6-f5trx   1/1     Running       0          18s
mytest-56c55b4c6-sh5wd   1/1     Running       0          24s
mytest-88d46bf99-48f6n   1/1     Terminating   0          70m
mytest-88d46bf99-mv6cf   1/1     Terminating   0          70m
mytest-88d46bf99-p9w79   1/1     Terminating   0          70m
[root@master ~]# kubectl get deploy -o wide
NAME     READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                SELECTOR
mytest   3/3     3            3           72m   mytest       wangzan18/mytest:v2   run=mytest
[root@master ~]# kubectl get rs -o wide
NAME               DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                SELECTOR
mytest-56c55b4c6   3         3         3       59s   mytest       wangzan18/mytest:v2   pod-template-hash=56c55b4c6,run=mytest
mytest-88d46bf99   0         0         0       71m   mytest       wangzan18/mytest:v1   pod-template-hash=88d46bf99,run=mytest

我们可以看到 Deployment 的镜像更新为wangzan18/mytest:v2,创建了新的 ReplicaSet mytest-56c55b4c6,并管理了三个新的 Pod。

[root@master ~]# kubectl describe deployment mytest
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  4m9s  deployment-controller  Scaled up replica set mytest-56c55b4c6 to 1
  Normal  ScalingReplicaSet  4m9s  deployment-controller  Scaled down replica set mytest-88d46bf99 to 2
  Normal  ScalingReplicaSet  4m9s  deployment-controller  Scaled up replica set mytest-56c55b4c6 to 2
  Normal  ScalingReplicaSet  4m3s  deployment-controller  Scaled down replica set mytest-88d46bf99 to 1
  Normal  ScalingReplicaSet  4m3s  deployment-controller  Scaled up replica set mytest-56c55b4c6 to 3
  Normal  ScalingReplicaSet  4m3s  deployment-controller  Scaled down replica set mytest-88d46bf99 to 0

每次只更新替换一个 Pod。

三、回滚

kubectl apply 每次更新应用时 Kubernetes 都会记录下当前的配置,保存为一个 revision(版次),这样就可以回滚到某个特定 revision。

默认配置下,Kubernetes 只会保留最近的几个 revision,可以在 Deployment 配置文件中通过 revisionHistoryLimit 属性增加 revision 数量。

通过命令kubectl rollout undo我们可以回滚到上一个版本。

[root@master ~]# kubectl rollout undo deploy mytest
deployment.extensions/mytest rolled back
[root@master ~]# kubectl get rs -o wide
NAME               DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                SELECTOR
mytest-56c55b4c6   0         0         0       12m   mytest       wangzan18/mytest:v2   pod-template-hash=56c55b4c6,run=mytest
mytest-88d46bf99   3         3         3       82m   mytest       wangzan18/mytest:v1   pod-template-hash=88d46bf99,run=mytest

可以看到三个 Pod 重新由镜像为wangzan18/mytest:v1的ReplicaSet进行接管。