Docker搭建HAproxy+tomcat 实现高可用
构建业务镜像1
创建tomcat-app1和tomcat-app2两个目录,代表不同的两个基于tomcat的业务。
准备tomcat的配置文件
[root@localhost ~]#mkdir -p /data/dockerfile/web/tomcat/tomcat-app{1,2}
[root@localhost ~]#tree /data/dockerfile/web/tomcat/
/data/dockerfile/web/tomcat/
├── tomcat-app1
├── tomcat-app2
#上传和修改server.xml
[root@localhost ~]#cd /data/dockerfile/web/tomcat/tomcat-base-8.5.50
[root@localhost tomcat-base-8.5.50]# tar xf apache-tomcat-8.5.50.tar.gz
[root@localhost tomcat-base-8.5.50]# cp apache-tomcat-8.5.50/conf/server.xml /data/dockerfile/web/tomcat/tomcat-app1/
[root@localhost tomcat-base-8.5.50]# cd /data/dockerfile/web/tomcat/tomcat-app1/
[root@localhost tomcat-app1]#vim server.xml
<Host name=”localhost” appBase=”/data/tomcat/webapps”
unpackWARs=”true” autoDeploy=”true”>
准备自定义页面
[root@localhost tomcat-app1]# mkdir app
[root@localhost tomcat-app1]# echo “Tomcat Page in app1” > app/index.jsp
[root@localhost tomcat-app1]# tar zcf app.tar.gz app
准备容器启动执行脚本
[root@localhost tomcat-app1]# vim run_tomcat.sh
[root@localhost tomcat-app1]# cat run_tomcat.sh
#!/bin/bash
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Author: Santiago_hao
#Data: 2021-03-04
#FileName: run_tomcat.sh
#Desrition: The test script
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo “nameserver 180.76.76.76” >etc/resolv.conf
su – www -c “/apps/tomcat/bin/catalina.sh start”
su – www -c “tail -f /etc/hosts”
[root@localhost tomcat-app1]# chmod a+x run_tomcat.sh
准备Dockerfile
[root@localhost tomcat-app1]# vim Dockerfile
[root@localhost tomcat-app1]# cat Dockerfile
#Tomcat Web Image
FROM tomcat-base:v8.5.50 #这个基础镜像要提前准备好
LABEL maintainer=”santiagohaojun”
ADD server.xml /apps/tomcat/conf/server.xml
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD app.tar.gz /data/tomcat/webapps/
RUN useradd www &&chown -R www.www /apps/ /data/tomcat/
EXPOSE 8080 8009
CMD [“/apps/tomcat/bin/run_tomcat.sh”]
执行构建脚本制作镜像
[root@localhost tomcat-app1]# vim build.sh
[root@localhost tomcat-app1]# cat build.sh
#!/bin/bash
docker build -t tomcat-web:app1 .
[root@localhost tomcat-app1]# pwd
/data/dockerfile/web/tomcat/tomcat-app1
[root@localhost tomcat-app1]# tree
.
├── app
│ └── index.jsp
├── app.tar.gz
├── build.sh
├── Dockerfile
├── run_tomcat.sh
└── server.xml
[root@localhost tomcat-app1]# bash build.sh
[root@localhost tomcat-app1]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat-web app1 3e9eacc5ef86 4 seconds ago 824MB
tomcat-base v8.5.50 8d5395cb72c4 35 minutes ago 824MB
centos7-jdk 8u212 e0fe770a7ccd 57 minutes ago 809MB
centos7-base v1 34ab3afcd3b3 2 hours ago 403MB
centos centos7.7.1908 08d05d1d5859 2 months ago 204MB
从镜像启动测试容器
[root@localhost tomcat-app1]# docker run -d -p 8080:8080 tomcat-web:app1
82e6690e36c3a6faf2dae62bd706a89cbba490d567c841c37501f0fba670ea25
访问测试
[root@localhost ~]#curl 127.0.0.1:8080/app/
Tomcat Page in app1
[root@localhost ~]#docker exec -it 82e6690e36c3 bash
[root@82e6690e36c3 /]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.2 15136 2248 ? Ss 22:14 0:00 /bin/bash /apps/tomcat/bin/run_tomcat.sh
www 25 0.8 9.7 2241656 95924 ? Sl 22:14 0:04 /usr/local/jdk/bin/java -Djava.util.logging.config.file=/apps/tomcat
root 26 0.0 0.4 85428 4472 ? S 22:14 0:00 su – www -c tail -f /etc/hosts
www 27 0.0 0.0 4416 720 ? Ss 22:14 0:00 tail -f /etc/hosts
root 82 25.0 0.3 15800 3820 pts/0 Ss 22:22 0:00 bash
root 101 0.0 0.3 55196 3836 pts/0 R+ 22:22 0:00 ps aux
[root@82e6690e36c3 /]# vim /data/tomcat/webapps/app/index.jsp
[root@82e6690e36c3 /]# cat /data/tomcat/webapps/app/index.jsp
Tomcat Page in app1 v2
[root@82e6690e36c3 /]# /apps/tomcat/bin/catalina.sh stop
Using CATALINA_BASE: /apps/tomcat
Using CATALINA_HOME: /apps/tomcat
Using CATALINA_TMPDIR: /apps/tomcat/temp
Using JRE_HOME: /usr/local/jdk/jre
Using CLASSPATH: /apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jar
[root@82e6690e36c3 /]# /apps/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /apps/tomcat
Using CATALINA_HOME: /apps/tomcat
Using CATALINA_TMPDIR: /apps/tomcat/temp
Using JRE_HOME: /usr/local/jdk/jre
Using CLASSPATH: /apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@localhost tomcat-app1]# curl 127.0.0.1:8080/app/
Tomcat Page in app1 v2
构建业务镜像2
准备自定义页面和其它数据
[root@ubuntu1804 tomcat]#pwd
/data/dockerfile/web/tomcat
[root@localhost tomcat]# cp -a tomcat-app1/* tomcat-app2/
[root@localhost tomcat]# tree tomcat-app2/
tomcat-app2/
├── app
│ └── index.jsp
├── app.tar.gz
├── build.sh
├── Dockerfile
├── run_tomcat.sh
└── server.xml
[root@localhost tomcat]# cd tomcat-app2
[root@localhost tomcat-app2]#echo “Tomcat Page in app2” > app/index.html
[root@localhost tomcat-app2]#rm -f app.tar.gz
[root@localhost tomcat-app2]#tar zcf app.tar.gz app
准备容器启动脚本run_tomcat.sh,和业务1一样不变
准备Dockerfile,和业务1一样不变
执行构建脚本制作镜像
[root@localhost tomcat-app2]#vim build.sh
[root@localhost tomcat-app2]#cat build.sh
#!/bin/bash
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Author: Santiago_hao
#Data: 2021-03-04
#FileName: build.sh
#Desrition: The test script
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
docker build -t tomcat-web:app2 .
[root@localhost tomcat-app2]#bash build.sh
[root@localhost tomcat-app2]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat-web app2 0e1760fe79a6 37 seconds ago 838MB
tomcat-web app1 76016219a0ca 27 minutes ago 838MB
tomcat-base v8.5.50 8d5395cb72c4 2 hours ago 824MB
centos7-jdk 8u212 e0fe770a7ccd 2 hours ago 809MB
centos7-base v1 34ab3afcd3b3 3 hours ago 403MB
centos centos7.7.1908 08d05d1d5859 2 months ago 204MB
从镜像启动容器测试
[root@localhost tomcat-app2]#docker run -d -p 8081:8080 tomcat-web:app2
3fc9437e42099e92f88e8e09bac0507f2d837ac8a6dba8cb1e4efc934bcf81ff
访问测试
[root@localhost tomcat-app2]#curl 127.0.0.1:8081/app/
Tomcat Page in app2
准备haproxy源码文件
[root@localhost ~]#mkdir -p /data/dockerfile/web/haproxy/2.1.11-centos7
[root@localhost ~]#cd /data/dockerfile/web/haproxy/2.1.11-centos7
[root@localhost 2.2.11-centos7]# wget https://www.haproxy.org/download/2.1/src/haproxy-2.1.11.tar.gz
#准备haproxy启动脚本
[root@localhost 2.2.11-centos7]#vim run_haproxy.sh
[root@localhost 2.2.11-centos7]#cat run_haproxy.sh
#!/bin/bash
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Author: Santiago_hao
#Data: 2021-03-04
#FileName: run_haproxy.sh
#Desrition: The test script
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
haproxy -f /etc/haproxy/haproxy.cfg
tail -f /etc/hosts
#准备haproxy配置文件
[root@localhost 2.2.11-centos7]# vim haproxy.cfg
[root@localhost 2.2.11-centos7]# cat haproxy.cfg
global
chroot /apps/haproxy
#stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
uid 99
gid 99
daemon
nbproc 1
pidfile /apps/haproxy/run/haproxy.pid
log 127.0.0.1 local3 info
defaults
option http-keep-alive
option forwardfor
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
listen stats
mode http
bind 0.0.0.0:9999
stats enable
log global
stats uri /haproxy-status
stats auth haadmin:123456 #用户名和密码
listen web_port
bind 0.0.0.0:80
mode http
log global
balance roundrobin
server web1 10.0.0.152:8080 check inter 3000 fall 2 rise 5
server web2 10.0.0.152:8081 check inter 3000 fall 2 rise 5
准备Dockerfile
[root@localhost 2.2.11-centos7]# pwd
/data/dockerfile/web/haproxy/2.2.11-centos7
[root@localhost 2.2.11-centos7]# vim Dockerfile
[root@localhost 2.2.11-centos7]# cat Dockerfile
FROM centos7-base:v1 #这里的基础镜像要提前准备好,准备基础镜像的方法,请看下一篇笔记
LABEL maintainer=”santiagohaojun”
ADD haproxy-2.1.11.tar.gz /usr/local/src/
RUN cd /usr/local/src/haproxy-2.1.11 \
&& make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/apps/haproxy \
&& make install PREFIX=/apps/haproxy \
&& ln -s /apps/haproxy/sbin/haproxy /usr/sbin/ \
&& mkdir /apps/haproxy/run \
&& rm -rf /usr/local/src/haproxy*
ADD haproxy.cfg /etc/haproxy/
ADD run_haproxy.sh /usr/bin
EXPOSE 80 9999
CMD [“run_haproxy.sh”]
准备构建脚本构建haproxy镜像
[root@localhost 2.2.11-centos7]# vim build.sh
[root@localhost 2.2.11-centos7]# cat build.sh
#!/bin/bash
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Author: Santiago_hao
#Data: 2021-03-04
#FileName: build.sh
#Desrition: The test script
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
docker build -t haproxy-centos7:2.1.11 .
[root@localhost 2.2.11-centos7]# chmod +x *.sh
[root@localhost 2.2.11-centos7]# ls
build.sh Dockerfile haproxy-2.1.11.tar.gz haproxy.cfg run_haproxy.sh
[root@localhost 2.1.11-centos7]#bash build.sh
[root@localhost 2.1.11-centos7]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy-centos7 2.1.11 e2658ad3a7be 47 minutes ago 460MB
tomcat-web app2 d50ebfecb92d About an hour ago 870MB
tomcat-web app1 2a58ff83a520 2 hours ago 870MB
tomcat-base v8.5.63 12f5a4caf2f8 2 hours ago 855MB
centos7-jdk v1 4b877c35fc80 3 hours ago 840MB
centos7-base v1 c78b143f3fcd 27 hours ago 434MB
[root@localhost 2.1.11-centos7]#docker run -d -p 80:80 -p 9999:9999 haproxy-centos7:2.1.11
e0a7c827cb5fdd5a630f7dfe58b1f60822da18929a4dfeccb7490fb78403e3df
[root@localhost ~]# curl 10.0.0.152:8080/app/
Tomcat Page in app1
[root@localhost ~]# curl 10.0.0.152:8081/app/
Tomcat Page in app2
[root@localhost ~]# curl 10.0.0.152/app/
Tomcat Page in app2
[root@localhost ~]#
[root@localhost ~]# curl 10.0.0.152/app/
Tomcat Page in app1
[root@localhost ~]# curl 10.0.0.152/app/
Tomcat Page in app2
[root@localhost ~]# curl 10.0.0.152/app/
Tomcat Page in app1
网页登录查看
http://10.0.0.152:9999/haproxy-status
用户名:haadmin
密码:123456
停一个容器后再查看
[root@localhost ~]# docker stop 58a13f6e9c3d
58a13f6e9c3d
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7f6889f41dbb haproxy-centos7:2.1.11 “run_haproxy.sh” 55 minutes ago Up 55 minutes 0.0.0.0:80->80/tcp, 0.0.0.0:9999->9999/tcp nifty_ramanujan
ae30c7174096 tomcat-web:app1 “/apps/tomcat/bin/ru…” 2 hours ago Up 2 hours 8009/tcp, 0.0.0.0:8080->8080/tcp beautiful_engelbart
#观察状态页,发现后端服务器down
恢复容器
[root@localhost ~]# docker start 58a13f6e9c3d
[root@localhost ~]# docker start 58a13f6e9c3d
58a13f6e9c3d
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7f6889f41dbb haproxy-centos7:2.1.11 “run_haproxy.sh” 57 minutes ago Up 57 minutes 0.0.0.0:80->80/tcp, 0.0.0.0:9999->9999/tcp nifty_ramanujan
58a13f6e9c3d tomcat-web:app2 “/apps/tomcat/bin/ru…” 2 hours ago Up 7 seconds 8009/tcp, 0.0.0.0:8081->8080/tcp wonderful_chandrasekhar
ae30c7174096 tomcat-web:app1 “/apps/tomcat/bin/ru…” 2 hours ago Up 2 hours 8009/tcp, 0.0.0.0:8080->8080/tcp beautiful_engelbart
观察状态页,发现后端服务器上线