k8s之滚动更新(RollingUpdate)与回滚-创新互联
滚动更新
滚动更新是一次只更新一小部分副本,成功后,再更新更多的副本,最终完成所有副本的更新。滚动更新的大的好处是零停机,整个更新过程始终有副本在运行,从而保证了业务的连续性。
创新互联公司网站建设公司,提供网站建设、成都网站建设,网页设计,建网站,PHP网站建设等专业做网站服务;可快速的进行网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,是专业的做网站团队,希望更多企业前来合作!下面我们部署三副本应用,初始镜像为 httpd:2.2.31,然后将其更新到 httpd:2.2.32。
httpd:2.2.31 的配置文件如下:
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
通过 kubectl apply 部署。
[root@k8s-master ~]# kubectl apply -f httpd.yml
deployment.apps/httpd created
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get deployments httpd -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 0/3 3 0 14s httpd httpd:2.2.31 run=httpd
[root@k8s-master ~]# kubectl get replicasets -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
httpd-56df754d56 3 3 3 37s httpd httpd:2.2.31 pod-template-hash=56df754d56,run=httpd
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
httpd-56df754d56-5hjt6 1/1 Running 0 52s
httpd-56df754d56-ngw9l 1/1 Running 0 52s
httpd-56df754d56-qtgbt 1/1 Running 0 52s
部署过程如下:
- 创建 Deployment httpd
- 创建 ReplicaSet httpd-551879778
- 创建三个 Pod
- 当前镜像为 httpd:2.2.31
将配置文件中 httpd:2.2.31 替换为 httpd:2.2.32,再次执行 kubectl apply。
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: httpd
spec:
replicas: 3
template:
metadata:
labels:
run: httpd
spec:
containers:
- name: httpd
image: httpd:2.2.32
ports:
- containerPort: 80
[root@k8s-master ~]# kubectl apply -f httpd.yml
deployment.apps/httpd configured
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get deployments httpd -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 3/3 1 3 3m54s httpd httpd:2.2.32 run=httpd
[root@k8s-master ~]# kubectl get replicasets -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
httpd-56df754d56 2 2 2 4m8s httpd httpd:2.2.31 pod-template-hash=56df754d56,run=httpd
httpd-77896d99b5 2 2 1 28s httpd httpd:2.2.32 pod-template-hash=77896d99b5,run=httpd
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
httpd-77896d99b5-pk52m 1/1 Running 0 24s
httpd-77896d99b5-pmlr9 1/1 Running 0 40s
httpd-77896d99b5-spn96 1/1 Running 0 9s
我们发现了如下变化:
- Deployment httpd 的镜像更新为 httpd:2.2.32
- 新创建了 ReplicaSet httpd-1276601241,镜像为 httpd:2.2.32,并且管理了三个新的 Pod。
- 之前的 ReplicaSet httpd-551879778 里面已经没有任何 Pod。
结论是:ReplicaSet httpd-551879778 的三个 httpd:2.2.31 Pod 已经被 ReplicaSet httpd-1276601241 的三个 httpd:2.2.32 Pod 替换了。
具体过程可以通过 kubectl describe deployment httpd 查看。
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 6m4s deployment-controller Scaled up replica set httpd-56df754d56 to 3
Normal ScalingReplicaSet 2m23s deployment-controller Scaled up replica set httpd-77896d99b5 to 1
Normal ScalingReplicaSet 2m7s deployment-controller Scaled down replica set httpd-56df754d56 to 2
Normal ScalingReplicaSet 2m7s deployment-controller Scaled up replica set httpd-77896d99b5 to 2
Normal ScalingReplicaSet 112s deployment-controller Scaled down replica set httpd-56df754d56 to 1
Normal ScalingReplicaSet 112s deployment-controller Scaled up replica set httpd-77896d99b5 to 3
Normal ScalingReplicaSet 110s deployment-controller Scaled down replica set httpd-56df754d56 to 0
每次只更新替换一个 Pod:
- ReplicaSet httpd-1276601241 增加一个 Pod,总数为 1。
- ReplicaSet httpd-551879778 减少一个 Pod,总数为 2。
- ReplicaSet httpd-1276601241 增加一个 Pod,总数为 2。
- ReplicaSet httpd-551879778 减少一个 Pod,总数为 1。
- ReplicaSet httpd-1276601241 增加一个 Pod,总数为 3。
- ReplicaSet httpd-551879778 减少一个 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:
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: httpd
spec:
revisionHistoryLimit: 10
replicas: 3
template:
metadata:
labels:
run: httpd
spec:
containers:
- name: httpd
image: httpd:2.4.16
ports:
- containerPort: 80
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: httpd
spec:
revisionHistoryLimit: 10
replicas: 3
template:
metadata:
labels:
run: httpd
spec:
containers:
- name: httpd
image: httpd:2.4.17
ports:
- containerPort: 80
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: httpd
spec:
revisionHistoryLimit: 10
replicas: 3
template:
metadata:
labels:
run: httpd
spec:
containers:
- name: httpd
image: httpd:2.4.18
ports:
- containerPort: 80
通过 kubectl apply 部署并更新应用:
[root@k8s-master ~]# kubectl apply -f httpd.v1.yml --record
deployment.apps/httpd created
[root@k8s-master ~]# kubectl get deployments httpd -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 3/3 3 3 36s httpd httpd:2.4.16 run=httpd
[root@k8s-master ~]# kubectl apply -f httpd.v2.yml --record
deployment.apps/httpd configured
[root@k8s-master ~]# kubectl get deployments httpd -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 3/3 1 3 2m24s httpd httpd:2.4.17 run=httpd
[root@k8s-master ~]# kubectl apply -f httpd.v3.yml --record
deployment.apps/httpd configured
[root@k8s-master ~]# kubectl get deployments httpd -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 3/3 1 3 2m43s httpd httpd:2.4.18 run=httpd
--record 的作用是将当前命令记录到 revision 记录中,这样我们就可以知道每个 revison 对应的是哪个配置文件。通过 kubectl rollout history deployment httpd 查看 revison 历史记录。
[root@k8s-master ~]# 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@k8s-master ~]# kubectl rollout undo deployment httpd --to-revision=1
deployment.extensions/httpd rolled back
[root@k8s-master ~]# kubectl get deployments. httpd -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 3/3 3 3 6m50s httpd httpd:2.4.16 run=httpd
此时,revison 历史记录也会发生相应变化。
[root@k8s-master ~]# 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参数。
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网站标题:k8s之滚动更新(RollingUpdate)与回滚-创新互联
当前路径:http://myzitong.com/article/djoeps.html