构建业务镜像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

观察状态页,发现后端服务器上线

Docker搭建HAproxy+tomcat 实现高可用的更多相关文章

  1. 搭建 Keepalived + Nginx + Tomcat 的高可用负载均衡架构

    1 概述 初期的互联网企业由于业务量较小,所以一般单机部署,实现单点访问即可满足业务的需求,这也是最简单的部署方式,但是随着业务的不断扩大,系统的访问量逐渐的上升,单机部署的模式已无法承载现有的业务量 ...

  2. Keepalived + Nginx + Tomcat 的高可用负载均衡架构搭建

    Keepalived + Nginx + Tomcat 的高可用负载均衡架构搭建 Nginx 是一个高性能的 HTTP反向代理服务器 Keepalived 是一个基于VRRP协议来实现的LVS服务高可 ...

  3. 搭建Keepalived + Nginx + Tomcat的高可用负载均衡架构

    1 概述 初期的互联网企业由于业务量较小,所以一般单机部署,实现单点访问即可满足业务的需求,这也是最简单的部署方式,但是随着业务的不断扩大,系统的访问量逐渐的上升,单机部署的模式已无法承载现有的业务量 ...

  4. docker compose搭建redis7.0.4高可用一主二从三哨兵集群并整合SpringBoot【图文完整版】

    一.前言 redis在我们企业级开发中是很常见的,但是单个redis不能保证我们的稳定使用,所以我们要建立一个集群. redis有两种高可用的方案: High availability with Re ...

  5. haproxy+keepalived实现高可用负载均衡

    软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第三方应用的软负载实现.LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载. HA ...

  6. haproxy+keepalived实现高可用负载均衡(转)

      软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第三方应用的软负载实现.LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载. ...

  7. Nginx+Tomcat+memcached高可用会话保持

    Nginx+Tomcat+memcached高可用会话保持 文章来源dyc2005   一.概述 之前文章已经描述了企业高可用负载相关的架构及实现,其中常用的nginx或haproxy,LVS结合ke ...

  8. Keepalived+Nginx+Tomcat 实现高可用Web集群

    https://www.jianshu.com/p/bc34f9101c5e Keepalived+Nginx+Tomcat 实现高可用Web集群 0.3912018.01.08 20:28:59字数 ...

  9. linux -- 基于zookeeper搭建yarn的HA高可用集群

    linux -- 基于zookeeper搭建yarn的HA高可用集群 实现方式:配置yarn-site.xml配置文件 <configuration> <property> & ...

随机推荐

  1. HDU-3499Flight (分层图dijkstra)

    一开始想的并查集(我一定是脑子坏掉了),晚上听学姐讲题才知道就是dijkstra两层: 题意:有一次机会能使一条边的权值变为原来的一半,询问从s到e的最短路. 将dis数组开成二维,第一维表示从源点到 ...

  2. 2020-2021 ICPC, NERC, Southern and Volga Russian Regional Contest (Online Mirror, ICPC Rules) D. Firecrackers (贪心,二分)

    题意:有个长度为\(n\)的监狱,犯人在位置\(a\),cop在位置\(b\),你每次可以向左或者向右移动一个单位,或者选择不动并在原地放一个爆竹\(i\),爆竹\(i\)在\(s[i]\)秒后爆炸, ...

  3. hdu1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  4. Codeforces Round #653 (Div. 3) D. Zero Remainder Array (数学,模拟)

    题意:有一组数,刚开始时\(x=0\),每次可以让\(x\)++或让某一个元素+=\(x\)后\(x\)++,每个元素只能加一次\(x\),问最少操作多少次使得所有元素能被\(k\)整除. 题解:每个 ...

  5. Kafka SASL/SCRAM+ACL实现动态创建用户及权限控制

    kafka系列文章 第一章 linux单机安装kafka 第二章 kafka--集群安裝部署(自带zookeeper) 第三章 Kafka SASL/SCRAM+ACL实现动态创建用户及权限控制 Ka ...

  6. IDEA 安装常用操作一

    关于IDEA的下载,破解自行百度 一.安装完成的常用设置 SDK选择.编译版本的选择,单项目选择,全局选择 maven配置,单项目,全局配置 二.IDEA如何安装lombok https://www. ...

  7. 微服务架构Day04-SpringBoot之web开发

    引入项目 把html页面放在模板引擎文件夹templates下,这样能使用模板引擎的功能. 登录页面国际化 国际化:编写国际化配置文件 1.编写国际化配置文件,抽取页面需要显示的国际化消息 2.Spr ...

  8. 快速搞懂.NET 5/.NET Core应用程序的发布部署

    .NET Framework时代,.NET 应用程序大多直接部署运行在Windows服务器上,当然也可以通过Mono部署运行在Linux上.无论部署exe,还是IIS站点.或是Windows Serv ...

  9. JavaScript this All In One

    JavaScript this All In One js, this, bind, call, apply, new, function, arrow function, constructor f ...

  10. LeetCode & list cycle

    LeetCode & list cycle 链表是否存在环检测 singly-linked list 单链表 "use strict"; /** * * @author x ...