kubernetes: pod升級與回滾擴容與縮容暫停恢復
阿新 • • 發佈:2021-07-06
執行一個容器:
apiVersion: apps/v1 kind: Deployment metadata: annotations: deployment.kubernetes.io/revision: "1" generation: 1 labels: app: nginx name: nginx namespace: default spec: progressDeadlineSeconds: 600 replicas: 1 # 副本數 revisionHistoryLimit: 10 # 歷史記錄保留的個數 selector: matchLabels: app: nginx strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - image: nginx:1.15.4 imagePullPolicy: IfNotPresent name: nginx resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File dnsPolicy: ClusterFirst restartPolicy: Always schedulerName: default-scheduler securityContext: {} terminationGracePeriodSeconds: 30
# 儲存為deployment.yaml 並建立nginx pod: [root@k8s-master01 yaml]# kubectl create -f deployment-nginx.yaml deployment.apps/nginx created #檢查: [root@k8s-master01 yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-6cdd5dd489-d7kll 1/1 Running 0 17s nginx-6cdd5dd489-dblw2 1/1 Running 0 17s nginx-6cdd5dd489-zc8fl 1/1 Running 0 17s #檢查nginx版本: [root@k8s-master01 yaml]# kubectl exec -it nginx-6cdd5dd489-d7kll -- sh # nginx -v nginx version: nginx/1.15.4 #刪除pod kubectl delete -n default deployment nginx #檢查 [root@k8s-master01 pod]# kubectl get pod No resources found in default namespace.
映象升級與回滾
#升級: root@k8s-master01[11:10:24]:~/yaml$ kubectl set image deploy nginx nginx=nginx:1.15.5 --record=true deployment.apps/nginx image updated #檢查升級過程: kubectl describe pod nginx-7c4b6d99fd-csh5s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 76s default-scheduler Successfully assigned default/nginx-df9789dc8-6h7p7 to k8s-node02 Normal Pulling 76s kubelet Pulling image "nginx:1.15.5" Normal Pulled 51s kubelet Successfully pulled image "nginx:1.15.5" in 24.828362289s Normal Created 51s kubelet Created container nginx Normal Started 51s kubelet Started container nginx #檢查現在的版本: [root@k8s-master01 yaml]# kubectl exec -it nginx-df9789dc8-trtwq -- sh # nginx -v nginx version: nginx/1.15.5 #檢查更新過程 kubectl rollout status deploy nginx [root@k8s-master01 yaml]# kubectl rollout status deploy nginx Waiting for deployment "nginx" rollout to finish: 1 out of 2 new replicas have been updated... Waiting for deployment "nginx" rollout to finish: 1 out of 2 new replicas have been updated... Waiting for deployment "nginx" rollout to finish: 1 out of 2 new replicas have been updated... Waiting for deployment "nginx" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "nginx" rollout to finish: 1 old replicas are pending termination... deployment "nginx" successfully rolled out [root@k8s-master01 yaml]# kubectl exec -it nginx-6cdd5dd489-bsb7f -- sh # nginx -v nginx version: nginx/1.15.5 #為了實驗效果 在升級一次 [root@k8s-master01 yaml]# kubectl set image deploy nginx nginx=nginx:1.15.6 --record=true [root@k8s-master01 yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-5455fbc855-64brv 1/1 Running 0 2m48s nginx-5455fbc855-nblks 1/1 Running 0 3m16s nginx-5455fbc855-ns4kz 1/1 Running 0 2m47s [root@k8s-master01 yaml]# kubectl exec -it nginx-5455fbc855-64brv -- sh # nginx -v nginx version: nginx/1.15.6 #回滾: 1. 檢視歷史更新 kubectl rollout history deploy nginx [root@k8s-master01 yaml]# kubectl rollout history deploy nginx deployment.apps/nginx REVISION CHANGE-CAUSE 1 <none> 2 kubectl set image deploy nginx nginx=nginx:1.15.5 --record=true 3 kubectl set image deploy nginx nginx=nginx:1.15.6 --record=true 2. 回滾到上一個版本[kubectl rollout undo deploy {name} ] [root@k8s-master01 yaml]# kubectl rollout undo deploy nginx deployment.apps/nginx rolled back 3. 檢查回滾狀態: [root@k8s-master01 yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-6cdd5dd489-bzdgd 1/1 Running 0 21s nginx-6cdd5dd489-cwxv5 1/1 Running 0 22s nginx-6cdd5dd489-xb6xj 1/1 Running 0 20s 3.1 進入容器檢查: [root@k8s-master01 yaml]# kubectl exec -it nginx-6cdd5dd489-bzdgd -- sh # nginx -v nginx version: nginx/1.15.4 4. 回滾到指定版本 [root@k8s-master01 yaml]# kubectl rollout history deploy nginx deployment.apps/nginx REVISION CHANGE-CAUSE 1 kubectl set image deploy nginx nginx=nginx:1.15.5 --record=true 2 kubectl set image deploy nginx nginx=nginx:1.15.6 --record=true # 檢視指定版本資訊 kubectl rollout history deploy nginx --revision=1 #回滾到指定版本 kubectl rollout undo deploy nginx --to-revision=1 [root@k8s-master01 yaml]# kubectl exec -it nginx-6cdd5dd489-q44cr -- sh # nginx -v nginx version: nginx/1.15.5
使用pod擴容,縮容
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-df9789dc8-gqk5j 1/1 Running 0 11m
nginx-df9789dc8-vhqkd 1/1 Running 0 11m
#修改資源:
[root@k8s-master01 yaml]# kubectl edit deploy nginx
deployment.apps/nginx edited
找到: replicas: 2
改為: replicas: 3
#檢查狀態:
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-df9789dc8-c47mc 1/1 Running 0 3s
nginx-df9789dc8-gqk5j 1/1 Running 0 11m
nginx-df9789dc8-vhqkd 1/1 Running 0 11m
#縮容就是繼續把 replicas: 3 改為: replicas: 2即可:
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-df9789dc8-gqk5j 1/1 Running 0 13m
nginx-df9789dc8-vhqkd 1/1 Running 0 13m
#通過命令列進行擴容縮容
#縮容:
[root@k8s-master01 yaml]# kubectl scale --replicas=2 deploy nginx
deployment.apps/nginx scaled
#擴容:
[root@k8s-master01 yaml]# kubectl scale --replicas=10 deploy nginx
deployment.apps/nginx scaled
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-565979ccb4-5j896 1/1 Running 0 2s
nginx-565979ccb4-6hkjw 1/1 Running 0 2s
nginx-565979ccb4-6wkkx 1/1 Running 0 2s
nginx-565979ccb4-7nnlh 1/1 Running 0 8m54s
nginx-565979ccb4-cs2km 1/1 Running 0 2s
nginx-565979ccb4-dztkx 1/1 Running 0 2s
nginx-565979ccb4-jbmx7 1/1 Running 0 2s
nginx-565979ccb4-jqpzq 1/1 Running 0 2s
nginx-565979ccb4-vfnrd 1/1 Running 0 9m22s
nginx-565979ccb4-zsgwv 1/1 Running 0 2s
#注意,每個節點預設最大擴容數量為110個
# 如果需要修改則需要配置
使用yaml檔案進行升級
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2020-09-19T02:41:11Z"
generation: 1
labels:
app: nginx
name: nginx
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 3 # 副本數
revisionHistoryLimit: 10 # 歷史記錄保留的個數
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.4
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
#修改:
spec:
containers:
- image: nginx:1.15.5 #改為想要的映象版本
#改為 1.15.5
[root@k8s-master01 yaml]# kubectl apply -f nginx-1.yaml
deployment.apps/nginx configured
[root@k8s-master01 yaml]# kubectl get pod
#注意升級的時候 他會起新的rs(replicas),通過命令可以看到,他會新起來一個pod 並把老副本數設定為2,新副本起來2個的時候
#老副本數會設定為1,直到變成0 最後才會真正的完成滾動更新。
[root@k8s-master01 yaml]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-5455fbc855 3 3 3 4m46s
nginx-565979ccb4 1 1 0 5s
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-5455fbc855-j552c 1/1 Running 0 17s
nginx-5455fbc855-sl8m8 1/1 Running 0 15s
nginx-5455fbc855-sq4mr 1/1 Running 0 14s
[root@k8s-master01 yaml]# kubectl exec -it nginx-5455fbc855-j552c -- sh
# nginx -v
nginx version: nginx/1.15.5
#檢視滾動更新過程:
NewReplicaSet: nginx-565979ccb4 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 10m deployment-controller Scaled up replica set nginx-6cdd5dd489 to 3
Normal ScalingReplicaSet 9m53s deployment-controller Scaled up replica set nginx-df9789dc8 to 1
Normal ScalingReplicaSet 9m51s deployment-controller Scaled down replica set nginx-6cdd5dd489 to 2
Normal ScalingReplicaSet 9m51s deployment-controller Scaled up replica set nginx-df9789dc8 to 2
Normal ScalingReplicaSet 9m50s deployment-controller Scaled down replica set nginx-6cdd5dd489 to 1
Normal ScalingReplicaSet 9m50s deployment-controller Scaled up replica set nginx-df9789dc8 to 3
Normal ScalingReplicaSet 9m48s deployment-controller Scaled down replica set nginx-6cdd5dd489 to 0
Normal ScalingReplicaSet 7m47s deployment-controller Scaled up replica set nginx-5455fbc855 to 1
Normal ScalingReplicaSet 7m45s deployment-controller Scaled down replica set nginx-df9789dc8 to 2
Normal ScalingReplicaSet 2m18s (x10 over 7m45s) deployment-controller (combined from similar events): Scaled down replica set nginx-5455fbc855 to 0
Deployment 暫停[優化觸發更新]
暫停功能使用在多次更新場景下的多次更新有多個記錄的問題,如果每條更新都被記錄,在查詢有用更新的時候會比較麻煩,通過暫停更新功能,可以退回到指定版本,並且rs回滾只會產生一條記錄。
[root@k8s-master01 ~]# # 開啟 Deployment 暫停功能
[root@k8s-master01 ~]# kubectl rollout pause deployment nginx
deployment.apps/nginx paused
# 執行一次更新
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:1.15.3 --record
deployment.apps/nginx image updated
#此時按照常理來說 執行完了 kubectl set image deploy nginx nginx=nginx:1.15.3 --record 命令後應該會出現多次滾動更新來更新應用,但因為使用了 Deployment 暫停功能,在沒有確認變更指令下達之前這些變更都不會生效
[root@k8s-master01 ~]# # 進行第二次配置變更
[root@k8s-master01 ~]# # 新增記憶體CPU配置
[root@k8s-master01 ~]# kubectl set resources deploy nginx -c nginx --limits=cpu=200m,memory=128Mi --requests=cpu=10m,memory=16Mi
deployment.apps/nginx resource requirements updated
#檢查配置
[root@k8s-master01 ~]# kubectl get deploy nginx -oyaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "11"
kubernetes.io/change-cause: kubectl set image deploy nginx nginx=nginx:1.15.3
--record=true
creationTimestamp: "2020-09-19T02:41:11Z"
generation: 18
labels:
app: nginx
name: nginx
namespace: default
resourceVersion: "2660534"
selfLink: /apis/apps/v1/namespaces/default/deployments/nginx
uid: 1d9253a5-a36c-48cc-aefe-56f95967db66
spec:
paused: true
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.3
imagePullPolicy: IfNotPresent
name: nginx
resources: #-------------新增但未生效的配置
limits:
cpu: 200m
memory: 128Mi
requests:
cpu: 10m
memory: 16Mi #-------------新增但未生效的配置
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
availableReplicas: 2
conditions:
- lastTransitionTime: "2020-09-19T03:26:50Z"
lastUpdateTime: "2020-09-19T03:26:50Z"
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available
- lastTransitionTime: "2020-09-19T03:30:15Z"
lastUpdateTime: "2020-09-19T03:30:15Z"
message: Deployment is paused
reason: DeploymentPaused
status: Unknown
type: Progressing
observedGeneration: 18
readyReplicas: 2
replicas: 2
[root@k8s-master01 ~]# # 檢視pod是否被更新 [未更新]
[root@k8s-master01 ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
nginx-6cdd5dd489-lv28z 1/1 Running 0 30m
nginx-6cdd5dd489-nqqz7 1/1 Running 0 30m
#檢查rs更新狀態: [未更新]
[root@k8s-master01 pod]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-5dfc8689c6 0 0 0 29m
nginx-64f878dfbf 0 0 0 32m
nginx-66bbc9fdc5 0 0 0 24m
nginx-7d5b7bcf78 2 2 2 30m
#關閉暫停更新功能
[root@k8s-master01 ~]# kubectl rollout resume deploy nginx
deployment.apps/nginx resumed
#檢查狀態
[root@k8s-master01 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-5475c49ffb 0 0 0 21m
nginx-5dfc8689c6 0 0 0 33m
nginx-66bbc9fdc5 0 0 0 52m
nginx-68db656dd8 1 1 0 15s [狀態更新]
nginx-6cdd5dd489 2 2 2 31m
nginx-799b8478d4 0 0 0 21m
nginx-7d79b96f68 0 0 0 24m
nginx-f974656f7 0 0 0 21m
#pod狀態
[root@k8s-master01 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-68db656dd8-cc4jv 1/1 Running 0 22s [狀態更新]
nginx-68db656dd8-hvj8x 1/1 Running 0 20s [狀態更新]
支付寶讚賞