Apache Do load Balancing configuration tutorial (Windows/linux)

Source: Internet
Author: User

Windows Apache does load balancing

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:

The code is as follows Copy 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:

The code is as follows Copy 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!!!

The code is as follows Copy Code
<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 code is as follows Copy Code

Proxypass/balancer://mycluster

The protocol address can be defined casually. Then, set the contents of the Balancer protocol in the segment. The Balancermember directive can add a real server address in a load-balancing group.

The following section is used to monitor load balancing work, which can be added when debugging (in the production environment!). And then access the http://localhost/balancer-manager/to see the load-balancing work situation.

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:

The code is as follows Copy 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

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:

The code is as follows Copy 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

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:

The code is as follows Copy 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, 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.

Linux to do load balancing under Apache


Operating system: DEBIAN 5.03 (Ubuntu can also be configured with empathy)

First, install the Apache loading module

The code is as follows Copy Code
Apt-get Install Apache2

Then go to the Apache configuration directory

The code is as follows Copy Code
Cd/etc/apache2

Apache reverse proxy and load balancing actually use the principle of reverse proxy, as for what is called reverse proxy can point this http://baike.baidu.com/view/1165595.htm
Key needs to load the following three modules

The code is as follows Copy 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

Since all of the Apache modules in the Debian system are placed inside the/usr/lib/apache2/modules/, they are loaded through the *.load inside the/etc/apache2/mods-available, if the module requires Etc/apache2/mods-enabled inside using ln to establish a link, of course you can also/etc/apache2/apache2.conf inside directly to the above three words to write into, but do not recommend this. Execute code as follows

The code is as follows Copy Code
Ln-s/etc/apache2/mods-available/proxy.load/etc/apache2/mods-enabled/proxy.load
Ln-s/etc/apache2/mods-available/proxy_http.load/etc/apache2/mods-enabled/proxy_http.load
Ln-s/etc/apache2/mods-available/proxy_balancer.load/etc/apache2/mods-enabled/proxy_banancer.load

Well, after a few steps, we'll restart Apache.

The code is as follows Copy Code
/etc/init.d/apache2 restart

Second, configure the reverse proxy function

After loading the module, you can set up a virtual host to act as a reverse proxy.

The code is as follows Copy Code
Vi/etc/apache2/sites_available/proxy

Configured as follows

The code is as follows Copy Code
Listen 202.xxx.xxx.xxx:80 (IP and port accessed by others)
<virtualhost 202.xxx.xxx.xxx:80> (IBID.)
ServerAdmin admin@bit.net
Proxyrequests off (note that the reverse proxy is turned on)
Proxymaxforwards 100
Proxypreservehost on
Proxypass/http://10.x.xx.xxx/(requests forwarded to the URL)
proxypassreverse/http://10.x.xx.xxx/
<proxy *> (This section is the control of the access)
Order Deny,allow
Allow from all
</Proxy>
</VirtualHost>

And then create the ln link inside the/etc/apache2/sites_enabled/.

The code is as follows Copy Code
Ln-s/etc/apache2/sites_available/proxy/etc/apache2/sites_enabled/proxy

Restart Apache

The code is as follows Copy Code
/etc/init.d/apache2 restart

And then the reverse proxy is turned on.
When someone enters http://202.xxx.xxx.xxx, it goes through the reverse proxy to the http://10.x.xx.xxx, so the simple reverse proxy function is turned on


three, Apache load balanced configuration

A, simple load balancing
And then talk about Apache's load-balancing module.
Similarly, establish a virtual host to be used as load balancing

The code is as follows Copy Code
Vi/etc/apache2/sites_available/balancer

Configured as follows

The code is as follows Copy Code
listen 202. x.xx.xxx:80        (IP and port accessed by others)
<virtualhost 202.x.xx.xxx:80>
     ServerAdmin admin@bit.com
     proxyrequests off
      proxypass/balancer://proxy/ 
     <proxy balancer://proxy>
         Order Deny,allow
          Allow from all
         balancermember http://10.0.0.1
 & nbsp;       balancermember http://10.0.0.2
     </proxy
</virtualhost>

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 address
The proxypass/balancer://proxy/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.
And then create the ln link inside the/etc/apache2/sites_enabled/.

The code is as follows Copy Code
Ln-s/etc/apache2/sites_available/balancer/etc/apache2/sites_enabled/balancer

Restart Apache
/etc/init.d/apache2 restart
So the Apache load balance is configured.
If you visit http://202.x.xx.xxx, you will open http://10.0.0.1 and http://10.0.0.2 evenly, for example, when you open the http://202.x.xx.xxx two times, you open the http://again. 10.0.0.1 Open http://10.0.0.2 at once this is the average request received by two servers to achieve a load balancing effect.

b, load proportional distribution
Of course, if you find that your two server configuration is not the same, one better, one poor, then start to configure a different proportion of load balance. If two servers you want to configure the load allocation ratio to 3:1 then the configuration file is as follows

The code is as follows Copy Code
listen 202. x.xx.xxx:80        (IP and port accessed by others)
<virtualhost 202.x.xx.xxx:80>
     ServerAdmin admin@bit.com
     proxyrequests off
      proxypass/balancer://proxy/ 
     <proxy balancer://proxy>
         Order Deny,allow
          Allow from all
         balancermember http://10.0.0.1    loadfactor= 3
         balancermember http://10.0.0.2    loadfactor= 1
     </proxy>
</virtualhost>

C, Load Assignment algorithm
of course, you might want to achieve load balancing with different algorithms, such as the number of requests, or the flow balance, where the instructions are Lbmethod
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)
In accordance with the flow of the balance configuration as follows

The code is as follows Copy Code
listen 202. x.xx.xxx:80        (IP and port accessed by others)
<virtualhost 202.x.xx.xxx:80>
     ServerAdmin admin@bit.com
     proxyrequests off
      proxypass/balancer://proxy/ 
     proxyset lbmethod=bytraffic    (Add this sentence)
     <proxy balancer://proxy>
          order Deny,allow
         Allow to all
          balancermember http://10.0.0.1   loadfactor= 3
          balancermember http://10.0.0.2   loadfactor= 1
      </proxy>
</virtualhost>

So your load balance can be balanced according to the flow.

D, Hot backup
at the end of the talk about hot backup bar, the implementation of hot backup is very simple, just add the Status=+h attribute, you can designate a server as a backup server, the configuration file as follows

The code is as follows Copy Code
listen 202. x.xx.xxx:80        (IP and port accessed by others)
<virtualhost 202.x.xx.xxx:80>
     ServerAdmin admin@bit.com
     proxyrequests off
      proxypass/balancer://proxy/ 
     proxyset lbmethod=bytraffic    (Add this sentence)
     <proxy balancer://proxy>
          order Deny,allow
         Allow to all
          balancermember http://10.0.0.1  
          balancermember http://10.0.0.2   status=+h
     </ Proxy>
</virtualhost>

  from the configuration can see that the request always flow to http://10.0.0.1, once the http://10.0.0.1 hang up, Apache will detect the error and divert the request to http://10.0.0.2. Apache detects http://10.0.0.1 every few minutes and, if http://10.0.0.1 recovers, continues to use http://10.0.0.1, which enables hot backup.

Related Article

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.