当前位置 : 主页 > 网络编程 > 其它编程 >

dockerDockerfile学习---nginx负载均衡tomcat服务

来源:互联网 收集:自由互联 发布时间:2023-07-02
1、此过程在nginx的基础上,也就是上篇博客写的内容。2、创建项目目录并上传包,解压$mkdircentos_tomcat$cdcentos_tomcat$ 1、此过程在nginx的基础上,也就是上篇博客写的内容。 2、创建项目目
1、此过程在nginx的基础上,也就是上篇博客写的内容。2、创建项目目录并上传包,解压$mkdircentos_tomcat$cdcentos_tomcat$

1、此过程在nginx的基础上,也就是上篇博客写的内容。

2、创建项目目录并上传包,解压

$ mkdir centos_tomcat$ cd centos_tomcat

$ tar zxvf jdk-7u80-linux-x64.tar.gz $ tar zxvf apache-tomcat-7.0.94.tar.gz

3、编辑Dockerfile

# From表示使用centos:latest这个镜像为基础构建我们的镜像FROM centos:latest# 创建者的基本信息MAINTAINER xiaozhou (xiaozhou@docker.com)LABEL Discription="基于centos的nginx镜像" version="1.0"# 把jdk文件拷到容器ADD jdk1.7.0_80 /usr/local/java# 设置环境变量ENV JAVA_HOME /usr/local/javaENV JAVA_BIN /usr/local/java/binENV JRE_HOME /usr/local/java/jreENV PATH $PATH:/usr/local/java/bin:/usr/local/java/jre/binENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar# 把tomcat文件拷到容器ADD apache-tomcat-7.0.94 /usr/local/tomcat7# 服务端口EXPOSE 8080 View Code

4、构建镜像

$ docker build -t centos:tomcat .

5、启动容器

启动tomcat1

$ docker run --name tomcat01 -d -p 8081:8080 -it centos:tomcat /bin/bash23a5ff7617772787f644a510b201fc343d541946ea12aedfb13bb2ab64dbfdeb

启动tomcat2

$ docker run --name tomcat02 -d -p 8082:8080 -it centos:tomcat /bin/bashb016a1cd5a1e7da602a11243107840bf6eff47f8321a284a1f15c95988228b86

6、启动tomcat服务

登陆容器,启动服务

启动服务1

$ docker exec -it 23a5ff761777 /bin/bash[root@23a5ff761777 /]# cd /usr/local/tomcat7/bin[root@23a5ff761777 bin]# ./startup.sh Using CATALINA_BASE: /usr/local/tomcat7Using CATALINA_HOME: /usr/local/tomcat7Using CATALINA_TMPDIR: /usr/local/tomcat7/tempUsing JRE_HOME: /usr/local/java/jreUsing CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jarTomcat started.

启动服务2

$ docker exec -it b016a1cd5a1e /bin/bash[root@b016a1cd5a1e /]# cd /usr/local/tomcat7/bin[root@b016a1cd5a1e bin]# ./startup.sh Using CATALINA_BASE: /usr/local/tomcat7Using CATALINA_HOME: /usr/local/tomcat7Using CATALINA_TMPDIR: /usr/local/tomcat7/tempUsing JRE_HOME: /usr/local/java/jreUsing CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jarTomcat started.

7、验证并修改默认页面

浏览器输入:http://192.168.121.121:8081

出现tomcat服务的默认访问页面

浏览器输入:http://192.168.121.121:8082

出现tomcat服务的默认访问页面

登陆容器tomcat1

[root@23a5ff761777 /]# cd /usr/local/tomcat7/webapps/ROOT[root@23a5ff761777 ROOT]# rm -f index.jsp[root@23a5ff761777 ROOT]# vi index.html Simple Webserver Server1!

登陆容器tomcat2

[root@b016a1cd5a1e /]# cd /usr/local/tomcat7/webapps/ROOT[root@b016a1cd5a1e ROOT]# rm -f index.jsp[root@b016a1cd5a1e ROOT]# vi index.html Simple Webserver Server2!

刷新打开的访问页面,发现默认tomcat页面变化为

Simple Webserver Server1! 和 Simple Webserver Server2!

8、修改nginx配置

查看所有容器:

$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb016a1cd5a1e centos:tomcat "/bin/bash" 11 minutes ago Up 11 minutes 0.0.0.0:8082->8080/tcp tomcat0223a5ff761777 centos:tomcat "/bin/bash" 16 minutes ago Up 12 minutes 0.0.0.0:8081->8080/tcp tomcat01d350b2c8a3d5 centos:nginx "/bin/sh -c '/bin/sh…" 30 minutes ago Up 30 minutes 0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp wonderful_elion

查看tomcat容器IP:

登陆tomcat1

$ docker exec -it 23a5ff761777 /bin/bash[root@23a5ff761777 /]# yum install net-tools -y [root@23a5ff761777 /]# ifconfigeth0: flags=4163 mtu 1500 inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet) RX packets 2975 bytes 14039534 (13.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2479 bytes 160092 (156.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

登陆tomcat2

[root@b016a1cd5a1e /]# yum install net-tools -y[root@b016a1cd5a1e /]# ifconfigeth0: flags=4163 mtu 1500 inet 172.17.0.4 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:04 txqueuelen 0 (Ethernet) RX packets 2541 bytes 14008601 (13.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2244 bytes 227840 (222.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

登陆nginx容器

$ docker exec -it d350b2c8a3d5 bash

修改nginx配置文件

[root@d350b2c8a3d5 nginx-1.8.0]# vi /usr/local/nginx/conf/nginx.conf 在http{} 添加upstream tomcat_server {server 172.17.0.3:8080 weight=1;server 172.17.0.4:8080 weight=1;}在server{} 修改内容为location / {proxy_pass http://tomcat_server;proxy_redirect default;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;}

重载配置

[root@d350b2c8a3d5 nginx-1.8.0]# cd /usr/local/nginx/sbin[root@d350b2c8a3d5 sbin]# ./nginx -s reload

浏览器输入:http://192.168.121.121:32769/

出现tomcat测试内容信息,刷新页面后出现另一个tomcat的测试内容,页面轮训出现结果。

参考:

http://www.mamicode.com/info-detail-2412717.html

上一篇:codeforcesround#804(div.2)A–C
下一篇:没有了
网友评论