Apache+Tomcat構建Tomcat負載均衡集羣

1、環境介紹
html

2、安裝後端服務器前端

3、安裝前端Apache服務java

4、配置Apache使用mod_jk模塊實現代理及負載均衡mysql

5、配置Apache基於mod_proxy模塊實現代理及負載均衡linux

6、論壇安裝c++

7、安裝並配置Keepalived實現前端高可用web


1、環境介紹redis

123536836.gif

系統版本:CentOS 6.4_x86_64 算法

Mysql版本:mysql-5.1.66-2.el6_3.x86_64 sql

Apache版本:httpd-2.2.25 點此下載

Keepalived版本:keepalived-1.2.7 點此下載

Tomcat版本:apache-tomcat-7.0.33 點此下載

Jdk版本: jdk-7u40-linux-x64 點此下載

Tomcat-commectors版本:tomcat-connectors-1.2.37 點此下載

論壇版本:JspRun!_6.0.0_GBK 點此下載

環境介紹:

當用戶經過 "www.allen.com" 域名訪問時,首先是由前端兩臺Apache代理服務器響應並轉發到後端Tomcat服務器上,而在Apache上作的是反向代理負載均衡到後端Tomcat服務器上,前端服務器使用Keepalived作的高可用集羣,【Apache1默認爲Master,Apache2爲Backup】虛擬IP地址爲"172.16.14.10"模擬爲公網IP;固然這裏數據庫只有一臺主機會成爲點故障,這裏不要介紹Mysql的高可用,若是有興趣能夠看前面寫的博客

提示:這裏使用Keepalived作高可用時,前面全部的服務都已完成後,最後實現的對前端服務器作高可用


2、安裝後端Tomcat與Mysql服務器

一、在Tomcat1與Tomcat2服務器上安裝Tomcat,安裝方法相同,這裏只介紹一次

安裝JDK
# rpm -ivh jdk-7u40-linux-x64.rpm
# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/latest
export PATH=$JAVA_HOME/bin:$PATH
# . /etc/profile.d/java.sh
# java -version
java version "1.7.0_40"
Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)
------------------------------------------------------------------------
安裝Tomcat
# tar xf apache-tomcat-7.0.33.tar.gz -C /usr/local/
# cd /usr/local/
# ln -s apache-tomcat-7.0.33 tomcat
# vim /etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
# . /etc/profile.d/tomcat.sh
# catalina.sh version
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/latest
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/7.0.33
Server built:   Nov 18 2012 04:15:21
Server number:  7.0.33.0
OS Name:        Linux
OS Version:     2.6.32-358.el6.x86_64
Architecture:   amd64
JVM Version:    1.7.0_40-b43
JVM Vendor:     Oracle Corporation

二、啓動Tomcat服務並訪問測試,默認訪問端口爲"8080";以下

# catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/latest
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
# jps
2283 Jps
2272 Bootstrap
# ss -tanlp | grep 8080
LISTEN     0      100       :::8080        :::*      users:(("java",2272,40))

135156267.gif

三、開啓Tomcat管理及狀態頁面

# vim /usr/local/tomcat/conf/tomcat-users.xml
  <role rolename="manager-gui"/>
  <role rolename="admin-gui"/>
  <user username="allen" password="admin" roles="manager-gui,admin-gui"/>
# catalina.sh stop
# catalina.sh start

四、訪問Tomcat後臺管理

200201168.gif

200214252.gif

五、安裝Mysql服務器

######安裝Mysql並建立數據庫,受權用戶訪問新建立的數據庫有全部權,方便後面使用
[root@Mysql ~]# yum -y install mysql-server
[root@Mysql ~]# service mysqld start
[root@Mysql ~]# mysql
mysql> create database bbs;
mysql> grant all on bbs.* to 'bbsuser'@'172.16.%.%' identified by 'bbspass';
mysql> flush privileges;
----------------------------------------------------------------------
遠程鏈接數據庫測試受權用戶是否能鏈接成功
# mysql -ubbsuser -pbbspass -h 172.16.14.5 -e 'show databases';
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bbs                |
| test               |
+--------------------+

3、安裝前端Apache服務

一、在Apache1與Apache2服務器上安裝Httpd軟件,安裝方法相同,這裏只介紹一次

# tar xf httpd-2.2.25.tar.bz2
# cd httpd-2.2.25
# yum -y install gcc gcc-c++ openssl-devel pcre-devel
# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-modules=most --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --enable-mpms-shared=all --with-mpm=prefork --enable-proxy --enable-proxy-http --enable-proxy-ajp --enable-proxy-balancer --enable-cache --enable-cache-disk --enable-mem-cache --enable-file-cache --enable-proxy-connect
# make && make install
# cp build/rpm/httpd.init /etc/init.d/httpd
------修改以下內容
# vim /etc/init.d/httpd
httpd=${HTTPD-/usr/local/apache/bin/httpd}
pidfile=${PIDFILE-/usr/local/apache/logs/${prog}.pid}
lockfile=${LOCKFILE-/var/lock/subsys/${prog}}
RETVAL=0
# check for 1.3 configuration
check13 () {
        CONFFILE=/etc/httpd/httpd.conf
----------------------------------------------------------------------
修改環境變量並把Httpd加入到系統服務
# echo "PATH=/usr/local/apache/bin:$PATH" >> /etc/profile.d/http.sh
# . /etc/profile.d/http.sh
# ln -s /usr/local/apache/include/ /usr/include/httpd
# chkconfig --add httpd
----------------------------------------------------------------------
建立測試頁
# echo "<h1>APACHE</h1>" > /usr/local/apache/htdocs/index.html

二、在Apache1與Apache2服務器上安裝"tomcat-connectors"

# tar xf tomcat-connectors-1.2.37-src.tar.gz
# cd tomcat-connectors-1.2.37-src/native/
# ./configure --with-apxs=/usr/local/apache/bin/apxs
# make && make install
------------------------------------------------------------------------
安裝完成後會生成以下文件
# ls /usr/local/apache/modules/
  mod_jk.so
------------------------------------------------------------------------
啓動Httpd服務
#service httpd start
# ss -tanl |grep 80
LISTEN     0      128              :::80               :::*

三、訪問測試Apache是否正常工做

144627161.gif


4、配置Apache使用mod_jk模塊實現代理及負載均衡

一、修改Apache主配置文件,包含一個文件並建立該文件;實現代理功能

將下面指令添加到文件末尾便可
# vim /etc/httpd/httpd.conf
Include /etc/httpd/extra/mod_jk.conf
------------------------------------------------------------------------
建立文件添加以下內容
# vim /etc/httpd/extra/mod_jk.conf
LoadModule  jk_module  modules/mod_jk.so
JkWorkersFile  /etc/httpd/extra/workers.properties
JkLogFile  logs/mod_jk.log
JkLogLevel  debug
JkMount  /*  TomcatA
JkMount  /status/  stat1
------------------------------------------------------------------------
# vim /etc/httpd/extra/workers.properties
worker.list=TomcatA,stat1
worker.TomcatA.port=8009
worker.TomcatA.host=172.16.14.3
worker.TomcatA.type=ajp13
worker.TomcatA.lbfactor=1
worker.stat1.type = status
----------------------------------------------------------------------
# service httpd restart

二、修改後端Tomcat1服務器,添加一個虛擬主機並提供測試頁面

[root@Tomcat1 ~]# cd /usr/local/tomcat/conf/
[root@Tomcat1 conf]# cp server.xml server.xml.bak
[root@Tomcat1 conf]# vim server.xml
######修改以下內容
<Engine name="Catalina" defaultHost="www.allen.com" jvmRoute="TomcatA">
######在"Engine"中添加以下內容
<Host name="www.allen.com"  appBase="/web/allen"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="allen_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        <Context path="" docBase="/web/allen" />
      </Host>
----------------------------------------------------------------------
######建立網站存放目錄並建立測試頁
[root@Tomcat1 ~]# mkdir -p /web/allen
[root@Tomcat1 ~]# vim /web/allen/index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="red">TomcatA </font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("abc","abc"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>
----------------------------------------------------------------------
######重啓Tomcat服務並查看是否啓動成功
[root@Tomcat1 ~]# catalina.sh stop
[root@Tomcat1 ~]# catalina.sh configtest
[root@Tomcat1 ~]# catalina.sh start
[root@Tomcat1 ~]# ss -tanlp | grep java
LISTEN     0      100                      :::8080                    :::*      users:(("java",6323,40))
LISTEN     0      1          ::ffff:127.0.0.1:8005                    :::*      users:(("java",6323,52))
LISTEN     0      100                      :::8009                    :::*      users:(("java",6323,41))

三、訪問Apache1主機,驗證是否代理成功

153428423.gif

四、修改Apache配置文件(mod_jk.conf、workers.properties);實現負載均衡

# vim /etc/httpd/extra/mod_jk.conf
LoadModule  jk_module  modules/mod_jk.so
JkWorkersFile  /etc/httpd/extra/workers.properties
JkLogFile  logs/mod_jk.log
JkLogLevel  debug
JkMount  /*  lbcluster
JkMount  /status/  stat1
------------------------------------------------------------------------
# vim /etc/httpd/extra/workers.properties
worker.list=lbcluster,stat1
worker.TomcatA.port=8009
worker.TomcatA.host=172.16.14.3
worker.TomcatA.type=ajp13
worker.TomcatA.lbfactor=1
worker.TomcatB.port = 8009
worker.TomcatB.host=172.16.14.4
worker.TomcatB.type = ajp13
worker.TomcatB.lbfactor = 1
worker.lbcluster.type = lb
worker.lbcluster.sticky_session = 0  #取值爲{1|0}1將用戶session與後端服務器綁定,0爲不綁定,若是支持session複製或session共享能夠設置爲0
worker.lbcluster.balance_workers = TomcatA, TomcatB
worker.stat1.type = status
----------------------------------------------------------------------
# service httpd reload

五、修改後端Tomcat2服務器,添加一個虛擬主機並提供測試頁面

[root@Tomcat2 ~]# cd /usr/local/tomcat/conf/
[root@Tomcat2 conf]# cp server.xml server.xml.bak
[root@Tomcat2 conf]# vim server.xml
######修改以下內容
<Engine name="Catalina" defaultHost="www.allen.com" jvmRoute="TomcatB">
######在"Engine"中添加以下內容
<Host name="www.allen.com"  appBase="/web/allen"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="allen_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        <Context path="" docBase="/web/allen" />
      </Host>
----------------------------------------------------------------------
######建立網站存放目錄並建立測試頁
[root@Tomcat2 ~]# mkdir -p /web/allen
[root@Tomcat2 ~]# vim /web/allen/index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatB</title></head>
  <body>
    <h1><font color="blue">TomcatB </font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("abc","abc"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>
----------------------------------------------------------------------
######重啓Tomcat服務並查看是否啓動成功
[root@Tomcat2 ~]# catalina.sh stop
[root@Tomcat2 ~]# catalina.sh configtest
[root@Tomcat2 ~]# catalina.sh start
[root@Tomcat2 ~]# ss -tanlp | grep java
LISTEN     0      100                      :::8080                    :::*      users:(("java",6323,40))
LISTEN     0      1          ::ffff:127.0.0.1:8005                    :::*      users:(("java",6323,52))
LISTEN     0      100                      :::8009                    :::*      users:(("java",6323,41))

六、訪問Apache服務器,測試是否實現負載均衡

160608952.gif

160611630.gif

由上可見,基於Apache的mod_jk模塊已成功實現負載均衡功能


5、配置Apache基於mod_proxy模塊實現代理及負載均衡

一、修改Apache的主配置文件,包含一個文件並建立該文件;實現代理功能

# vim /etc/httpd/httpd.conf
#Include /etc/httpd/extra/mod_jk.conf    #註釋此行
Include /etc/httpd/extra/mod_proxy.conf
------------------------------------------------------------------------
# vim /etc/httpd/extra/mod_proxy.conf
ProxyVia On
ProxyRequests Off
ProxyPreserveHost Off
<Proxy *>
  Order allow,deny
  Allow from all
</Proxy>
  ProxyPass  /  ajp://172.16.14.3:8009/
  ProxyPa***everse  /  ajp://172.16.14.3:8009/
<Location  / >
  Order allow,deny
  Allow from all
</Location>
----------------------------------------------------------------------
# service httpd reload
======================================================================
註釋:
  ProxyPass  /  ajp://172.16.14.3:8009/    #使用的是ajp協議
  ProxyPa***everse  /  ajp://172.16.14.3:8009/
能夠更改成使用http協議;以下
  ProxyPass  /  http://172.16.14.3:8080/
  ProxyPa***everse  /  http://172.16.14.3:8080/
######這裏只演示使用ajp協議,若是是Apache與Tomcat結合建議使用ajp協議

二、訪問Apache1服務器,測試是否代理成功

163026801.gif

三、修改Apache配置文件(mod_proxy.conf);實現負載均衡

# vim /etc/httpd/extra/mod_proxy.conf
ProxyVia Off
ProxyRequests Off
ProxyPreserveHost Off
<Proxy balancer://allen>
  BalancerMember ajp://172.16.14.3:8009 loadfactor=1
  BalancerMember ajp://172.16.14.4:8009 loadfactor=1
  ProxySet lbmethod=bytraffic
</Proxy>
<Location /allen>    #設置狀態及管理頁面
  SetHandler balancer-manager
  Proxypass !
  Order allow,deny
  Allow from all
</Location>
<Proxy *>
  Order allow,deny
  Allow from all
</Proxy>
  ProxyPass  /  balancer://allen/    stickysession=JSESSIONID
  ProxyPa***everse  /  balancer://allen/
<Location  / >
  Order allow,deny
  Allow from all
</Location>
----------------------------------------------------------------------
註釋:
  BalancerMember ajp://172.16.14.3:8009 loadfactor=1  #使用ajp協議
  BalancerMember ajp://172.16.14.4:8009 loadfactor=1
可更改成使用http協議
  BalancerMember http://172.16.14.3:8080 loadfactor=1
  BalancerMember http://172.16.14.4:8080 loadfactor=1
######這裏只介紹使用ajp協議,若是有興趣能夠更改一下;loadfactor:權重
----------------------------------------------------------------------
ProxySet lbmethod=bytraffic    #設置調度算法
byrequests:即基於權重將統計請求個數進行調度(默認)
bytraffic:則執行基於權重的流量計數調度
bybusyness:經過考量每一個後端服務器的當前負載進行調度
----------------------------------------------------------------------
# service httpd reload

四、訪問Apache服務器,測試基於mod_proxy模塊是否實現負載均衡

164809362.gif

164821202.gif

五、訪問狀態頁面

174702882.gif


6、論壇安裝

一、在Tomcat1服務器上安裝

# unzip JspRun\!_6.0.0_GBK.zip
# mv /web/allen/index.jsp /web/allen/test.jsp
# cp -r upload/* /web/allen/
------------------------------------------------------------------------
修改論壇數據庫鏈接文件
# vim /web/allen/config.properties
dbhost = 172.16.14.5
dbport=3306
dbuser = bbsuser
dbpw = bbspass
dbname = bbs
------------------------------------------------------------------------
# catalina.sh stop
# catalina.sh start

二、安裝論壇程序

181112824.gif

三、點擊下一步-->我贊成-->下一步,進入數據庫配置頁面

181241966.gif

四、點下一步-->(填寫管理員密碼)下一步-->進入建立數據表階段-->而後點103517659.gif-->進入首頁

181509214.gif

五、將論壇程序拷貝到Tomcat2服務器上一份並訪問測試

# mv /web/allen/index.jsp /web/allen/test.jsp    #把原來的測試頁重命名
------------------------------------------------------------------------
# scp -r 172.16.14.3:/web/allen/* /web/allen/
# catalina.sh stop
# catalina.sh start

182314200.gif

六、訪問Apache服務器,測試是否能正常訪問論壇程序,這裏就不在作訪問測試


7、安裝並配置Keepalived

一、將Apache1服務器的Httpd配置文件複製到Apache2服務器

[root@Apache1 ~]# cd /etc/httpd/
[root@Apache1 httpd]# scp httpd.conf 172.16.14.2:/etc/httpd/
[root@Apache1 httpd]# cd extra/
[root@Apache1 extra]# scp mod_jk.conf mod_proxy.conf workers.properties 172.16.14.2:/etc/httpd/extra/

二、在Apache1與Apache2服務器上安裝Keepalived;這裏使用rpm包安裝,光盤映像中有

[root@Apache1 ~]# yum -y install keepalived
--------------------------------------------
[root@Apache2 ~]# yum -y install keepalived

三、配置Apache1服務器上的Keepalived

[root@Apache1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
   root@localhost
   }
   notification_email_from admin@allen.com
   smtp_server 172.16.0.0
   smtp_connect_timeout 30
   router_id LVS_ALLEN
}
vrrp_script chk_httpd {
    script "killall -0 httpd"
    interval 1
    weight -2
}
vrrp_instance httpd_1 {
    state MASTER
    interface eth0
    virtual_router_id 58
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1058
    }
    virtual_ipaddress {
    172.16.14.10
    }
    track_script {
    chk_httpd
    }
}
[root@Apache1 ~]# service keepalived start
[root@Apache1 ~]# chkconfig keepalived on
[root@Apache1 ~]# chkconfig --list keepalived
keepalived      0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@Apache1 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2c:1a:24 brd ff:ff:ff:ff:ff:ff
    inet 172.16.14.1/16 brd 172.16.255.255 scope global eth0
    inet 172.16.14.10/32 scope global eth0
    inet6 fe80::20c:29ff:fe2c:1a24/64 scope link
       valid_lft forever preferred_lft forever

四、配置Apache2服務器上的Keepalived

[root@Apache2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
   root@localhost
   }
   notification_email_from admin@allen.com
   smtp_server 172.16.0.0
   smtp_connect_timeout 30
   router_id LVS_ALLEN
}
vrrp_script chk_httpd {
    script "killall -0 httpd"
    interval 1
    weight -2
}
vrrp_instance httpd_1 {
    state BACKUP
    interface eth0
    virtual_router_id 58
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1058
    }
    virtual_ipaddress {
    172.16.14.10
    }
    track_script {
    chk_httpd
    }
}
[root@Apache2 ~]# service keepalived start
[root@Apache2 ~]# chkconfig --list keepalived
keepalived      0:off   1:off   2:on    3:on    4:on    5:on    6:off

五、使用"www.allen.com" 域名訪問,驗證是否能訪問到論壇;因爲沒有DNS服務器解析,修改了"hosts"文件;如:

######添加以下內容解析
C:\Windows\System32\drivers\etc\hosts
172.16.14.10    www.allen.com

194112861.gif

六、模擬前端一臺服務器出現故障;查看虛擬IP是否轉移

######中止Apache1服務器上的Httpd服務來模擬故障;並查看IP
[root@Apache1 ~]# service httpd stop
[root@Apache1 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2c:1a:24 brd ff:ff:ff:ff:ff:ff
    inet 172.16.14.1/16 brd 172.16.255.255 scope global eth0
    inet6 fe80::20c:29ff:fe2c:1a24/64 scope link
       valid_lft forever preferred_lft forever
========================================================================
######查看Apache2服務器上的IP
[root@Apache2 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:ec:f6:3f brd ff:ff:ff:ff:ff:ff
    inet 172.16.14.2/16 brd 172.16.255.255 scope global eth0
    inet 172.16.14.10/32 scope global eth0
    inet6 fe80::20c:29ff:feec:f63f/64 scope link
       valid_lft forever preferred_lft forever

七、從上一步能夠看出,虛擬IP地址已成功轉移,說明還能正常提供服務,能夠再次訪問測試;若是服務器修復好從新上線,虛擬IP還會轉移回去;這裏就再也不測試


到此,基於Apache作反向代理實現了Tomcat的負載均衡;而Keepalived實現了前端服務器的高可用;關於session保存的問題,方案有不少,好比能夠保存到"memcached"、"redis"等,使用哪一種方案具體還要根據本身的需求而定...

相關文章
相關標籤/搜索