前面我们已经创建一个v1版本的http-server的pod在k8s环境中,如果我们想要做一次版本升级该怎么办呢?难道把原来的pod停掉,再使用新的镜像拉起来一个新的pod吗,这样做明显是不合适的。
我们不使用配置文件,直接使用kubectl rolling-update 命令,加上--images参数指定新版镜像的名称来完成Pod的滚动升级,执行结果是旧的RC会被删除,新的RC依然使用旧的名字,SVC则不需要重建,不影响业务;
1、我们制作一个v2版本的镜像,docker build -t bcmt-registry:5000/docker.io/http-server:v1.0.1 .
Dockerfile
# Docker image for springboot file run
# VERSION 2.0
# Author: Springboot Docker
# 基础镜像使用java
FROM java:8
# 作者
MAINTAINER daopinz
# VOLUME 指定了临时文件目录为/tmp。
# 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名为app.jar
ADD http-server-2.0.0-SNAPSHOT.jar /app.jar
# 运行jar包
RUN bash -c 'mkdir /config'
RUN bash -c 'mkdir /log'
ADD application.yml config/application.yml
ADD logback.xml config/logback.xml
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-DfilePath=config","-jar","/app.jar","--spring.config.location=/config/application.yml"]
2、同样我们需要把这个进行上传到镜像仓库,docker push xxx-xxx:5000/docker.io/http-server:v2.0
3、kubectl rolling-update http-server http-server-v2 --image=xxx-xxx:5000/docker.io/http-server:v2.0
[root ~]# kubectl rolling-update http-server http-server-v2 --image=xxx-xxx:5000/docker.io/http-server:v2.0
Command "rolling-update" is deprecated, use "rollout" instead
Created http-server-v2
Scaling up http-server-v2 from 0 to 2, scaling down http-server from 2 to 0 (keep 2 pods available, don't exceed 3 pods)
Scaling http-server-v2 up to 1
Scaling http-server down to 1
Scaling http-server-v2 up to 2
Scaling http-server down to 0
Update succeeded. Deleting http-server
replicationcontroller/http-server-v2 rolling updated to "http-server-v2"
可以看到,kubectl 新建一个新版本的pod,停掉旧的pod,逐步迭代来完成整个RC的版本更新的。
kubectl get pod -w #查看pod的实时状态,类似tail -f nohup.out
http-server-b4tf9 1/1 Running 0 1m
http-server-kvdw7 1/1 Running 0 1m
http-server-v2-fnv8j 0/1 Pending 0 0s
http-server-v2-fnv8j 0/1 Pending 0 0s
http-server-v2-fnv8j 0/1 ContainerCreating 0 0s
http-server-v2-fnv8j 1/1 Running 0 1s
http-server-kvdw7 1/1 Terminating 0 2m
http-server-kvdw7 0/1 Terminating 0 2m
http-server-v2-pw6qm 0/1 Pending 0 0s
http-server-v2-pw6qm 0/1 Pending 0 0s
http-server-v2-pw6qm 0/1 ContainerCreating 0 0s
http-server-v2-pw6qm 1/1 Running 0 3s
http-server-kvdw7 0/1 Terminating 0 2m
http-server-kvdw7 0/1 Terminating 0 2m
http-server-b4tf9 1/1 Terminating 0 3m
http-server-b4tf9 0/1 Terminating 0 3m
http-server-b4tf9 0/1 Terminating 0 3m
http-server-b4tf9 0/1 Terminating 0 3m
4、kubectl get rc 更新完成后查看RC
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
http-server-v2 2 2 2 44m http-server xxx-xxx:5000/docker.io/http-server:v2.0 deployment=a5a6a23ae1f188639be746aexxxxxxxx,name=http-server
会发现,给添加了一个key为deployment的label,便于我们观察区分是否更新到新的镜像信息;
5、执行过程中 若发现有错误的地方,可以中断更新操作,执行版本回滚;kubectl rolling-update http-server http-server-v2 --image=xxx-xxx:5000/docker.io/http-server:v2.0 --rollback
执行 则可以看到,Pod又恢复到之前的版本啦