Generally, Server Load balancer distributes client requests to various real backend servers to achieve Server Load balancer. Another method is to use two servers, one as the master server and the other as the hot standby. All requests are distributed to the master server, switch to the backup server immediately to improve the overall availability of the system.
I was surprised to see this title for the first time. Can Apache still perform load balancing? It's so powerful. After some investigation, we found that it is true, and its functions are not bad at all. This is all due to the mod_proxy module. It is a powerful Apache.
To put it bluntly, Let's explain how to set up Server Load balancer.
Generally, Server Load balancer distributes client requests to various real backend servers to achieve Server Load balancer. Another method is to use two servers, one as the master server and the other as the hot standby. All requests are distributed to the master server, switch to the backup server immediately to improve the overall system reliability.
1. Server Load balancer settings
1). basic configuration
Apache can meet the above two requirements. Let's first discuss how to implement load balancing. Assume that the domain name of an Apache server is www.a.com. You must first 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 the proxy server function. mod_proxy_balancer provides the Server Load balancer function. mod_proxy_http enables the proxy server to support the HTTP protocol. If you change mod_proxy_http to another protocol module (such as mod_proxy_ftp), you may be able to support Load Balancing for other protocols. If you are interested, try it on your own.
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 configurations are only used for debugging. Do not add them to the 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 the domain name of the current server
From the above proxyrequests off, we can see that the Server Load balancer is actually a reverse proxy, but its proxy forwarding address is not a specific server, but a balancer: // protocol:
Proxypass/balancer: // The mycluster Protocol address can be defined at will. Then, set the content of the balancer protocol in the <proxy> section. The balancermember command can be used to add real server addresses in the Server Load balancer group.
The following <location/balancer-Manager> is used to monitor the working conditions of Server Load balancer. It can be added during debugging (unavailable in the production environment !), Access http: // localhost/balancer-Manager/to view the working status of the Server Load balancer.
OK. Restart the server and access the address of your Apache server (www.a.com) to see the effect of Server Load balancer.
Error prompt:
The webpage prompts internal serveral error. Check the 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 encoded in the configuration using loadmodule.
The reason is that the configuration: # proxypass/balancer: // mycluster may be missing one/
2). Load Ratio allocation
On the balancer-Manager page, you can see that requests are evenly distributed.
What should I do if I don't want an average allocation? Add the loadfactor parameter to balancermember. The value range is 1-100. For example, if you have three servers and the load distribution ratio is, you only need to set them as follows:
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, Server Load balancer tries its best to make the number of requests received by each server meet the predefined ratio. If you want to change the algorithm, you can use the lbmethod attribute. For example:
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 include:
Lbmethod = byrequests balanced by the number of requests (default)
Lbmethod = bytraffic balanced by traffic
Lbmethod = bybusyness balanced by the degree of busy (always allocated to the server with the least number of active requests)
For the principles of various algorithms, see the Apache documentation.
2. Hot Standby)
Hot Backup is easy to implement. You only need to add 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
From the balancer-Manager interface, we can see that requests always flow to node-A. Once node-A fails, Apache will detect errors and distribute requests to node-B. Apache checks the status of node-A every few minutes. If node-A recovers, it continues to use node-.
How to configure Apache Server Load balancer: mod_proxy