HAProxy load balancer configuration and algorithm

  
                  

Although we see that the official documentation of HAProxy does have a lot of configuration content, in fact, we use it in the online environment. Only a few of them are enough. This is why HAPROxy is simple. The corresponding configuration file of HAProxy is as follows. Show:

global log 127.0.0.1 local0 maxconn 4096 chroot /usr/local/haproxy uid 99 gid 99 daemon nbproc 1 pidfile /usr/local/haproxy/logs/haproxy.pid debug defaults log 127.0.0.1 local3 Mode http option httplog option httpclose option dontlognull option forwardfor option redispatch retries 2 maxconn 2000 balance source stats uri /haproxy-stats contimeout 5000 clitimeout 50000 srvtimeout 50000 listen 1paituan_proxy 203.93.236.149:80 option httpchk HEAD /index.jsp HTTP/1.0 server web1 203.93 .236.147:80 cookie app1inst1 check inter 2000 rise 2 fall 1 server web2 203.93.236.146:80 cookie app1inst2 check inter 2000 rise 2 fall 1

plus log support as follows:

#vim /etc/syslog.conf Add: local3.* /var/log/haproxy.log local0.* /var/log/haproxy.log #vim /Etc/sysconfig/syslog Modifications: SYSLOGD_OPTIONS="-r -m 0" service syslog restart

There are several places to note here:

First, HAProyx uses the balance source mechanism. It is the same as the persistence of LVS and the ip_hash of Nginx. It is a real web server that allows the client to access the backend at all times;

2.203.93.236.149 is the VIP generated by my website through Keepalived. Address;

Third, option httpchk HEAD /index.jsp HTTP/1.0 is the web page approaching, if HAProxy does not detect the root directory of the Web without index.jsp, it will generate 503 error.

HAProxy+keepalived/Heartbeat is the second solution of my website, because I just started the LVS+Keepalived high-availability architecture on the website. Later, when a few friends said that using mobile phones to test, the forwarding is very slow, sometimes not playing. Open, and even some computers are like this. I tried the following: I used Nginx+Keepalived and found that the forwarding speed is really much faster. There is no such problem on the mobile phone. Because of Nginx+Keepalived customers in Dongguan. It has been deployed very successfully, so I later considered designing this website as HAProyx+Keepalived.

The algorithm of HAProxy has the following eight kinds:

First, roundrobin, which means simple polling, this is not much to say, this is basically supported by load balancing;

Second, static-rr, indicating that according to the weight, it is recommended to pay attention;

Third, leastconn, indicating that the least connector is processed first, it is recommended to pay attention;

Fourth, source, according to the request source IP, suggest Concern;

Five, uri, indicating the URI according to the request;

Six, url_param, indicating the URl parameter according to the request 'balance url_param' requires an URL parameter name

, hdr (name), means to lock each HTTP request according to the HTTP request header;

eight, rdp-cookie (name), means to lock and hash each TCP request according to the cookie (name).

In fact, these algorithms have their own usages. We should use roundrobin, source and lestconn in general. You can focus on the following. Here I used HAProxy+Keepalived without considering the HA of the HAProxy process level. First, because Taobao is also using it as a load balancer, the stability certainly does not need to be skeptical; secondly, it was found to be quite stable during the test, killing The HAProxy process must also use kill -9, so it didn't waste time on it; it did consider using HAProxy+Keepalived to do the dual-master architecture during the test, and later found that it was not too good to maintain.

One thing to remind everyone is that HAProxy supports virtual hosting. I have seen some friends say that it is not supported. This is actually very good. We have a 1+1 architecture. Nginx or Aapceh's web server may verify this statement; in addition, after comparing with Nginx's regularity, we also found that HAProxy's regular processing power is not as strong and flexible as Nginx, we can also take a compromise here. Solution, the latter web server we use Nginx to do, if it is a jsp application, you can use Nginx+tomat, if you are running PHP, you can use Nginx+FastCGI (PHP5).

Copyright © Windows knowledge All Rights Reserved