Software load balancing is generally implemented in two ways: Software load implementation based on the operating system and software load implementation based on third-party applications. LVS is a kind of soft load based on the Linux operating system. haproxy is an open-source software load based on the third application.
AD:
Software load balancing is generally implemented in two ways: Software load implementation based on the operating system and software load implementation based on third-party applications. LVS is a kind of soft load based on the Linux operating system. haproxy is an open-source software load based on the third application.
Compared with LVS, haproxy is much easier to use and has rich functions. Previously, haproxy supported two main proxy modes: "TCP", that is, layer-4 (mostly used for mail servers, internal protocol communication servers, etc.), and layer-7 (HTTP ). In layer-4 mode,HaproxyOnly two-way traffic is forwarded between the client and the server. In layer-7 mode, haproxy analyzes the protocol and controls the Protocol by allowing, rejecting, exchanging, adding, modifying, or deleting specified content in a request or response, this operation is based on specific rules.
I use haproxy mainly because it has the following advantages:
I,Free open-source, stability is also very good, this can be seen through some of my small projects, a single haproxy also runs well, stability can be comparable to LVS;
II,According to the official documentation, haproxy can run full 10gbps-new benchmark of haproxy at 10 Gbps using myricom's 10gbe platinum (Myri-10G PCI-Express), which is also amazing as a software-level load balancing;
III,Haproxy can be used as MySQL, email, or other non-WebServer Load balancerWe often use it as MySQL (read) Load Balancing;
IV,A page with powerful monitoring server status is provided. In the actual environment, we use Nagios for email or SMS alarms. This is one of the reasons why I like it very much;
V,Haproxy supports virtual hosts.
The experiment environment is also simple:
HAProxy IP:192.168.4.192 Centos5.5-64bit
Web1 IP:192.168.4.45 Centos5.4-64bit
Web2 IP:192.168.21.45 FreeBSD8.0-64bit
The installation and configuration documents are as follows:
1. Download and install haproxy:
wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz
make TARGET=linux26 prefix=/usr/local/haproxy install
cd /usr/local/haproxy
mkdir conf
cd conf
vim haproxy.conf
The content is as follows:
Global
Log 127.0.0.1 local0
Maxconn 4096
Chroot/usr/local/haproxy
UID 501
GID 501
Daemon
Nbproc 1
Pidfile/usr/local/haproxy/logs/haproxy. PID
Debug
Ults
Log 127.0.0.1 local3
Mode HTTP
Option httplog
Option httpclose
Option dontlognull
Option forwardfor
Option redispatch
Retries 2
Maxconn 2000
Balance roundrobin
Stats uri/haproxy-Stats
Contimeout 5000
Clitimeout 50000
Srvtimeout 50000
Listen web_proxy 192.168.4.192: 80
# Option httpchk heads/index. php HTTP/1.0
Server webbench 192.168.21.45 192.168.21.45: 80 cookie app1inst1 check inter 2000 rise 2 fall 5
Server web2_192.168.4.45 192.168.4.45: 80 cookie app1inst2 check inter 2000 rise 2 fall 5
The configuration file is copied from the server to ensure availability. Here is an illustration. Sometimes the following error is reported when we go to the http: // 192.168.4.192/Page:
503 service unavailableno server is available to handle this request.
Option httpchk head/check.txt HTTP/1.0
You can change metadata to index. jsp or index. php;
In addition, we recommend that you configure a script for starting, disabling, and restarting haproxy, which is stored in/etc/init. d. We should develop a habit in our daily work. server startup or restart should be the simplest. In case of errors in the production environment, we can start it as quickly as possible, if you do not need a script, we will try to start it as follows:
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
After the following script is generated, we can use/etc/init. d/haproxy start to start the haproxy service.
#! /Bin/bash
Base_dir = "/usr/local/haproxy"
Argv = "[email protected]"
Start ()
{
Echo "Start hapoxy servers"
$ Base_dir/sbin/haproxy-F $ base_dir/CONF/haproxy. cfg
}
Stop ()
{
Echo "Stop hapoxy listen"
Kill-ttou $ (cat $ base_dir/logs/haproxy. PID)
Echo "Stop hapoxy process"
Kill-usr1 $ (cat $ base_dir/logs/haproxy. PID)
}
Case $ argv in
Start)
Start
Error = $?
;;
Stop)
Stop
Error = $?
;;
Restart)
Stop
Start
Error = $?
;;
*)
Echo "Hactl. Sh [Start | restart | stop]"
Esac
Exit $ Error
Enter http: // 192.168.4.192 on Firefox or IE to check the web page of the backend. In addition, we can monitor the HTTP page at any time: // 192.168.4.192/haproxy-status/, as follows:
In addition, if you want to achieve high availability of haproxy, we recommend using haproxy + keepalived. There are also many successful online security examples. You can learn from googel, if you have any questions or other questions about this article,
Hope to communicate with me via my blog at 51cto http://andrewyu.blog.51cto.com (fuqin cooking ).