构建业务镜像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. Codeforces Round #697 (Div. 3) D. Cleaning the Phone (思维,前缀和)

    题意:你的手机有\(n\)个app,每个app的大小为\(a_i\),现在你的手机空间快满了,你需要删掉总共至少\(m\)体积的app,每个app在你心中的珍惜值是\(b_i\),\(b_i\)的取值 ...

  2. hdu4533 威威猫系列故事——晒被子

    Problem Description 因为马拉松初赛中吃鸡腿的题目让不少人抱憾而归,威威猫一直觉得愧对大家,这几天他悄悄搬到直角坐标系里去住了. 生活还要继续,太阳也照常升起,今天,威威猫在第一象限 ...

  3. TCP协议与UDP协议的区别以及与TCP/IP协议的联系

    先介绍下什么是TCP,什么是UDP. 1. 什么是TCP? TCP(Transmission Control Protocol,传输控制协议)是面向连接的.可靠的字节流服务,也就是说,在收发数据前,必 ...

  4. c# App.xaml

    随着wpf自动创建的,是项目的起始点..Net先再App里找,找到了window然后开启window,项目真正的起始点是在App里. 这两个 (App 的xaml和cs文件)和MainWindow 的 ...

  5. 翻译:《实用的Python编程》01_03_Numbers

    目录 | 上一节 (1.2 第一个程序) | 下一节 (1.4 字符串) 1.3 数字 本节讨论数学计算. 数字类型 Python 有 4 种类型的数字: 布尔型 整型 浮点型 复数(虚数) 布尔型( ...

  6. 加密算法大全图解 :密码体系,对称加密算法,非对称加密算法,消息摘要, Base64,数字签名,RSA,DES,MD5,AES,SHA,ElGamal,

    1. 加密算法大全: ***************************************************************************************** ...

  7. H5 CSS 悬浮滚动条

    H5 CSS 悬浮滚动条 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  8. iOS WebView All In One

    iOS WebView All In One WKWebView / UIWebView Swift Playground //: A UIKit based Playground for prese ...

  9. The Weekly Web Dev Challenge: Emoji Ratings

    The Weekly Web Dev Challenge: Emoji Ratings /* DESCRIPTION: You job is to enable users to give a rat ...

  10. 为什么国内的好多具备 HTTPS 的网站却没有使用 HTTPS 重定向功能

    为什么国内的好多具备 HTTPS 的网站却没有使用 HTTPS 重定向功能 HTTPS 重定向 good demos ️ HTTPS http://www.xgqfrms.xyz/ https://w ...