【高可用HA】Apache (3) —— Mac下配置Apache Httpd负载均衡(Load Balancer)之mod_proxy

Mac下配置Apache Httpd负载均衡(Load Balancer)之mod_proxy


httpd版本: httpd-2.4.17

参考来源:

Apache (1) —— Mac下安装Apache Httpd到自定义路径(非/etc/apache2)

Apache (2) —— Mac 下安装多个Apache Tomcat实例

Apache httpd mod_proxy

Apache httpd mod_proxy_ajp

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
*注意 本篇文章主要关注第一类配置

mod_proxy的两种实现

  1. http方式
  2. ajp方式

1. mod_proxy使用http方式实现负载均衡

  • 加载mod_proxy的so库

我们在"./servers/cluster/httpd/node-a/conf/"下修改httpd.conf文件

配置http balancer
<Proxy balancer://mycluster>  
	BalancerMember http://127.0.0.1:8081 loadfactor=3  
	BalancerMember http://127.0.0.1:8082 loadfactor=3  
	ProxySet lbmethod=byrequests 
</Proxy>  
ProxyRequests Off  
ProxyPass /test balancer://mycluster/ stickysession=JSESSIONID nofailover=Off  
ProxyPassReverse /test balancer://mycluster/
开启so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
启动httpd
$ ./bin/httpd -f /Users/Richard/Documents/Dev/servers/cluster/httpd/node-a/conf/httpd.conf -k start
AH00526: Syntax error on line 515 of /Users/Richard/Documents/Dev/servers/cluster/httpd/node-a/conf/httpd.conf:
ProxySet: unknown lbmethod lbmethod=byrequests; balancer://mycluster	

在Terminal中出现错误提示"unknown lbmethod lbmethod=byrequests"

发现忘记配置lbmethod相关模块(如果只使用byrequests方式,可以按需开启以下模块)

LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so
LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so
LoadModule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so
修改后重启Tomcat

发现仍然无法正常访问,在"./node-a/log/error_log"中出现错误提示

[Tue Dec 08 17:16:13.028078 2015] [proxy_balancer:emerg] [pid 10235:tid 140735135085312] AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
[Tue Dec 08 17:16:13.028176 2015] [:emerg] [pid 10235:tid 140735135085312] AH00020: Configuration Failed, exiting

仍有模块没有配置

LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
修改后重启Tomcat

通过浏览器访问

http://localhost:81/test

刷新浏览器我们会发现下面两句话会交替出现

If you're seeing this, you've successfully installed Tomcat. Congratulations! Node - A!!

.

"If you're seeing this, you've successfully installed Tomcat. Congratulations! Node - B!!"

2. mod_proxy使用ajp方式实现负载均衡

配置ajp balancer

将之前的http配置注解掉,加入一下配置

<Proxy balancer://mycluster>  
	BalancerMember ajp://127.0.0.1:8019 loadfactor=2 route=tomcat1  
	BalancerMember ajp://127.0.0.1:8029 loadfactor=2 route=tomcat2  
ProxySet lbmethod=byrequests  
</Proxy>  
ProxyRequests Off  
ProxyPass /test balancer://mycluster/ stickysession=JSESSIONID nofailover=Off  
ProxyPassReverse /test balancer://mycluster/   

需要开启ajp相关模块

LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
重启Tomcat

用浏览器访问出现错误

查看error_log

[Wed Dec 09 08:45:34.824060 2015] [proxy:warn] [pid 10582:tid 4338487296] [client ::1:54735] 
AH01144: No protocol handler was valid for the URL /test. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule.	

这是因为ajp的底层是依赖TCP的。

查看httpd Apache官网上关于mod_proxy_ajp的相关说明

The AJP13 protocol is packet-oriented. A binary format was presumably chosen over the more readable plain text for reasons of performance. The web server communicates with the servlet container over TCP connections. To cut down on the expensive process of socket creation, the web server will attempt to maintain persistent TCP connections to the servlet container, and to reuse a connection for multiple request/response cycles.

最后我们为这个httpd的Load Balancer配置一个管理器——Load Balancer Manager

在httpd.conf中,增加配置

<Location /balancer-manager>
	SetHandler balancer-manager

	#Order Deny,Allow
	#Deny from all
	#Allow from .example.com
	Order Deny,Allow
	Allow from all
</Location>

同时将Proxy Cluster的配置修改成

<Proxy balancer://mycluster>  
	BalancerMember http://127.0.0.1:8081 loadfactor=3  
    BalancerMember http://127.0.0.1:8082 loadfactor=3  
	ProxySet lbmethod=byrequests 
</Proxy>  
ProxyRequests Off 
ProxyPass /images !
ProxyPass /css !
ProxyPass /js !
ProxyPass /balancer-manager !
ProxyPass / balancer://mycluster/ stickysession=JSESSIONID nofailover=Off  
ProxyPassReverse / balancer://mycluster/	

然后通过浏览器访问

“http://localhost:81/balancer-manager”

得到结果

这样就完成一个以httpd单节点为负载均衡器分发到不同Tomcat的简单架构。

* 扩展

  • lbmethod具体代表什么意思?
  • Load Balancer有哪些具体算法?
  • 为什么通过Proxy访问的时候目标页面没有样式和图片?
  • loadfactor、stickysession有什么作用
  • ProxyRequests、ProxyPass、ProxyPassReverse又有什么作用?
posted @ 2015-12-09 09:52  Richaaaard  阅读(3224)  评论(0编辑  收藏  举报