本章是《kubernetes下的Nginx加Tomcat三部曲系列》的终篇,今天咱们一起在kubernetes环境对下图中tomcat的数量进行调整,再修改tomcat中web工程的源码,并将现有的tomcat的pod全部替换成新代码构建的结果:
root@master:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
ng-59b887b8bc-jzcv5 1/1 Running 3 2d
tomcathost-7f68566795-8pl29 1/1 Running 3 2d
tomcathost-7f68566795-mvg5f 1/1 Running 3 2d
tomcathost-7f68566795-trscg 1/1 Running 3 2d
如上所示,目前是三个tomcat的pod;
root@master:~# kubectl scale deployment tomcathost --replicas=5
deployment "tomcathost" scaled
root@master:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
ng-59b887b8bc-jzcv5 1/1 Running 3 2d
tomcathost-7f68566795-8kf76 1/1 Running 0 18s
tomcathost-7f68566795-8pl29 1/1 Running 3 2d
tomcathost-7f68566795-mvg5f 1/1 Running 3 2d
tomcathost-7f68566795-tp5xp 1/1 Running 0 18s
tomcathost-7f68566795-trscg 1/1 Running 3 2d
tomcat的pod已经增加到5个了,创建时间都只有18秒;
点击上图红框1,再点击tomcathost这个服务,可以看到上图中的详情; 上图红框2中是5个tomcat的pod的容器IP地址; 点击上图红框3中的每个容器名,可以查看容器详情; 上图红框4显示了刚才的扩容事件;
nginx服务所在的node机器的IP地址是192.168.119.153,所以在浏览器上访问:http://192.168.119.153:30006/getserverinfo,反复刷新此页面,看到返回的IP地址在不断的更新,都是tomcat所在pod的IP地址,5个都会出现,如下图:
扩容实战就到这里,接下来我们修改web工程的源码,验证在线升级的能力;
tomcat上运行的web工程源码,可以GitHub下载,地址和链接信息如下表所示:
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
这个git项目中有多个目录,本次的web工程源码放在k8stomcatdemo,如下图红框所示:
@RequestMapping(value = "/getserverinfo", method = RequestMethod.GET)
public String getUserInfoWithRequestParam(){
return String.format("server : %s, time : %s", getIPAddr(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
@RequestMapping(value = "/getserverinfo", method = RequestMethod.GET)
public String getUserInfoWithRequestParam(){
return String.format("From new version, server : %s, time : %s", getIPAddr(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
<groupId>com.bolingcavalry</groupId>
<artifactId>k8stomcatdemo</artifactId>
<version>0.0.3</version>
<packaging>jar</packaging>
<name>k8stomcatdemo</name>
root@maven:/usr/local/work/k8s/k8stomcatdemo# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
bolingcavalry/k8stomcatdemo 0.0.3 5f2d27eafd1b 17 seconds ago 658 MB
bolingcavalry/k8stomcatdemo latest 5f2d27eafd1b 17 seconds ago 658 MB
现在的镜像只存在于开发和构建web工程的电脑上,为了让kubernetes的node机器能用上这个镜像,可以用以下几种方式实现:
kubectl set image deployment/tomcathost tomcathost=bolingcavalry/k8stomcatdemo:0.0.3
root@maven:/usr/local/work/k8s/k8stomcatdemo# kubectl set image deployment/tomcathost tomcathost=bolingcavalry/k8stomcatdemo:0.0.3
deployment "tomcathost" image updated
root@maven:/usr/local/work/k8s/k8stomcatdemo# kubectl get pod
NAME READY STATUS RESTARTS AGE
ng-59b887b8bc-jzcv5 1/1 Running 3 2d
tomcathost-6dfc87dc8b-9bkfv 1/1 Running 0 50s
tomcathost-6dfc87dc8b-h6gx4 0/1 ContainerCreating 0 50s
tomcathost-6dfc87dc8b-ht2d8 1/1 Running 0 18s
tomcathost-6dfc87dc8b-pfb56 1/1 Running 0 10s
tomcathost-6dfc87dc8b-x8pnn 1/1 Running 0 14s
tomcathost-7f68566795-8pl29 0/1 Terminating 3 2d
tomcathost-7f68566795-trscg 0/1 Terminating 3 2d
在之前的文章中,我们实战了SpringCloud环境下服务提供方的扩容,由于是“注册&发现”的方式,扩容只需要往SpringCloud环境添加provider机器,不需要做什么设置,请求就会被落到新的provider上,相关实战的地址如下:
在Nginx加Tomcat环境中,如果不是在kubernetes环境,我们增加Tomcat之后需要修改Nginx配置,否则到达Nginx的请求不会被转发到新的机器上去,幸运的是kubernete帮我们解决了这个问题,扩容缩容都只需要控制副本数即可,不用修改Nginx配置了;
doceker-compose虽然可以修改副本数,但是Nginx的配置仍然需要修改,否则新创建的Tomcat容器都有自己的IP地址,Nginx还是感知不到;
至此,kubernetes下的Nginx加Tomcat三部曲就全部结束了,希望能在您的kubernetes实战中有所帮助;