【高可用HA】Apache (4) —— Mac下配置Apache Httpd負載均衡(Load Balancer)之mod_jk

Richaaaard發表於2015-12-09

Mac下配置Apache Httpd負載均衡(Load Balancer)之mod_jk


httpd版本: httpd-2.4.17
jk版本: tomcat-connectors-1.2.41

參考來源:

Apache (1) —— Mac下安裝Apache Httpd到自定義路徑(非/etc/apache2)

Apache (2) —— Mac 下安裝多個Apache Tomcat例項

Working with mod_jk

How to Compile Tomcat mod_jk connector on Mac OS X Mavericks

Installing mod_jk for Apache 2.2 on Mac OS X 10.5 Leopard

Apache負載均衡配置

Apache學習之二、HTTPD的負載均衡

Apache配置反向代理、負載均衡和叢集(mod_proxy方式)

準備

首先我們參照下面兩篇文章配置好httpd和兩個Tomcat例項

其中Tomcat的兩個例項node-a和node-b分別存放與"./servers/cluster/tomcat/node-a"和"/servers/cluster/tomcat/node-b"下

對應的配置分別為:

  • Server Port: 8015 與 8025
  • Connector: 8081 與 8082
  • AJP: 8019 與 8029

另分別為連個例項新增jvmRoute的配置

  • node-a

      <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
  • node-b

      <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
注意:不要忘記修改供測試的頁面特徵

配置httpd負載均衡(Load Balancer)

httpd負載均衡Tomcat的方式主要可以分為兩類

  1. httpd自帶的mod_proxy
  2. Tomcat提供了專門的JK外掛來負責Tomcat和HTTP伺服器的通訊mod_jk
*注意 本篇文章主要關注第二類配置

首先編譯

在Apache的網站上下載tomcat-connectors-1.2.41,然後解壓。

執行命令編譯

$ cd native
$ ./configure CFLAGS='-arch x86_64' APXSLDFLAGS='-arch x86_64' --with-apxs=/usr/sbin/apxs
$ make
$ sudo make install
*注意 --with-apxs需要指向目標節點的apxs

此處我本地的路徑為

/Users/Richard/Documents/Dev/servers/cluster/httpd/node-a/bin/apxs

如果執行正常不出錯,末尾幾行的輸出為

...
config.status: creating common/list.mk
config.status: creating common/jk_types.h
config.status: creating common/config.h
config.status: executing depfiles commands
config.status: executing libtool commands

然後"./native"下執行make

期間會出現warning,可以暫時忽略不影響

jk_lb_worker.c:1397:36: warning: address of array
  'p->worker->session_cookie_path' will always evaluate to 'true'
  [-Wpointer-bool-conversion]
                if (p->worker->session_cookie_path && *p->worker->se...
                    ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~ ~~

make執行正常的最後幾行輸出為

...
Making all in common
make[1]: Nothing to be done for `all'.
Making all in apache-2.0
make[1]: Nothing to be done for `all'.

make install如果在非sudo狀態下,最後會出現錯誤

...
install: /Users/Richard/Documents/Dev/servers/cluster/httpd/node-a/modules/mod_jk.so: Permission denied
apxs:Error: Command failed with rc=4653056
.
make[1]: *** [install_dynamic] Error 1
make: *** [install-recursive] Error 1

sudo執行make install

...
libtool: install: ranlib /Users/Richard/Documents/Dev/servers/cluster/httpd/node-a/modules/mod_jk.a
chmod 755 /Users/Richard/Documents/Dev/servers/cluster/httpd/node-a/modules/mod_jk.so

Please be sure to arrange /Users/Richard/Documents/Dev/servers/cluster/httpd/node-a/conf/httpd.conf...

make[2]: Nothing to be done for `install-exec-am'.
make[2]: Nothing to be done for `install-data-am'.

配置mod_jk

在node-a下新建資料夾"other",並建立檔案workers.properties

# Define two status worker:  
# - jk-status for read-only use  
# - jk-manager for read/write use  
worker.list=jk-status  
worker.jk-status.type=status  
worker.jk-status.read_only=true  

worker.list=jk-manager  
worker.jk-manager.type=status  

# We define a load balancer worker  
# with name "balancer"  
worker.list=balancer  
worker.balancer.type=lb  
worker.balancer.error_escalation_time=0  
worker.balancer.max_reply_timeouts=10  
worker.balancer.sticky_session=true  
worker.balancer.sticky_session_force=true    

# Now we add members to the load balancer First member is "tomcat1", most attributes are inherited from the template "worker.template".  
worker.balancer.balance_workers=tomcat1  
worker.tomcat1.reference=worker.template  
worker.tomcat1.host=127.0.0.1  
worker.tomcat1.port=8019  
worker.tomcat1.activation=A  

# Second member is "tomcat2", most attributes are inherited from the template "worker.template".  
worker.balancer.balance_workers=tomcat2  
worker.tomcat2.reference=worker.template  
worker.tomcat2.host=127.0.0.1  
worker.tomcat2.port=8029  
worker.tomcat2.activation=A  

# Finally we put the parameters  
worker.template.type=ajp13  
worker.template.socket_connect_timeout=5000  
worker.template.socket_keepalive=true  
worker.template.ping_mode=A  
worker.template.ping_timeout=10000  
worker.template.connection_pool_minsize=0  
worker.template.connection_pool_timeout=600  
worker.template.reply_timeout=300000  
worker.template.recovery_options=3  
*注意上面的host配置都是127.0.0.1,會影響我們下面的測試

並同時在"./node-a/other/"下建立檔案uriworkermap.properties

/*.do=balancer  
/*.jsp=balancer  

#/*.gif=balancer  
#/*.jpg=balancer  
#/*.png=balancer  
#/*.css=balancer  
#/*.js=balancer  
#/*.htm=balancer  
#/*.html=balancer  
#/*.txt=balancer  


# Optionally filter out all .jpeg files inside that context  
# For no mapping the url has to start with exclamation (!)  

!/servlets-examples/*.jpeg=lb  

#  
# Mount jkstatus to /jkmanager  
# For production servers you will need to  
# secure the access to the /jkmanager url  
#  
/jk-manager=jk-manager  
/jk-status=jk-status  

最後

為httpd.conf,新增配置

# 載入jk配置檔案  
Include conf/mod_jk.conf 

測試

重新啟動httpd,並用瀏覽器訪問"localhost:81",提示"Service Unavailable"錯誤

【高可用HA】Apache (4) —— Mac下配置Apache Httpd負載均衡(Load Balancer)之mod_jk

檢視"./node-a/log"目錄下"mod_jk.log"檔案

[Wed Dec 09 15:00:31.403 2015] [13735:4417540096] [info] get_most_suitable_worker::jk_lb_worker.c (1119): all workers are in error state for session QUMQZVRPS1-4MX593EAOD71Y6YAFXUP1-U734UPHI-1
[Wed Dec 09 15:00:31.403 2015] [13735:4417540096] [info] service::jk_lb_worker.c (1664): All tomcat instances failed, no more workers left (attempt=1, retry=1)
[Wed Dec 09 15:00:31.403 2015] [13735:4417540096] [info] service::jk_lb_worker.c (1675): All tomcat instances are busy or in error state
[Wed Dec 09 15:00:31.403 2015] [13735:4417540096] [error] service::jk_lb_worker.c (1680): All tomcat instances failed, no more workers left
[Wed Dec 09 15:00:31.403 2015] [13735:4417540096] [info] jk_handler::mod_jk.c (2991): Service error=0 for worker=balancer

嘗試訪問

127.0.0.1:81

得到正常結果

【高可用HA】Apache (4) —— Mac下配置Apache Httpd負載均衡(Load Balancer)之mod_jk

這樣就完成一個以httpd單節點為負載均衡器分發到不同Tomcat的簡單架構。

*擴充套件

  • 如果將host改為"localhost"是否能正常訪問?
  • 與另一篇中用httpd自帶lb的方式不同對映的路徑("./test"),這裡是根目錄,需要如何配置達到相同效果?
  • 這裡測試結果可以正常顯示樣式和圖片,是什麼配置起的作用?

相關文章