Apache + Tomcat基於mod_jk模組實現反代、動靜分離、熱備和負載均衡
1、簡單說明:
mod_jk是ASF的一個專案,是一個工作於Apache端基於AJP協議與Tomcat通訊的聯結器,是Apache的一個模組,是AJP協議的客戶端,服務端是Tomcat的AJP聯結器。
Web客戶訪問Tomcat JSP元件的兩種方式:
2、演示環境:
IP |
作業系統 |
部署程式 |
192.168.1.143 |
CentOS 7.6 |
Apache |
192.168.1.144 |
CentOS 7.6 |
Tomcat |
192.168.1.145 |
CentOS 7.6 |
Tomcat |
3、192.168.1.144節點禁用HTTP/1.1聯結器:
# cd /usr/local/tomcat/conf
# cp server.xml server.xml.bak
# vim server.xml,註釋如下程式碼:
<!-- <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> -->
4、192.168.1.145節點禁用HTTP/1.1聯結器:
# cd /usr/local/tomcat/conf
# cp server.xml server.xml.bak
# vim server.xml,註釋如下程式碼:
<!-- <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> -->
5、192.168.1.144節點新增jvmRoute引數:# vim server.xml
<Engine name="Catalina" defaultHost="localhost">修改為
<Engine name="Catalina" defaultHost="localhost"
6、192.168.1.145節點新增jvmRoute引數:# vim server.xml
<Engine name="Catalina" defaultHost="localhost">修改為
<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatB">
7、192.168.1.144節點建立測試頁:
# vim /usr/local/tomcat/webapps/ROOT/test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA</font></h1>
<table border="1">
<tr>
<td>Session ID</td>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created ON</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
8、192.168.1.145節點建立測試頁:
# vim /usr/local/tomcat/webapps/ROOT/test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="blue">TomcatB</font></h1>
<table border="1">
<tr>
<td>Session ID</td>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created ON</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
9、分別啟動192.168.1.144節點和192.168.1.145節點上的Tomcat:
# catalina.sh stop # catalina.sh configtest # catalina.sh start # ss -tunlp | grep -w :8009
10、192.168.1.143節點安裝Apache:
# yum -y install httpd
# cd /etc/httpd/conf
# cp httpd.conf httpd.conf.bak
# vim httpd.conf,新增程式碼:ServerName localhost:80
# httpd -t # systemctl start httpd.service # ss -tunlp | grep -w :80
11、192.168.1.143節點配置status狀態頁面:
# vim /etc/httpd/conf/httpd.conf,末尾新增如下程式碼:
<Location /server-status>
SetHandler server-status
Require all granted
</Location>
# httpd -t # systemctl reload httpd.service # ss -tunlp | grep -w :80
本地瀏覽器訪問:
192.168.1.143
192.168.1.143/server-status
12、192.168.1.143編譯安裝tomcat-connectors(下載地址http://tomcat.apache.org/download-connectors.cgi):
# yum -y install gcc gcc-c++ httpd-devel
# ls /etc/httpd/modules | grep mod_jk.so //預設沒有此模組
# tar -xf tomcat-connectors-1.2.46-src.tar.gz -C /usr/src
# cd /usr/src/tomcat-connectors-1.2.46-src/native
# which apxs --> /usr/bin/apxs
# ./configure --with-apxs=/usr/bin/apxs
# make && make install
# ls /etc/httpd/modules | grep mod_jk.so
13、192.168.1.143配置mod_jk模組,實現TomcatB為TomcatA的Hot Standby熱備
(1)建立httpd-jk.conf配置檔案:# vim /etc/httpd/conf.d/mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf.d/workers.properties
JkMountFile conf.d/uriworkermap.properties
JkLogFile logs/mod_jk.log
JkLogLevel info
JkShmFile logs/mod_jk.shm
JkMount /* lbcluster
JkMount /jk-status jk-status
備註:常用指令說明
Ø JkWorkersFile conf.d/workers.properties:JkWorkersFile用於定義worker例項的工作屬性的配置檔案
Ø JkLogFile logs/mod_jk.log:JkLogFile用於指定mod_jk模組的日誌檔案
Ø JkLogLevel info:JkLogLevel用於指定日誌的級別
Ø JkMount /* lbcluster:JkMount用於控制URL與worker例項的對應關係
(2)建立workers.properties配置檔案:# vim /etc/httpd/conf.d/workers.properties
worker.list=lbcluster,jk-status
worker.lbcluster.balance_workers=TomcatA,TomcatB
worker.TomcatA.type=ajp13
worker.TomcatA.host=192.168.1.144
worker.TomcatA.port=8009
worker.TomcatA.lbfactor=1
worker.TomcatB.type=ajp13
worker.TomcatB.host=192.168.1.145
worker.TomcatB.port=8009
worker.TomcatB.lbfactor=1
worker.lbcluster.type=lb
worker.jk-status.type=status
worker.TomcatA.redirect=TomcatB
worker.TomcatB.activation=disabled
worker.lbcluster.sticky_session=0
備註:常用指令說明
Ø worker.TomcatA.lbfactor=1:lbfactor負載均衡因數,數值越大,分配到此節點的請求越多
Ø worker.TomcatA.type=ajp13:根據工作機制的不同,worker有多種不同型別的type,這是需要為每個worker定義的一項屬性。常見的type型別如下:
² ajp13:表示當前worker為一個執行著的Tomcat例項
² lb:即load balancing,專用於負載均衡場景中的worker,此worker並不真正負責處理使用者請求,而是將使用者請求排程給其它型別為ajp13的worker
² status:顯示分散式環境中各實際worker工作狀態的特殊worker,它不處理任何請求,也不關聯到任何實際工作的worker例項
(3)建立uriworkermap.properties配置檔案:# vim /etc/httpd/conf.d/uriworkermap.properties
/*=lbcluster
/jk-status=jk-status
!/*.html=lbcluster
!/*.htm=*
!/*.gif=*
!/*.jpg=*
!/*.jpeg=*
!/*.png=*
!/*.bmp=*
!/*.ico=*
!/*.mp3=*
!/*.mp4=*
!/*.mid=*
!/*.wav=*
!/*.flv=*
!/*.swf=*
!/*.rar=*
!/*.zip=*
!/*.txt=*
!/*.md=*
!/*.doc=*
!/*.docx=*
!/*.xls=*
!/*.xlsx=*
!/*.js=*
!/*.css=*
!/*.exe=*
!/*.msi=*
備註:常用指令說明
Ø /*=lbcluster:所有請求都轉發給lbcluster負載均衡控制器處理
Ø /jk-status=jk-status:包含jk-status的請求交由jk-status這個worker處理
Ø !/*.html=lbcluster:所有以.html格式結尾的請求不交由lbcluster處理
Ø !/*.htm=*:所有以.htm格式結尾的請求不交由任何worker例項處理
(4)192.168.1.143節點建立用於測試的靜態頁面:
# echo "<h3>/var/www/html/a.html in 192.168.1.143</h3>" > /var/www/html/a.html
# echo "<h3>/var/www/html/b.htm in 192.168.1.143</h3>" > /var/www/html/b.htm
(5)192.168.1.143節點配置jk-status訪問控制:
# vim /etc/httpd/conf/httpd.conf,末尾新增如下程式碼:
<Location /jk-status>
JkMount jk-status
Options MultiViews
AuthType Basic
AuthName "Marion"
AuthUserFile conf.d/.htpasswd
require valid-user granted
</Location>
# htpasswd -c /etc/httpd/conf.d/.htpasswd marion
# httpd -t # systemctl reload httpd.service # ss -tunlp | grep -w :80
本地火狐瀏覽器訪問(效果比Google Chrome瀏覽器明顯):http://192.168.1.143/jk-status
備註:上述頁面可以啟動或者停止叢集中的伺服器,也可以修改負載均衡器的配置
(6)本地瀏覽器驗證熱備效果:
192.168.1.143
192.168.1.143/server-status
192.168.1.143/a.html
192.168.1.143/b.htm
192.168.1.143/test.jsp,無論怎麼重新整理頁面,顯示的都是TomcatA,且Session ID保持不變
http://192.168.1.143/jk-status
停止192.168.1.144節點的TomcatA,無論怎麼重新整理頁面,顯示的都是TomcatB,且Session ID保持不變
192.168.1.143/test.jsp
http://192.168.1.143/jk-status
恢復192.168.1.144節點的TomcatA,無論怎麼重新整理頁面,顯示的都是TomcatA,且Session ID保持不變
192.168.1.143/test.jsp
http://192.168.1.143/jk-status
備註:熱備場景中只有在TomcatA不可用的情況下才會向TomcatB發起請求,一旦TomcatA恢復正常,TomcatB就立即停止處理請求
14、192.168.1.143配置mod_jk模組,實現TomcatA、TomcatB基於實際流量大小進行負載均衡
(1)httpd-jk.conf和uriworkermap.properties配置檔案內容保持不變
(2)修改workers.properties配置檔案:# vim /etc/httpd/conf.d/workers.properties
刪除如下2行:
worker.TomcatA.redirect=TomcatB
worker.TomcatB.activation=disabled
替換為:
worker.lbcluster.sticky_session_force=0
worker.lbcluster.method=T
備註:常用指令說明
Ø worker.lbcluster.sticky_session=0:預設值為1 | true,表示是否啟用session會話保持,如果後端各worker例項之間支援session複製,則可以將此屬性值設為0
Ø worker.lbcluster.sticky_session_force=0:預設值為0 | false,如果叢集中某臺伺服器多次請求沒有響應,將轉發當前的請求到其它伺服器上處理,worker.lbcluster.sticky_session_force=1只有在worker.lbcluster.sticky_session=1時才生效
Ø worker.lbcluster.method=R:常用方法為R、T或B,預設為R[equest],即根據請求個數進行排程;T[raffic]表示根據已經發送給worker的實際流量大小進行排程;B[usyness]表示根據實際負載情況進行排程
# httpd -t # systemctl reload httpd.service # ss -tunlp | grep -w :80
(3)本地瀏覽器驗證負載均衡效果:
192.168.1.143/test.jsp
重新整理頁面,輪詢顯示,且Session ID一直在變:
192.168.1.143/jk-status
15、192.168.1.143配置mod_jk模組,實現TomcatA、TomcatB負載均衡和session會話保持
(1)httpd-jk.conf和uriworkermap.properties配置檔案內容保持不變
(2)修改workers.properties配置檔案:# vim /etc/httpd/conf.d/workers.properties
刪除如下2行:
worker.lbcluster.method=T
worker.lbcluster.sticky_session=0
替換為:
worker.lbcluster.sticky_session=1
# httpd -t # systemctl reload httpd.service # ss -tunlp | grep -w :80
(3)本地瀏覽器驗證負載均衡和session會話保持效果:
192.168.1.143/test.jsp,無論怎麼重新整理頁面,頁面內容和Session ID均不會改變:
192.168.1.143/jk-status
# vim /etc/httpd/conf.d/workers.properties,目前配置如下所示:
worker.lbcluster.sticky_session=1
worker.lbcluster.sticky_session_force=0
停止192.168.1.145節點的TomcatB,無論怎麼重新整理頁面,顯示的都是TomcatA,且Session ID保持不變
192.168.1.143/test.jsp
192.168.1.143/jk-status
恢復192.168.1.145節點的TomcatB,無論怎麼重新整理頁面,顯示的還是TomcatA,且Session ID保持不變
192.168.1.143/test.jsp
192.168.1.143/jk-status
# vim /etc/httpd/conf.d/workers.properties,修改為如下配置:
worker.lbcluster.sticky_session=1
worker.lbcluster.sticky_session_force=1
192.168.1.143/test.jsp,無論怎麼重新整理頁面,頁面內容和Session ID都保持不變:
192.168.1.143/jk-status
停止192.168.1.144節點的TomcatA,無論怎麼重新整理頁面,提示的都是503錯誤
192.168.1.143/test.jsp
192.168.1.143/jk-status
恢復192.168.1.144節點的TomcatA,無論怎麼重新整理頁面,顯示的還是TomcatA,且Session ID保持不變
192.168.1.143/test.jsp
192.168.1.143/jk-status