這篇文章給大家介紹Kubernetes中怎么實(shí)現(xiàn)滾動(dòng)更新,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
站在用戶的角度思考問題,與客戶深入溝通,找到黃平網(wǎng)站設(shè)計(jì)與黃平網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋黃平地區(qū)。
為了應(yīng)用升級(jí)部署時(shí)候k8s不停服達(dá)到用戶無感知,Kubernetes支持稱為滾動(dòng)更新的功能。此功能允許您按順序更新pod,一次更新一個(gè)(按照配置比例),而不是一次停止/更新整個(gè)pod。使發(fā)布版本更新和回滾而不會(huì)中斷服務(wù)
kubectl rolling-update僅在使用Replication Controllers 部署應(yīng)用程序時(shí)才使用該命令。最新版本的Kubernetes 建議使用Deployment部署應(yīng)用程序。
創(chuàng)建deployment部署nginx:v1
[root@k8s-master ~]# cat deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-dm labels: app: nginx-dm spec: replicas: 3 minReadySeconds: 10 strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 0 selector: matchLabels: app: nginx-dm template: metadata: labels: app: nginx-dm spec: containers: - name: nginx-dm image: registry.cn-hangzhou.aliyuncs.com/k8simages_wt/nginx:v1 imagePullPolicy: Always ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: nginx-dm-service spec: ports: - name: http port: 80 targetPort: 80 selector: app: nginx-dm type: NodePort
kubectl apply -f deployment.yaml [root@k8s-master ~]# kubectl get pod -l app=nginx-dm NAME READY STATUS RESTARTS AGE nginx-deployment-7597c9f695-b8qvt 2/2 Running 0 19s nginx-deployment-7597c9f695-l4x6g 2/2 Running 0 19s nginx-deployment-7597c9f695-nr724 2/2 Running 0 19s
主要部分 replicas: 3 minReadySeconds: 10 strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 0
spec.replicas
表示Pod的副本數(shù)量。我已經(jīng)設(shè)置了初始配置來復(fù)制三個(gè)Pod以進(jìn)行滾動(dòng)更新測(cè)試
spec.minReadySeconds
這是從pod變?yōu)镽eady階段到變?yōu)锳vailable階段的時(shí)間。滾動(dòng)升級(jí)時(shí)10s后認(rèn)為該pod就緒可用
,建議設(shè)置適當(dāng)?shù)臅r(shí)間minReadySeconds以考慮pod容器初始化的時(shí)間
spec.strategy
為RollingUpdate進(jìn)行詳細(xì)設(shè)置 定義升級(jí)的策略
spec.strategy.type
可以是”Recreate”或者是 “RollingUpdate”?!盧ollingUpdate”是默認(rèn)值。Recreate時(shí),在創(chuàng)建出新的Pod之前會(huì)先殺掉所有已存在的Pod。RollingUpdate
時(shí),Deployment使用rolling update 的方式更新Pod 。你可以指定maxUnavailable
和maxSurge
來控制 rolling update 進(jìn)程。
spec.strategy.rollingUpdate
如果在spec.strategy.type中設(shè)置“RollingUpdate”,請(qǐng)對(duì)RollingUpdate進(jìn)行詳細(xì)設(shè)置。
spec.strategy.rollingUpdate.maxSurge
滾動(dòng)更新期間可以創(chuàng)建的pod的最大數(shù)量超過指定數(shù)量的pod。1表示當(dāng)一個(gè)新的pod被創(chuàng)建才會(huì)刪除一個(gè)pod,以此類推??梢允蔷唧w的整數(shù),也可以是百分百 默認(rèn)值為25%
更新過程中,最多有一個(gè) Pod 不可用。在rollgin更新期間無法使用的最大pod數(shù)。該值可以設(shè)置為具有大于0的整數(shù)的pod的絕對(duì)數(shù)量,并且百分比表示也是可能的,例如“25%”。 maxUnavailable中的百分比計(jì)算向下舍入,默認(rèn)值為25%。 maxSurge和maxUnavailable值不能同時(shí)為零。
在使用Deployment進(jìn)行應(yīng)用程序部署的情況下,kubectl set image
命令用于更新鏡像版本。
滾動(dòng)更新進(jìn)度監(jiān)控查看
kubectl get pod -w
^C[root@k8s-master ~]# kubectl get pod -w | grep nginx
nginx-deployment-7597c9f695-b8qvt 2/2 Running 0 11h
nginx-deployment-7597c9f695-l4x6g 2/2 Running 0 11h
nginx-deployment-7597c9f695-nr724 2/2 Running 0 11h
通過kubectl set image 命令更新nginx:v2版本
[root@k8s-master ~]# kubectl set image deployment/nginx-deployment nginx-dm=registry.cn-hangzhou.aliyuncs.com/k8simages_wt/nginx:v2 deployment.extensions/nginx-deployment image updated
查看滾動(dòng)更新
^C[root@k8s-master ~]# kubectl get pod -w | grep nginx nginx-deployment-7597c9f695-b8qvt 2/2 Running 0 11h nginx-deployment-7597c9f695-l4x6g 2/2 Running 0 11h nginx-deployment-7597c9f695-nr724 2/2 Running 0 11h nginx-deployment-5f948bdcb8-2s28z 0/2 Pending 0 0s nginx-deployment-5f948bdcb8-2s28z 0/2 Pending 0 0s nginx-deployment-5f948bdcb8-2s28z 0/2 Init:0/1 0 0s nginx-deployment-5f948bdcb8-2s28z 0/2 PodInitializing 0 1s nginx-deployment-5f948bdcb8-2s28z 2/2 Running 0 3s nginx-deployment-7597c9f695-l4x6g 2/2 Terminating 0 11h nginx-deployment-5f948bdcb8-ljdqz 0/2 Pending 0 0s nginx-deployment-5f948bdcb8-ljdqz 0/2 Pending 0 0s nginx-deployment-5f948bdcb8-ljdqz 0/2 Init:0/1 0 1s nginx-deployment-7597c9f695-l4x6g 0/2 Terminating 0 11h nginx-deployment-7597c9f695-l4x6g 0/2 Terminating 0 11h nginx-deployment-5f948bdcb8-ljdqz 0/2 PodInitializing 0 8s nginx-deployment-5f948bdcb8-ljdqz 2/2 Running 0 10s nginx-deployment-7597c9f695-l4x6g 0/2 Terminating 0 11h nginx-deployment-7597c9f695-l4x6g 0/2 Terminating 0 11h nginx-deployment-7597c9f695-b8qvt 2/2 Terminating 0 11h nginx-deployment-5f948bdcb8-ksk8w 0/2 Pending 0 0s nginx-deployment-5f948bdcb8-ksk8w 0/2 Pending 0 0s nginx-deployment-5f948bdcb8-ksk8w 0/2 Init:0/1 0 0s nginx-deployment-7597c9f695-b8qvt 0/2 Terminating 0 11h nginx-deployment-7597c9f695-b8qvt 0/2 Terminating 0 11h nginx-deployment-7597c9f695-b8qvt 0/2 Terminating 0 11h nginx-deployment-5f948bdcb8-ksk8w 0/2 PodInitializing 0 2s nginx-deployment-5f948bdcb8-ksk8w 2/2 Running 0 4s
關(guān)于Kubernetes中怎么實(shí)現(xiàn)滾動(dòng)更新就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。