结合Docker 搭建tomcat 集群

系统环境:Centos 7.6
最近研究学习了下Docker,借此机会用docker来搭建实现了下tomcat集群,以此加深对docker的认识。以此文记录相关的步骤细节及心得。

1、安装docker


yum install -y yum-utils device-mapper-persistent-data lvm2 epel-release

yum install -y docker

systemctl start docker

2、创建日志、配置文件目录

提前创建相应的日志文件目录和配置文件目录,用于挂载到相应的容器目录中。


[root@localhost ~]# mkdir /data/tomcatA/{catalina_logs,app_logs,conf,bin} -pv

mkdir: created directory `/data'

mkdir: created directory `/data/test'

mkdir: created directory `/data/test/catalina_logs'

mkdir: created directory `/data/test/app_logs'

mkdir: created directory `/data/test/conf'

mkdir: created directory `/data/test/bin'

[root@localhost ~]# mkdir /data/tomcatB/{catalina_logs,app_logs,conf,bin} -pv

mkdir: created directory ‘/data/tomcatB’

mkdir: created directory ‘/data/tomcatB/catalina_logs’

mkdir: created directory ‘/data/tomcatB/app_logs’

mkdir: created directory ‘/data/tomcatB/conf’

mkdir: created directory ‘/data/tomcatB/bin’

3、创建应用目录

用于存放相应的tomcat应用项目。


[root@localhost ~]# mkdir /usr/local/apps/{tomcatA,tomcatB} -pv

[root@localhost ~]# vim /usr/local/apps/tomcatA/index.jsp 

<%@ page language="java" %>

<html>

  <head><title>TomcatA</title></head>

  <body>

    <h1><font color="red">TomcatA.example.com</h1>

    <table align="centre" border="1">

      <tr>

        <td>Session ID</td>

    <% session.setAttribute("example.com","example.com"); %>

        <td><%= session.getId() %></td>

      </tr>

      <tr>

        <td>Created on</td>

        <td><%= session.getCreationTime() %></td>

     </tr>

    </table>

  </body>

</html>

[root@localhost ~]# vim /usr/local/apps/tomcatB/index.jsp 

<%@ page language="java" %>

<html>

  <head><title>TomcatB</title></head>

  <body>

    <h1><font color="green">TomcatB.example.com</h1>

    <table align="centre" border="1">

      <tr>

        <td>Session ID</td>

    <% session.setAttribute("example.com","example.com"); %>

        <td><%= session.getId() %></td>

      </tr>

      <tr>

        <td>Created on</td>

        <td><%= session.getCreationTime() %></td>

     </tr>

    </table>

  </body>

</html>

[root@localhost ~]# mkdir /usr/local/apps/tomcatA/WEB-INF/

[root@localhost ~]# mkdir /usr/local/apps/tomcatB/WEB-INF/

[root@localhost ~]# vim /usr/local/apps/tomcatA/WEB-INF/web.xml 

<?xml version="1.0" encoding="UTF-8"?>

<!--

 Licensed to the Apache Software Foundation (ASF) under one or more

  contributor license agreements. See the NOTICE file distributed with

  this work for additional information regarding copyright ownership.

  The ASF licenses this file to You under the Apache License, Version 2.0

  (the "License"); you may not use this file except in compliance with

  the License. You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software

  distributed under the License is distributed on an "AS IS" BASIS,

  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  See the License for the specific language governing permissions and

  limitations under the License.

-->

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

  version="3.1"

  metadata-complete="true">

<distributable/>            #分别在tomcatA、tomcatB的应用目录的WEB-INF的web.xml中添加此参数块。

  <display-name>Welcome to Tomcat</display-name>

  <description>

     Welcome to Tomcat

  </description>

</web-app>

4、复制创建相应的tomcat 配置文件


[root@localhost ~]# docker pull tomcat

#临时起一个tomcat容器,复制相应的配置文件,docker run 命令的--rm 选项可以在容器退出的时候直接删除容器

[root@localhost ~]# docker run --name tomcat-test --rm -it docker.io/tomcat /bin/bash

root@79b965c5e312:/usr/local/tomcat# 

#新起一个ssh终端复制相应的配置文件

[root@localhost ~]# docker cp tomcat-test:/usr/local/tomcat/conf/server.xml /data/tomcatA/conf/

[root@localhost ~]# docker cp tomcat-test:/usr/local/tomcat/conf/server.xml /data/tomcatB/conf/
[root@localhost ~]# docker cp tomcat-test:/usr/local/tomcat/bin/catalina.sh /data/tomcatA/bin/

[root@localhost ~]# docker cp tomcat-test:/usr/local/tomcat/bin/catalina.sh /data/tomcatB/bin/

5、配置运行Docker tomcat容器

调整docker tomcat的配置文件:


[root@localhost ~]# vim /data/tomcatA/bin/catalina.sh 

JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms512m -Xmx512m -Duser.timezone=Asia/Shanghai"

[root@localhost ~]# vim /data/tomcatB/bin/catalina.sh 

JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms512m -Xmx512m -Duser.timezone=Asia/Shanghai"

[root@localhost ~]# vim /data/tomcatA/conf/server.xml 

....

      <Host name="localhost" appBase="webapps"

            unpackWARs="true" autoDeploy="true">

        <Context path="" docBase="tomcatA" reloadable="false" />        #指定项目包的路径

        <!-- SingleSignOn valve, share authentication between web applications

             Documentation at: /docs/config/valve.html -->

        <!--

        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />

        -->

        <!-- Access log processes all example.

             Documentation at: /docs/config/valve.html

             Note: The pattern used is equivalent to using pattern="common" -->

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

               prefix="localhost_access_log" suffix=".txt"

               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>

.......

[root@localhost ~]# vim /data/tomcatB/conf/server.xml 

....

      <Host name="localhost" appBase="webapps"

            unpackWARs="true" autoDeploy="true">

        <Context path="" docBase="tomcatB" reloadable="false" />        #指定项目包的路径

        <!-- SingleSignOn valve, share authentication between web applications

             Documentation at: /docs/config/valve.html -->

        <!--

        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />

        -->

        <!-- Access log processes all example.

             Documentation at: /docs/config/valve.html

             Note: The pattern used is equivalent to using pattern="common" -->

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

               prefix="localhost_access_log" suffix=".txt"

               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>

.......

#docker run 参数选项解释

#--memory:指定容器运行的内存大小

#-d:以后台方式运行容器

#-v:指定挂载到容器中的目录,格式:宿主机目录:容器中的目录

#-p:指定容器映射的端口,格式:宿主机端口:容器端口
[root@localhost ~]# docker run -p 8080:8080 --name tomcatA --memory 512M -d -v /data/tomcatA/catalina_logs/:/usr/local/tomcat/logs/ -v /data/tomcatA/app_logs/:/usr/local/logs -v /usr/local/apps/:/usr/local/tomcat/webapps/ -v /data/tomcatA/conf/server.xml:/usr/local/tomcat/conf/server.xml -v /data/tomcatA/bin/catalina.sh:/usr/local/tomcat/bin/catalina.sh docker.io/tomcat

[root@localhost ~]# docker run -p 8081:8080 --name tomcatB --memory 512M -d -v /data/tomcatB/catalina_logs/:/usr/local/tomcat/logs/ -v /data/tomcatB/app_logs/:/usr/local/logs -v /usr/local/apps/:/usr/local/tomcat/webapps/ -v /data/tomcatB/conf/server.xml:/usr/local/tomcat/conf/server.xml -v /data/tomcatB/bin/catalina.sh:/usr/local/tomcat/bin/catalina.sh docker.io/tomcat

[root@localhost ~]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

2754623ea1de docker.io/tomcat "catalina.sh run" 2 minutes ago Up 37 seconds 0.0.0.0:8080->8080/tcp tomcatA             

3ab68e00ebdc docker.io/tomcat "catalina.sh run" 2 minutes ago Up 51 seconds 0.0.0.0:8081->8080/tcp tomcatB 

6、配置tomcat集群


#修改相应tomcat的配置信息,在host配置段中添加cluster配置段

[root@localhost ~]# vim /data/tomcatA/conf/server.xml

      <Host name="localhost" appBase="webapps"

            unpackWARs="true" autoDeploy="true">

        <Context path="" docBase="tomcatA" reloadable="false" />

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

               prefix="localhost_access_log" suffix=".txt"

               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

        <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"

                 channelSendOptions="8">

          <Manager className="org.apache.catalina.ha.session.DeltaManager"

                   expireSessionsOnShutdown="false"

                   notifyListenersOnReplication="true"/>

          <Channel className="org.apache.catalina.tribes.group.GroupChannel">

            <Membership className="org.apache.catalina.tribes.membership.McastService"

                        address="228.0.0.4"

                        port="45565"

                        frequency="50"

                        dropTime="3000"/>

            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"

                      address="auto"

                      port="4001"

                      autoBind="100"

                      selectorTimeout="5000"

                      maxThreads="10"/>

            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">

              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>

            </Sender>

            <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>

            <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatchInterceptor"/>

          </Channel>

          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"

                 filter=""/>

          <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>

        </Cluster>

      </Host>

[root@localhost ~]# vim /data/tomcatB/conf/server.xml

      <Host name="localhost" appBase="webapps"

            unpackWARs="true" autoDeploy="true">

<Context path="" docBase="tomcatB" reloadable="false" />

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

               prefix="localhost_access_log" suffix=".txt"

               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

        <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"

                 channelSendOptions="8">

          <Manager className="org.apache.catalina.ha.session.DeltaManager"

                   expireSessionsOnShutdown="false"

                   notifyListenersOnReplication="true"/>

          <Channel className="org.apache.catalina.tribes.group.GroupChannel">

            <Membership className="org.apache.catalina.tribes.membership.McastService"

                        address="228.0.0.4"

                        port="45565"

                        frequency="50"

                        dropTime="3000"/>

            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"

                      address="auto"

                      port="4001"

                      autoBind="100"

                      selectorTimeout="5000"

                      maxThreads="10"/>

            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">

              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>

            </Sender>

            <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>

            <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatchInterceptor"/>

          </Channel>

          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"

                 filter=""/>

          <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>

        </Cluster>

      </Host>

#重启Tomcat容器

[root@localhost ~]# docker restart tomcatA

tomcatA

[root@localhost ~]# docker restart tomcatB

tomcatB

7、验证docker部署的tomcat集群


[root@localhost ~]# yum install -y nginx

#查看相应的容器所使用的IP信息,docker inspect 可以查看当前运行的容器的相关信息,包括挂载的目录路径,网络IP信息等,--format可指定显示相匹配的json信息

[root@localhost ~]# docker inspect --format '{{.NetworkSettings}}' tomcatA

{ f602619757b50d5eefb76e816511589d11051e2414e0a30aaf2fa0db07b4d062 172.17.42.1 0 false 172.17.0.8 16 0 02:42:ac:11:00:08 17f8cc9318b27eac597aafdd3277209bb71b6b26bed7bae5ce684eddf14f9f6f map[] map[8080/tcp:[{0.0.0.0 8080}]] /var/run/docker/netns/2cfd1be49d4e [] []}

[root@localhost ~]# docker inspect --format '{{.NetworkSettings}}' tomcatB

{ ebb88aaba5eb3fecd1da103269fbb91ea52f5e07eecb9cc6823d2305a152dde6 172.17.42.1 0 false 172.17.0.6 16 0 02:42:ac:11:00:06 17f8cc9318b27eac597aafdd3277209bb71b6b26bed7bae5ce684eddf14f9f6f map[] map[8080/tcp:[{0.0.0.0 8081}]] /var/run/docker/netns/f34a7eb231d6 [] []}

#docker run 的--ip 指定容器IP的选项,需要在用户自建的docker网络才能生效,此处我用的是默认的docker网络,因此无法指定相应的容器IP。

[root@localhost ~]# vim /etc/nginx/nginx.conf

#在http配置段中添加下述配置

        upstream tomcat {

                server 172.17.0.8:8080 max_fails=1 fail_timeout=3s;

                server 172.17.0.6:8080 max_fails=1 fail_timeout=3s;

        }

        server {

                listen 80;

                server_name test.tomcat.com

                location / {

                        proxy_pass http://tomcat;

                        proxy_set_header Host $host;

                        proxy_set_header X-Real-IP $remote_addr;

                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                }

        }

#配置防火墙放开80端口的访问,注意docker需要系统防火墙来配置相应的转发规则,所以不能把防火墙停用。

[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent

[root@localhost ~]# firewall-cmd --reload

[root@localhost ~]# firewall-cmd --list-all

#启动nginx

[root@localhost ~]# service nginx configtest

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@localhost ~]# service nginx start

Starting nginx: [ OK ]

测试访问页面:


image.png
image.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,324评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,356评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,328评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,147评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,160评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,115评论 1 296
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,025评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,867评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,307评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,528评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,688评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,409评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,001评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,657评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,811评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,685评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,573评论 2 353

推荐阅读更多精彩内容