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

How to build a Web cluster in Haproxy?

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

HAProxy is free and open source software written in C language [1] that provides high availability, load balancing, and TCP and HTTP-based application proxies.

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 mode of operation makes it easy and secure to integrate into your current architecture, while protecting your web server from being exposed to the network.

Haproxy supports a variety of scheduling algorithms, of which three are the most commonly used:

(1) RR:RR algorithm is the simplest and most commonly used algorithm, that is, polling scheduling.

(2) LC:LC algorithm is the minimum number of connections algorithm, which dynamically allocates front-end requests according to the number of back-end node connections.

(3) SH:SH is based on the source access scheduling algorithm. The sub-algorithm is used for some scenarios where Session sessions are recorded on the server. Cluster scheduling can be done based on the source IP, Cookie, etc.

Experimental examples: (1) Environment preparation: two Nginx servers, a Haproxy scheduling server and a window are used for testing; two test web pages are written in two Nginx servers; all host network cards are set to host-only mode to modify the network card information; the client can access two node servers by accessing the scheduling server, without the need to access the real server address. Role IP address Haproxy 192.168.100.100Nginx 01 192.168.100.201Nginx 02 192.168.100.202window10 (for testing) 192.168.100.50

The first step: build two Nginx servers here no longer write the details, you can see the previous blog, there are details about the construction process and details. After building the Nginx service, add a test home page to the Nginx home page html to verify the experimental results: the first one:

Station 2:

Step 2: build Haproxy (1) install the compilation tool: yum install bzip2-devel pcre-devel gcc gcc-c++ make-y (2) you can mount the haproxy package locally by remote mount, and then extract it: tar zxvf haproxy-1.5.19.tar.gz-C / opt/ to the / opt directory (3) compile: make TARGET=linux3100 Note: this version number, you can use the uname-a command to check the version number

Make install (4) create a file Make backup modifications: mkdir / etc/haproxycp examples/haproxy.cfg / etc/haproxy/vim / etc/haproxy/haproxy.cfgglobal log 127.0.0.1 local0 log 127.0.0.1 local1 notice # log loghost local0 info maxconn 4096 uid 99 gid 99 daemon # debug # quietdefaults log global mode http option httplog Option dontlognull retries 3 maxconn 2000 contimeout 5000 clitimeout 50000 srvtimeout 50000listen webcluster 0.0.0.0 clitimeout 80 option httpchk GET / test.html balance roundrobin server inst1 192.168.100.201 server inst2 80 check inter 2000 fall 3 server inst2 192.168.100.202 check inter 2000 fall 3 (5) Startup script: cp examples/haproxy.init / etc/init.d/haproxy / / Copy to init.d startup process chmod + x / etc/init.d/haproxy / / add execution permission chkconfig-- add haproxy / / add to service Easy to identify ln-s / usr/local/sbin/haproxy / usr/sbin/haproxy / / create a soft connection (6) enable service: service haproxy start

Turn off firewalls and other environments [root@Haproxy haproxy-1.5.19] # systemctl stop firewalld.service [root@Haproxy haproxy-1.5.19] # setenforce 0 step 3: access the scheduling server (192.168.100.100/test.html) with a browser on win7

Step 4: optimize the log to optimize and modify the configuration file of the scheduler, so that the normal access information and error information can be stored in different log files to facilitate management; the log of Haproxy is output to the syslog of the system by default and is generally defined separately in the production environment. (1) modify the main configuration file vim/etc/haproxy/haproxy.cfg modify global section: log / dev/log local0 info / / normal access information storage log / dev/log local0 notice / / alerts, warnings and other information storage place

(2) restart the service service haproxy restart (3) create a file And add the rule [root@Haproxy haproxy] # touch / etc/rsyslog.d/haproxy.conf [root@Haproxy haproxy] # cd / etc/rsyslog.d/ [root@Haproxy rsyslog.d] # vim haproxy.conf if ($programname = = 'haproxy' and $syslogseverity-text = =' info') then-/ var/log/haproxy/haproxy-info.log&~if ($programname = = 'haproxy' and $syslogseverity-text = =' notice') then-/ var/log/haproxy/haproxy-notice.log&~ (4) restart date Log service systemctl restart rsyslog.service (5) View log file: / var/log/haproxy/haproxy-info.log

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