K8S 之 Deployment声明式地升级应用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了K8S 之 Deployment声明式地升级应用相关的知识,希望对你有一定的参考价值。
作用:通过Deployment声明式平滑升级POD
一、创建Deployment文件
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: kubia
namespace: test
spec:
replicas: 3
template:
metadata:
name: kubia
labels:
app: kubia
spec:
containers:
- name: nodejs
image: luksa/kubia:v1 #镜像为v1版本
二、创建service提供负载服务
apiVersion: v1
kind: Service
metadata:
name: kubia
namespace: test
spec:
ports:
- name: http
port: 80
targetPort: 8080
selector:
app: kubia #标签匹配到kubia
三、创建delplyment与service
[root@test-nodes1 k8s-yaml-file]# kubectl create -f kubia-deployment-v1.yaml --record
deployment.apps/kubia created
[root@test-nodes1 k8s-yaml-file]# kubectl get all -n test
NAME READY STATUS RESTARTS AGE
pod/kubia-66b4657d7b-77t4k 1/1 Running 0 9m5s
pod/kubia-66b4657d7b-ggdft 1/1 Running 0 9m5s
pod/kubia-66b4657d7b-qtckn 1/1 Running 0 9m5s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/kubia 3/3 3 3 9m6s
NAME DESIRED CURRENT READY AGE
replicaset.apps/kubia-66b4657d7b 3 3 3 9m5s
四、采用deployment对pod进行升级(变更POD版本)
[root@test-nodes1 k8s-yaml-file]# kubectl set image deployment kubia nodejs=luksa/kubia:v2 -n test
deployment.extensions/kubia image updated
[root@test-nodes1 k8s-yaml-file]# kubectl get all -n test
NAME READY STATUS RESTARTS AGE
pod/kubia-66b4657d7b-77t4k 0/1 Terminating 0 16m #终止
pod/kubia-66b4657d7b-ggdft 1/1 Terminating 0 16m #终止
pod/kubia-66b4657d7b-qtckn 1/1 Terminating 0 16m #终止
pod/kubia-75974f96b5-b7rhd 1/1 Running 0 27s
pod/kubia-75974f96b5-q4txq 1/1 Running 0 34s
pod/kubia-75974f96b5-tdpbf 1/1 Running 0 43s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubia ClusterIP 192.168.253.59 <none> 80/TCP 4m26s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/kubia 3/3 3 3 16m
NAME DESIRED CURRENT READY AGE
replicaset.apps/kubia-66b4657d7b 0 0 0 16m
replicaset.apps/kubia-75974f96b5 3 3 3 43s
[root@test-nodes1 k8s-yaml-file]# kubectl get deployment kubia -o wide -n test
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
kubia 3/3 3 3 20m nodejs luksa/kubia:v2 app=kubia
四、deployment回滚升级(当升级失败时)
$kubectl rollout undo deployment kubia -n test
提示:undo命令也可以在滚动升级过程中运行,并直接停止滚动升级。
在升级过程中已创建的pod会被删除,并被老版本的pod替代。
五、查看deployment滚动升级历史
[root@test-nodes1 k8s-yaml-file]# kubectl rollout history deployment kubia -n test
deployment.extensions/kubia
REVISION CHANGE-CAUSE
1 kubectl create --filename=kubia-deployment-v1.yaml --record=true
3 kubectl create --filename=kubia-deployment-v1.yaml --record=true
4 kubectl create --filename=kubia-deployment-v1.yaml --record=true
六、将deployment 回滚到相应的版本(如版本1)
[root@test-nodes1 k8s-yaml-file]# kubectl rollout undo deployment kubia --to-revision=1 -n test
deployment.extensions/kubia rolled back
[root@test-nodes1 k8s-yaml-file]# kubectl get deployment kubia -o wide -n test
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
kubia 3/3 3 3 14m nodejs luksa/kubia:v1 app=kubia
以上是关于K8S 之 Deployment声明式地升级应用的主要内容,如果未能解决你的问题,请参考以下文章