您好,登錄后才能下訂單哦!
滾動更新是一次只更新一小部分副本,成功后,再更新更多的副本,最終完成所有副本的更新。滾動更新的最大的好處是零停機,整個更新過程始終有副本在運行,從而保證了業務的連續性。
下面我們部署三副本應用,初始鏡像為 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
部署過程如下:
將配置文件中 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
我們發現了如下變化:
結論是: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:
每次替換的 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參數。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。