Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Several ways of summarizing HAProxy and its load

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

The following gives you an overview of HAProxy and its load in several ways, hoping to give you some help in practical application. Load balancing involves more things, not many theories, and there are many books on the Internet. Today, we will use the accumulated experience in the industry to do an answer.

1. Overview of HAProxy

HAProxy provides high availability, load balancing, agents based on TCP and HTTP applications, and supports virtual hosts. It is a free, fast and reliable solution. According to official data, its maximum limit supports 10G concurrency. HAProxy is especially suitable for heavily loaded web sites, which usually require session persistence or seven-tier processing. HAProxy runs on current hardware and can support tens of thousands of concurrent connections. And its operation mode makes it easy and safe to integrate into your current architecture, while protecting your web CVM from being exposed to the network. It supports network switching from layer 4 to layer 7, that is, covering all TCP protocols. That is, Haproxy even supports load balancing for Mysql.

Similarities: functionally, proxy uses reverse proxy to achieve web load balancing, just like Nginx,ApacheProxy,lighttpd, Cheroke, etc.

The difference: Haproxy is not a Http server. All the products with reverse proxy load balancing mentioned above are all WEB servers. Simply put, they can provide static (html,jpg,gif..) on their own. Or dynamic (php,cgi..) Transfer and processing of files. Haproxy is only, and specifically, an application agent for load balancing. It does not provide http services on its own. But its configuration is simple, it has a very good server health check function and a special system status monitoring page. When the back-end server of its agent fails, HAProxy will automatically remove the server, and then automatically join the server after the fault is restored.

Several ways to load HAProxy:

1. Polling roundrobin

two。 Minimum number of connections Leastconn

3. According to the source IP source

4. According to URI uri

5. According to the parameter url_param in URL (lb is done after hush according to the data in the request string, for example, if a userid is always on a server, the strategy is static.

2.HAProxy official website address

Http://www.haproxy.org/

3.Haproxy lab topology

HaProxy dispenser: 192.168.42.175

Web1 (nginx) server:192.168.42.176

Web2 (nginx) server:192.168.42.177

Haproxy server configuration

[root@xuegod175] # tar xvf haproxy-1.5.15.tar.gz-C / usr/local/src/

[root@xuegod175 ~] # cd / usr/local/src/haproxy-1.5.15/

[root@xuegod175 haproxy-1.5.15] # cat README | less / / View the installation method, which is special

[root@xuegod175 haproxy-1.5.15] # uname-a

Linux xuegod175.cn 2.6.32-431.el6.x86_64 # 1 SMP Fri Nov 22 03:15:09 UTC 2013 x86'64 GNU/Linux

[root@xuegod175 haproxy-1.5.15] # make TARGET=linux26 PREFIX=/usr/local/haproxy

/ / specify the operating system kernel type and the installation path. You can also directly modify the values of these two variables in the Makefile configuration file. As follows:

[root@xuegod175 haproxy-1.5.15] # vim Makefile

Add: TARGET = linux26

[root@xuegod175 haproxy-1.5.15] # make install PREFIX=/usr/local/haproxy

[root@xuegod175 haproxy-1.5.15] # cd / usr/local/haproxy/

[root@xuegod175 haproxy] # ls

Doc sbin share # does not have a configuration file directory

[root@xuegod175 haproxy] # mkdir / usr/local/haproxy/etc; cd

[root@xuegod175 ~] # cp / usr/local/src/haproxy-1.5.15/examples/haproxy.cfg / usr/local/haproxy/etc/

[root@xuegod175 ~] # vim / usr/local/haproxy/etc/haproxy.cfg#global global, subject to global definition # defaults default, under the global premise, default to local configuration, if local is not said, then go by default, if local is defined, press local. # frontend front end, listening address, listening port, and how to deal with it. # backend backend server Define a solution for those servers that actually handle business-Realserver.#listen combines frontend and backend-globallog 127.0.0.1 local0#log 127.0.0.1 local1 notice#log loghost local0 infomaxconn 4096chroot / usr/local/haproxyuid 99 # user uidgid 99 # user group daemon # run haproxynbproc 1 # later to start 1 haproxy instance. # # the number of worker processes (number of CPU) should be set to the same number as the number of CPU cores in actual work. In this way, you can maximize performance. Pidfile / usr/local/haproxy/run/haproxy.pid # writes all processes to the pid file # debug # use # quiet # to quiet the output direction of the defaultslog globallog 127.0.0.1 local3 # log file when debugging errors. The resulting log level is local3. Local1-7 in the system, the user defines the mode http # working mode. The category processed adopts http mode by default, and can be configured as tcp for layer 4 message forwarding option httplog # log category, recording http log option httpclose # actively closes the http channel after each request, haproxy does not support keep-alive, and can only simulate the implementation of this mode option dontlognull # does not record empty connections Generated log option forwardfor # if the backend server needs to obtain the parameters that the client needs to configure for the real ip, you can obtain the client ipoption redispatch from Http Header # when the server corresponding to serverid is hung up, the server is forcibly directed to another healthy server retries 2 # if the connection fails for 2 times, the server is considered unavailable Mainly check the maxconn 2000 # maximum number of connections balance roundrobin # load balancing algorithm stats uri / haproxy-stats # haproxy monitoring page access address # can be accessed through http://localhost:80/haproxy-stats timeout connect 5000ms # connection timeout. Unit: ms millisecond, timeout client 50000ms # client connection timeout, timeout server 50000ms # server connection timeout, listen localhost 0.0.0.0 mode httpoption httpchk GET 80 # running port and hostname mode httpoption httpchk GET / index.html # health check. # Note that when testing in practice, you should download a page for testing, so this page should be a small page instead of using the first page. Here is to check the page every other second. Server S1 192.168.42.176 server 80 weight 3 check # back-end host IP & tradeoff server S2 192.168.42.177 check 80 weight 3 check # back-end host IP & tradeoff

[root@xuegod175 ~] # id nobody / / 99 is a user running nobody

Uid=99 (nobody) gid=99 (nobody) groups=99 (nobody)

[root@xuegod175] # mkdir-p / usr/local/haproxy/run

[root@xuegod175] # chown nobody / usr/local/haproxy/-R

[root@xuegod175] # / usr/local/haproxy/sbin/haproxy-f / usr/local/haproxy/etc/haproxy.cfg / / start

[root@xuegod175 ~] # ps-aux | grep haproxy

Warning: bad syntax, perhaps a bogus'-'? See / usr/share/doc/procps-3.2.8/FAQ

Nobody 3327 0.0 0.0 12200 960? Ss 13:37 0:00 / usr/local/haproxy/sbin/haproxy-f

/ usr/local/haproxy/etc/haproxy.cfg

[root@xuegod175 ~] # netstat-antup | grep 80

Tcp 0 0 0.0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0 of the LISTEN 3327/haproxy.

Special restart methods:

[root@xuegod175] # / usr/local/haproxy/sbin/haproxy-f / usr/local/haproxy/etc/haproxy.cfg-st `cat / usr/local/haproxy/run/ haproxy.pid`

/ / restart-st parameter meaning:-sf/-st [pid (haproxy current process ID)] completes / terminates the old PID. This parameter must be the last parameter.

[root@xuegod175 etc] # killall haproxy / / stop the service

Make HAproxy startup script

[root@xuegod175 ~] # cp / usr/local/src/haproxy-1.5.15/examples/haproxy.init / etc/init.d/haproxy / / you can modify this file

Implement startup script, too many changes

Use the following directly as the startup script

[root@xuegod175 ~] # vim / etc/init.d/haproxy

[root@xuegod175 haproxy-1.5.15] # cat / etc/init.d/haproxy #! / bin/shset-ePATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/haproxy/sbinPROGDIR=/usr/local/haproxyPROGNAME=haproxyDAEMON=$PROGDIR/sbin/$PROGNAMECONFIG=$PROGDIR/etc/haproxy.cfgPIDFILE=$PROGDIR/$PROGNAME.pidDESC= "HAProxy daemon" SCRIPTNAME=/etc/init.d/$PROGNAME# Gracefully exit if the package has been removed.test-x $DAEMON | | exit 0start () {echo-n "Starting $DESC: $PROGNAME" $DAEMON-f $CONFIGecho "."} stop () {echo-n "Stopping $DESC: $PROGNAME" haproxy_pid=$ (cat $PIDFILE) kill-15 $haproxy_pidecho "."} restart () {echo-n "Restarting $DESC: $PROGNAME" $DAEMON-f $CONFIG-p $PIDFILE-sf $(cat $PIDFILE) echo. "} case" $1 "instart) start ; stop) stop;;restart) restart;;*) echo "Usage: $SCRIPTNAME {start | stop | restart}" > & 2exit 1 politics esacexit 0

# description: the role of set-e in the script

# every script you write should add set-e at the beginning of the file, which tells bash that if the result of any statement is not true, it should exit. The advantage is to prevent errors from snowballing into a fatal error that should have been dealt with before.

[root@xuegod175] # / etc/init.d/haproxy restart / / can be started

HA-Proxy log configuration

Configure log collection

[root@xuegod175 ~] # vim / etc/rsyslog.conf / / Open the comment with the following two lines. If you don't open it, you won't receive the log.

$ModLoad imudp # Uncomment

UDPServerRun 514 # Uncomment

Add two lines below local7.* / var/log/boot.log #

Local3.* / var/log/haproxy.log

Local0.* / var/log/haproxy.log

[root@xuegod175 ~] # / etc/init.d/rsyslog restart

Web server configuration test file

Note: all the web servers here use nginx, so the installation process is omitted.

[root@xuegod176 ~] # cat / usr/local/nginx/html/index.html

192.168.42.176

[root@xuegod177 ~] # cat / usr/local/nginx/html/index.html

192.168.42.177

Access to test:

Test access interface web1

Test access interface web2

3. Test access to HAProxy

The effect of polling access to web server 1 and 2 is achieved by accessing HAProxy, which shows that HAProxy load balancer has been built successfully.

By visiting http://192.168.42.175/haproxy-stats

Stop the web2 service for testing

After reading the above overview of HAProxy and its load, if there is anything else you need to know, you can find out what you are interested in in the industry information or find our professional technical engineer to answer, the technical engineer has more than ten years of experience in the industry.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report