实践
滚动更新是一次只更新一小部分副本,成功后,再更新更多的副本,最终完成所有副本的更新。滚动更新的最大的好处是零停机,整个更新过程始终有副本在运行,从而保证了业务的连续性。
下面我们部署三副本应用,初始镜像为 httpd:2.2.31,然后将其更新到 httpd:2.2.32。
第一步: httpd:2.2.31 的配置文件如下:
[root@ken ~]# cat httpd.yml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: httpd
spec:
replicas: 3
template:
metadata:
labels:
run: httpd
spec:
containers:
- name: httpd
image: httpd:2.2.31
ports:
- containerPort: 80
第二步:部署应用并查看
[root@ken ~]# kubectl apply -f httpd.yml
deployment.apps/httpd created
[root@ken ~]# kubectl get deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 0/3 3 0 10s httpd httpd:2.2.31 run=httpd
[root@ken ~]# kubectl get replicaset -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
httpd-76cfb94bf4 3 3 0 23s httpd httpd:2.2.31 pod-template-hash=76cfb94bf4,run=httpd
[root@ken ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
httpd-76cfb94bf4-629s2 1/1 Running 0 113s 10.244.1.34 host1
httpd-76cfb94bf4-lt9wb 1/1 Running 0 113s 10.244.1.33 host1
httpd-76cfb94bf4-n62nj 1/1 Running 0 113s 10.244.2.23 host2
部署过程如下:
创建 Deployment httpd
创建 ReplicaSet httpd-76cfb94bf4
创建三个 Pod
当前镜像为 httpd:2.2.31
第三步:将配置文件中 httpd:2.2.31 替换为 httpd:2.2.32,再次执行 kubectl apply。
[root@ken ~]# kubectl apply -f httpd.yml
deployment.apps/httpd configured
[root@ken ~]# kubectl get deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 3/3 1 3 4m2s httpd httpd:2.2.32 run=httpd
[root@ken ~]# kubectl get replicaset -o wide #这一步要稍等几分钟才会切换到32版本
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
httpd-6cf6bf9f57 3 3 3 2m47s httpd httpd:2.2.32 pod-template-hash=6cf6bf9f57,run=httpd
httpd-76cfb94bf4 0 0 0 6m30s httpd httpd:2.2.31 pod-template-hash=76cfb94bf4,run=httpd
[root@ken ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
httpd-6cf6bf9f57-5md94 1/1 Running 0 2m58s 10.244.2.24 host2
httpd-6cf6bf9f57-nvxnc 1/1 Running 0 75s 10.244.1.35 host1
httpd-6cf6bf9f57-v7lpg 1/1 Running 0 22s 10.244.1.36 host1
我们发现了如下变化:
Deployment httpd 的镜像更新为 httpd:2.2.32
新创建了 ReplicaSethttpd-6cf6bf9f57,镜像为 httpd:2.2.32,并且管理了三个新的 Pod。
之前的 ReplicaSet httpd-76cfb94bf4里面已经没有任何 Pod。
结论是:ReplicaSethttpd-76cfb94bf4的三个 httpd:2.2.31 Pod 已经被 ReplicaSethttpd-6cf6bf9f57的三个 httpd:2.2.32 Pod 替换了。
第四步:具体过程可以通过 kubectl describe deployment httpd 查看。
[root@ken ~]# kubectl describe deployment httpd
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 9m11s deployment-controller Scaled up replica set httpd-76cfb94bf4 to 3
Normal ScalingReplicaSet 5m28s deployment-controller Scaled up replica set httpd-6cf6bf9f57 to 1
Normal ScalingReplicaSet 3m45s deployment-controller Scaled down replica set httpd-76cfb94bf4 to 2
Normal ScalingReplicaSet 3m45s deployment-controller Scaled up replica set httpd-6cf6bf9f57 to 2
Normal ScalingReplicaSet 2m52s deployment-controller Scaled down replica set httpd-76cfb94bf4 to 1
Normal ScalingReplicaSet 2m52s deployment-controller Scaled up replica set httpd-6cf6bf9f57 to 3
Normal ScalingReplicaSet 2m50s deployment-controller Scaled down replica set httpd-76cfb94bf4 to 0
每次只更新替换一个 Pod:
ReplicaSet httpd-6cf6bf9f57 增加一个 Pod,总数为 1。
ReplicaSet httpd-76cfb94bf4 减少一个 Pod,总数为 2。
ReplicaSet httpd-6cf6bf9f57 增加一个 Pod,总数为 2。
ReplicaSet httpd-76cfb94bf4 减少一个 Pod,总数为 1。
ReplicaSet httpd-6cf6bf9f57 增加一个 Pod,总数为 3。
ReplicaSet httpd-76cfb94bf4 减少一个 Pod,总数为 0。
每次替换的 Pod 数量是可以定制的。Kubernetes 提供了两个参数 maxSurge 和 maxUnavailable 来精细控制 Pod 的替换数量,我们将在后面结合 Health Check 特性一起讨论。
更新回滚
kubectl apply 每次更新应用时 Kubernetes 都会记录下当前的配置,保存为一个 revision(版次),这样就可以回滚到某个特定 revision。
默认配置下,Kubernetes 只会保留最近的几个 revision,可以在 Deployment 配置文件中通过 revisionHistoryLimit 属性增加 revision 数量。
第一步:下面实践回滚功能。
应用有如下三个配置文件 httpd.v1.yml,httpd.v2.yml 和 httpd.v3.yml,分别对应不同的 httpd 镜像 2.4.16,2.4.17 和 2.4.18:
第二步:部署应用并更新
后面一个部署的应用,会覆盖掉前面的(名称相同)
[root@ken ~]# kubectl apply -f httpd_v1.yml --record
deployment.apps/httpd created
[root@ken ~]# kubectl get deployment httpd -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 3/3 3 3 18s httpd httpd:2.4.16 run=httpd
[root@ken ~]# kubectl apply -f httpd_v2.yml --record
deployment.apps/httpd configured
[root@ken ~]# kubectl get deployment httpd -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 3/3 2 3 75s httpd httpd:2.4.17 run=httpd
[root@ken ~]# kubectl apply -f httpd_v3.yml --record
deployment.apps/httpd configured
[root@ken ~]# kubectl get deployment httpd -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 3/3 1 3 94s httpd httpd:2.4.18 run=httpd
–record 的作用是将当前命令记录到 revision 记录中,这样我们就可以知道每个 revison 对应的是哪个配置文件。
第三步:通过 kubectl rollout history deployment httpd 查看 revison 历史记录。
[root@ken ~]# kubectl rollout history deployment httpd
deployment.extensions/httpd
REVISION CHANGE-CAUSE
1 kubectl apply --filename=httpd_v1.yml --record=true
2 kubectl apply --filename=httpd_v2.yml --record=true
3 kubectl apply --filename=httpd_v3.yml --record=true
CHANGE-CAUSE 就是 –record 的结果。
第四步:如果要回滚到某个版本,比如 revision 1,可以执行命令 kubectl rollout undo deployment httpd –to-revision=1:
[root@ken ~]# kubectl rollout undo deployment httpd --to-revision=1
deployment.extensions/httpd rolled back
[root@ken ~]# kubectl get deployment httpd -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 3/3 3 3 5m26s httpd httpd:2.4.16 run=httpd
第五步:此时,revison 历史记录也会发生相应变化。
[root@ken ~]# kubectl rollout history deployment httpd
deployment.extensions/httpd
REVISION CHANGE-CAUSE
2 kubectl apply --filename=httpd_v2.yml --record=true
3 kubectl apply --filename=httpd_v3.yml --record=true
4 kubectl apply --filename=httpd_v1.yml --record=true
revison 1 变成了 revison 4。不过我们可以通过 CHANGE-CAUSE 知道每个 revison 的具体含义。所以一定要在执行 kubectl apply 时加上 –record参数。
文章转自:http://www.kendd.cn/?p=703