k8s使用Deployment实现应用系统不间断的自动更新
1、两个应用系统的版本,从V1升级到V2,已经打包在两个DOCKER镜像里面并上传到私有仓库
deployment如下:
[[email protected] upsystem]# cat war.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcatwars
spec:
selector:
matchLabels:
app: tomcatwar
replicas: 2
template:
metadata:
labels:
app: tomcatwar
spec:
imagePullSecrets:
- name: registry-key
containers:
- name: tomcatwar
image: re.bcdgptv.com.cn/war:v1
ports:
- containerPort: 8080
service如下:
[[email protected] upsystem]# cat warservice.yml
kind: Service
apiVersion: v1
metadata:
name: warservice
labels:
app: tomcatwar
spec:
type: NodePort
ports:
- port: 8080
protocol: TCP
nodePort: 8899
selector:
app: tomcatwar
2、构建deployment,再发布服务
[[email protected] upsystem]# kubectl create -f war.yml
[[email protected] upsystem]# kubectl create -f warservice.yml
service "warservice" created
[[email protected] upsystem]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/tomcatwars-5d779647bb-9l5vd 1/1 Running 0 37m
pod/tomcatwars-5d779647bb-dqf5d 0/1 Evicted 0 37m
pod/tomcatwars-5d779647bb-pdg8l 1/1 Running 5 32m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.254.0.1 <none> 443/TCP 43d
service/warservice NodePort 10.254.101.86 <none> 8080:8899/TCP 1m
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/tomcatwars 2 2 2 2 37m
NAME DESIRED CURRENT READY AGE
replicaset.apps/tomcatwars-5d779647bb 2 2 2 37m
此时访问应用:
3、发布新的镜像包
将配置文件的V1改成V2,已经将V2上传到私有仓库
[[email protected] upsystem]# cat war.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcatwars
spec:
selector:
matchLabels:
app: tomcatwar
replicas: 2
template:
metadata:
labels:
app: tomcatwar
spec:
imagePullSecrets:
- name: registry-key
containers:
- name: tomcatwar
image: re.bcdgptv.com.cn/war:v2
ports:
- containerPort: 8080
应用配置文件
[[email protected] upsystem]# kubectl apply -f war.yml
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
deployment.apps "tomcatwars" configured
[[email protected] upsystem]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/tomcatwars-5d779647bb-9l5vd 1/1 Running 0 1h
pod/tomcatwars-5d779647bb-dqf5d 0/1 Evicted 0 1h
pod/tomcatwars-5d779647bb-pdg8l 1/1 Running 5 1h
pod/tomcatwars-795c6cf95f-v7kdx 0/1 ErrImagePull 0 2m
pod/tomcatwars-795c6cf95f-z5cpl 0/1 Evicted 0 9m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.254.0.1 <none> 443/TCP 43d
service/warservice NodePort 10.254.159.193 <none> 8080:8899/TCP 23m
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/tomcatwars 2 3 1 2 1h
NAME DESIRED CURRENT READY AGE
replicaset.apps/tomcatwars-5d779647bb 2 2 2 1h
replicaset.apps/tomcatwars-795c6cf95f 1 1 0 9m
[[email protected] upsystem]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/tomcatwars-5d779647bb-dqf5d 0/1 Evicted 0 1h
pod/tomcatwars-795c6cf95f-6kn9w 1/1 Running 0 7m
pod/tomcatwars-795c6cf95f-v7kdx 1/1 Running 0 13m
pod/tomcatwars-795c6cf95f-z5cpl 0/1 Evicted 0 20m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.254.0.1 <none> 443/TCP 43d
service/warservice NodePort 10.254.159.193 <none> 8080:8899/TCP 34m
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/tomcatwars 2 2 2 2 1h
NAME DESIRED CURRENT READY AGE
replicaset.apps/tomcatwars-5d779647bb 0 0 0 1h
replicaset.apps/tomcatwars-795c6cf95f 2 2 2 20m
等POD更新完成以后,应用发布完成,更新过程中,应用访问不中断