apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: myapp
release: dev
template:
metadata:
labels:
app: myapp
release: dev
spec:
containers:
- name: myapp-rongqi
image: ikubernetes/myapp:v1
vim de.yaml #修改yaml里的鏡像版本
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)站頁面設(shè)計(jì)、網(wǎng)站優(yōu)化SEO優(yōu)化為一體的專業(yè)網(wǎng)站制作公司,已為成都等多地近百家企業(yè)提供網(wǎng)站建設(shè)服務(wù)。追求良好的瀏覽體驗(yàn),以探求精品塑造與理念升華,設(shè)計(jì)最適合用戶的網(wǎng)站頁面。 合作只是第一步,服務(wù)才是根本,我們始終堅(jiān)持講誠信,負(fù)責(zé)任的原則,為您進(jìn)行細(xì)心、貼心、認(rèn)真的服務(wù),與眾多客戶在蓬勃發(fā)展的市場(chǎng)環(huán)境中,互促共生。
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 5
selector:
matchLabels:
app: myapp
release: dev
strategy:
rollingUpdate:
#設(shè)置策略為滾動(dòng)更新,還有一個(gè)重建更新,全部一起結(jié)束一起新建 不友好這里就不介紹了
maxSurge: 1
#最大可超出預(yù)設(shè)值1個(gè)容器
maxUnavailable: 0
#最大可有0個(gè)容器不可用 ,整體表示更新時(shí)可同時(shí)可有6個(gè)容器,必須保證有5個(gè)容器可用
template:
metadata:
labels:
app: myapp
release: dev
spec:
containers:
- name: myapp-rongqi
image: ikubernetes/myapp:v1
kubectl apply -f de.yaml #用apply更新配置
[root@cs25 ~]# kubectl get pod -l app=myapp -w
NAME READY STATUS RESTARTS AGE
myapp-deploy-576d479bcc-bt7hz 1/1 Running 0 47m
myapp-deploy-576d479bcc-bzqwv 1/1 Running 0 11m
myapp-deploy-576d479bcc-hf6kb 1/1 Running 0 47m
myapp-deploy-576d479bcc-hgb6w 1/1 Running 0 11m
myapp-deploy-576d479bcc-vztqp 1/1 Running 0 11m
myapp-deploy-7f969b64fb-2frdn 0/1 Pending 0 0s
myapp-deploy-576d479bcc-vztqp 1/1 Terminating 0 12m
myapp-deploy-7f969b64fb-6lhjw 0/1 Pending 0 0s
myapp-deploy-7f969b64fb-2frdn 0/1 Pending 0 0s
myapp-deploy-7f969b64fb-6lhjw 0/1 Pending 0 0s
myapp-deploy-7f969b64fb-5ms8f 0/1 Pending 0 0s
myapp-deploy-7f969b64fb-5ms8f 0/1 Pending 0 0s
myapp-deploy-7f969b64fb-6lhjw 0/1 ContainerCreating 0 1s
myapp-deploy-7f969b64fb-2frdn 0/1 ContainerCreating 0 1s
myapp-deploy-7f969b64fb-5ms8f 0/1 ContainerCreating 0 0s
myapp-deploy-7f969b64fb-2frdn 0/1 ContainerCreating 0 35s
myapp-deploy-7f969b64fb-5ms8f 0/1 ContainerCreating 0 35s
myapp-deploy-7f969b64fb-6lhjw 0/1 ContainerCreating 0 36s
myapp-deploy-576d479bcc-vztqp 0/1 Terminating 0 12m
myapp-deploy-576d479bcc-vztqp 0/1 Terminating 0 12m
myapp-deploy-576d479bcc-vztqp 0/1 Terminating 0 12m
myapp-deploy-7f969b64fb-5ms8f 1/1 Running 0 43s
myapp-deploy-7f969b64fb-2frdn 1/1 Running 0 47s
myapp-deploy-7f969b64fb-6lhjw 1/1 Running 0 51s
myapp-deploy-576d479bcc-hgb6w 1/1 Terminating 0 13m
myapp-deploy-576d479bcc-bzqwv 1/1 Terminating 0 13m
myapp-deploy-576d479bcc-bt7hz 1/1 Terminating 0 49m
myapp-deploy-7f969b64fb-gprvk 0/1 Pending 0 0s
myapp-deploy-7f969b64fb-fwdbr 0/1 Pending 0 0s
myapp-deploy-7f969b64fb-fwdbr 0/1 Pending 0 1s
myapp-deploy-7f969b64fb-gprvk 0/1 Pending 0 1s
myapp-deploy-7f969b64fb-fwdbr 0/1 ContainerCreating 0 1s
myapp-deploy-7f969b64fb-gprvk 0/1 ContainerCreating 0 1s
myapp-deploy-576d479bcc-hgb6w 0/1 Terminating 0 13m
myapp-deploy-576d479bcc-bzqwv 0/1 Terminating 0 13m
myapp-deploy-7f969b64fb-fwdbr 0/1 ContainerCreating 0 18s
myapp-deploy-7f969b64fb-gprvk 0/1 ContainerCreating 0 19s
myapp-deploy-576d479bcc-hgb6w 0/1 Terminating 0 13m
myapp-deploy-576d479bcc-bt7hz 0/1 Terminating 0 49m
myapp-deploy-576d479bcc-bzqwv 0/1 Terminating 0 13m
myapp-deploy-7f969b64fb-gprvk 1/1 Running 0 20s
myapp-deploy-7f969b64fb-fwdbr 1/1 Running 0 20s
kubectl get rs -o wide #使用命令查看
可以看到當(dāng)前鏡像版本已經(jīng)換成了v2 且模板也變了,之前的v1版本還保這方便回滾,yaml文件內(nèi)容修改一次就會(huì)當(dāng)做一次更新,最后一串?dāng)?shù)字就是通過yaml文件計(jì)算出來的
注意: deployment的pod模板被更改時(shí)才會(huì)創(chuàng)建新的修訂版本,例如更新模板標(biāo)簽或者容器鏡像可以觸發(fā)滾動(dòng)更新, 其它操作例如擴(kuò)展副本數(shù)將不會(huì)觸發(fā)deployment的更新操作
kubectl set image deploy myapp-deploy myapp-rongqi=ikubernetes/myapp:v3 && kubectl rollout pause deploy myapp-deploy
#kubectl set image deploy "控制器Name" "容器name"=ikubernetes/myapp:v3 && kubectl rollout pause deploy "控制器Name"
#原理就是先升級(jí)一個(gè)容器的版本,執(zhí)行pause 暫停升級(jí)任務(wù),待升級(jí)的那個(gè)容器經(jīng)過真實(shí)用戶測(cè)試后無錯(cuò)后解除暫停
kubectl get rs -o wide #查看控制器能看到,有1個(gè)v3版,5個(gè)v2版本,出現(xiàn)6個(gè)的原因是我滾動(dòng)策略定義了升級(jí)可允許存在6個(gè)
kubectl rollout resume deploy myapp-deploy
#解除暫停
#解除暫停后能看到,v2版本在一點(diǎn)點(diǎn)的關(guān)閉向v3版轉(zhuǎn)移,最終v2全部關(guān)閉,v3版5個(gè)容器全部正常上線
kubectl rollout history deployment/nginx-deployment
#查看升級(jí)歷史記錄
kubectl rollout history deployment/nginx-deployment --revision=2
#查看某個(gè)升級(jí)歷史記錄的的詳細(xì)信息
kubectl rollout undo deployment/nginx-deployment
#回滾到上一個(gè)版本
kubectl rollout undo deployment/nginx-deployment --to-revision=2
#回滾到指定版本