Introduction to the use of Apache load balancing setting method mod_proxy

  • 2020-05-06 12:07:25
  • OfStack

In general, load balancing is the client side of the request to the backend of each real server, to achieve the purpose of load balancing. Another way is to use two servers, one as the primary server (Master), the other as a hot backup (Hot Standby), request all allocated to the primary server, when the primary server is down, immediately switch to the backup server, in order to improve the overall
system I was also surprised when I first saw this title, how can Apache do load balancing? It's so powerful. After some research, we found that it does, and it's not bad at all. This is all thanks to the mod_proxy module. It's the mighty Apache.

Without further ado, explains how to set up load balancing.

Generally speaking,
load balancing is to distribute the requests from the client side to the real servers at the back end to achieve the purpose of load balancing. Another way is to use two servers, one as the primary server (Master), the other as a hot backup (Hot Standby), request all allocated to the primary server, when the primary server when the machine, immediately switch to the backup server, in order to improve the overall reliability of the system.

1. Set
for load balancing

1). Basic configuration of
Apache addresses both of these requirements. Let's talk about how to do load balancing. Assuming an apache server has the domain name www.a.com, you first need to enable several modules of Apache:
Httpd.
conf code
 
LoadModule proxy_module modules/mod_proxy.so 
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so 
LoadModule proxy_http_module modules/mod_proxy_http.so 

mod_proxy provides proxy server functionality, mod_proxy_balancer provides load balancing, and mod_proxy_http enables proxy servers to support the HTTP protocol. If you replace mod_proxy_http with another protocol module (such as mod_proxy_ftp), you may be able to support load balancing of other protocols, so you can try it yourself.

Then add the following configuration:
Httpd.
conf code
 
ProxyRequests Off 
<Proxy balancer://mycluster> 
BalancerMember http://node-a.myserver.com:8080 
BalancerMember http://node-b.myserver.com:8080 
</Proxy> 
ProxyPass / balancer://mycluster/ 
#  Warning: the following configuration is for debugging only and should never be added to a production environment!!  
<Location /balancer-manager> 
SetHandler balancer-manager 
order Deny,Allow 
Deny from all 
Allow from localhost 
</Location> 

Note: node-a.myserver.com, node-b.myserver.com is the domain name of the other two servers, not
of the current server
As you can see from the ProxyRequests Off above, the load balancer is actually a reverse proxy, except that its proxy forwarding address is not a specific server, but an balancer:// protocol:

ProxyPass/balancer://mycluster protocol address can be defined at will. Then, in < Proxy > Section to set the content of the balancer protocol. The BalancerMember directive can add a real server address in a load balancing group.

< below Location /balancer-manager > Is used to monitor the load balancing work, debugging can be added (production is not allowed!) , then visit http://localhost/ balancer-manager/to see the load balancing status.

OK, after the change, restart the server, access your Apache server address (www.a.com), you can see the effect of load balancing.

Error:
Visit Internal Serveral Error and see error.log file
Error.
log code
 
[warn] proxy: No protocol handler was valid for the URL /admin/login_form. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule. 


The reason is the configuration: # ProxyPass/balancer://mycluster may be missing one/

2). Load proportion distribution
Open the balancer-manager interface and you can see that the requests are evenly distributed.

What if you don't want to divide it equally? Add BalancerMember to loadfactor, which ranges from 1 to 100. For example, if you have three servers and the load distribution ratio is 7:2:1, just set it like this:
Httpd.
conf code
 
ProxyRequests Off 
<Proxy balancer://mycluster> 
BalancerMember http://node-a.myserver.com:8080 loadfactor=7 
BalancerMember http://node-b.myserver.com:8080 loadfactor=2 
BalancerMember http://node-c.myserver.com:8080 loadfactor=1 
</Proxy> 
ProxyPass / balancer://mycluster 


3). Load distribution algorithm

By default, load balancing tries to keep the number of requests each server accepts to a preset ratio. If you want to change the algorithm, you can use the lbmethod attribute. Such as:
Httpd.
conf code
 
ProxyRequests Off 
<Proxy balancer://mycluster> 
BalancerMember http://node-a.myserver.com:8080 loadfactor=7 
BalancerMember http://node-b.myserver.com:8080 loadfactor=2 
BalancerMember http://node-c.myserver.com:8080 loadfactor=1 
</Proxy> 
ProxyPass / balancer://mycluster 
ProxySet lbmethod=bytraffic 


Possible values of lbmethod are:

lbmethod=byrequests balanced by the number of requests (default)
lbmethod=bytraffic equalize
by traffic lbmethod=bybusyness balanced by busyness (always assigned to the server with the lowest number of active requests)

See the Apache documentation for the principles of the various algorithms.

2. Hot backup (Hot Standby)
The implementation of hot backup is as simple as adding the status=+H attribute to specify a server as a backup server:

Httpd.
conf code
 
ProxyRequests Off 
<Proxy balancer://mycluster> 
BalancerMember http://node-a.myserver.com:8080 
BalancerMember http://node-b.myserver.com:8080 status=+H 
</Proxy> 
ProxyPass / balancer://mycluster 

As you can see from the balancer-manager interface, requests always flow to node-a. Once node-a fails, Apache detects the error and shred the request to node-b. Apache will check the condition of node-a every few minutes. If node-a recovers, continue to use node-a.

Related articles: