Apache Load Balancing Setup method: MoD

Source: Internet
Author: User
Tags backup

The first time I saw this headline I was surprised that Apache can still do load balancing? It's so powerful. After a survey found that it can, and the function is not bad. This is thanks to the Mod_proxy module. Is worthy of the mighty Apache AH.

Less nonsense, the following is to explain the load balancing method of setting.

In general, load balancing is the client's request to the backend of the various real servers to achieve load balancing purposes. There is another way is to use two servers, one as the primary server (master), the other as a hot Standby, the request all to the primary server, when the primary server when, immediately switch to the backup server to improve the overall reliability of the system.

Settings for load Balancing

Apache can handle both of these requirements. Let's discuss how to do load balancing first. You first need to enable several modules of Apache:

Program 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 HTTP protocols. If you switch mod_proxy_http into other protocol modules (such as MOD_PROXY_FTP), you might be able to support other protocols with load balancing, and interested friends can try them on their own.

Then you add the following configuration:

Program 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 never added to the production environment!!!
<Location/balancer-manager>
SetHandler Balancer-manager
Order Deny,allow
Deny from all
Allow from localhost
</Location>
From the above proxyrequests off this can be seen, in fact, the load balancer is a reverse proxy, but its proxy forwarding address is not a specific server, but a balancer://protocol:

The Proxypass/balancer://mycluster protocol address can be defined casually. Then, set the contents of the Balancer protocol in the <Proxy> section. The Balancermember directive can add a real server address in a load-balancing group.

The following section <Location/balancer-manager> is used to monitor load balancing work, and debugging can be added (in production environment prohibited!) ), and then access http://localhost/balancer-manager/You can see the load balancing work condition.

OK, after the change, restart the server, access to your Apache server address, you can see the effect of load balancing. Open the Balancer-manager interface and you can see that the request is evenly distributed.

What if you don't want to distribute evenly? Add the Loadfactor parameter to the Balancermember, the value range is 1-100. For example, you have three servers, the load allocation ratio is 7:2:1, just this set:

Program code proxyrequests off
<proxy balancer://mycluster>
Balancermember http://node-a.myserver.com:8080Loadfactor=7
Balancermember http://node-b.myserver.com:8080loadfactor=2
Balancermember http://node-c.myserver.com:8080Loadfactor=1
</Proxy>
Proxypass/balancer://mycluster
By default, load balancing tries to make the number of requests accepted by each server meet the preset proportions. If you want to change the algorithm, you can use the Lbmethod property. Such as:

Program code proxyrequests off
<proxy balancer://mycluster>
Balancermember http://node-a.myserver.com:8080Loadfactor=7
Balancermember http://node-b.myserver.com:8080loadfactor=2
Balancermember http://node-c.myserver.com:8080Loadfactor=1
</Proxy>
Proxypass/balancer://mycluster
Proxyset lbmethod=bytraffic
Lbmethod Possible values are:

Lbmethod=byrequests is balanced according to the number of requests (default)
Lbmethod=bytraffic according to the flow balance
Lbmethod=bybusyness in accordance with the busy level (always assigned to the server with the least number of active requests)

The principles of the various algorithms refer to the Apache documentation.
Hot Standby
The implementation of hot backup is simple, you can designate a server as a backup server simply by adding the Status=+h attribute:

Program code proxyrequests off
<proxy balancer://mycluster>
Balancermember http://node-a.myserver.com:8080
Balancermember http://node-b.myserver.com:8080Status=+h
</Proxy>
Proxypass/balancer://mycluster

As you can see from the Balancer-manager interface, the request always flows to the node-a, and once node-a hangs off, Apache detects the error and streams the request to node-b. Apache detects node-a every few minutes and continues to use Node-a if node-a resumes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.