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

Detailed description of Haproxy Building web Cluster

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

Share

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

Common Web Cluster Scheduler at present the common Web cluster scheduler is divided into software and hardware, software usually uses open source LVS, Haproxy, Nginx, hardware is generally used more F5, there are also many people use some domestic products, such as barracuda, Green Alliance and other Haproxy applications analyze that LVS has strong anti-load ability in enterprise applications, but there are shortcomings that LVS does not support regular processing and can not achieve dynamic and static separation for large websites. The implementation configuration of LVS is complex, and the maintenance cost is relatively high. Haproxy is a software that can provide high availability, load balancing, and agents based on TCP and HTTP applications. It is especially suitable for Web sites with particularly heavy loads. On current hardware, it can support tens of thousands of concurrent connection requests Haproxy scheduling algorithm principle Haproxy supports a variety of scheduling algorithms, the most commonly used is three kinds of RR (Round Robin) RR algorithm is the most simple and most commonly used algorithm. That is, polling scheduling understanding example has three nodes A, B, C, the first user access will be assigned to node A, the second user access will be assigned to node B, the third user access will be assigned to the node the fourth user access will continue to be assigned to node A, polling allocation access requests to achieve load balancing effect LC (Least Connections) LC algorithm that is the minimum number of connections algorithm Dynamic allocation of front-end requests according to the number of connections in the backend. For example, there are three nodes A, B, and C, and the connection numbers of each node are A 4, B 5, and C 6, respectively. If there is a first user connection request, it will be assigned to A, and the second user request will continue to be assigned to A, and the number of connections will be 6, 5, and 6. Another new request will be assigned to B, and the new request will be assigned to the client with the least number of connections each time. Because the number of connections in A, B, C will be released dynamically, it is difficult to have the same number of connections. Therefore, this algorithm has a great improvement compared with rr algorithm. It is a widely used algorithm SH (Source Hashing) SH, that is, source-based access scheduling algorithm. This algorithm is used for scenarios where Session sessions are recorded on the server, and cluster scheduling can be done based on the source IP, Cookie, etc.

Understand examples

There are three nodes A, B, C, the first user is assigned to A for the first visit, and the second user is assigned to B for the first visit. The first user will continue to be assigned to A for the second visit, and the second user will still be assigned to B for the second visit. As long as the load balancer scheduler does not restart, the first user access will be assigned to A, and the second user access will be assigned to B. Implementation of cluster scheduling the advantage of this scheduling algorithm is session persistence, but some IP visits will be cited when the traffic is very large | some nodes with unbalanced load have too much traffic, which affects the business use case topology diagram.

Haproxy installation and startup install Haproxy on load balancer install steps install basic software package compile and install haproxy pay attention to the operating system version Whether it is a 32-bit system or 64-bit Haproxy configuration file creation configuration file directory / etc/haproxy copy the configuration file sample haproxy.cfg provided by the source package to the configuration file directory Haproxy configuration file detailed description of the Haproxy configuration file is usually divided into three parts global: global configuration defaults: default configuration listen: configure global configuration parameter log 127.0.0.1 local0: configure logging, local0 is a logging device Default storage in Syslog log 127.0.0.1 local1 notice: notice is log level, and there are usually 24 levels maxconn 4096: maximum number of connections uid 99: user uidd 99: user giddefaults configuration default parameters, which are generally inherited by the application component, if there is no special declaration in the application component Set the default configuration parameter log global: define log to log in global configuration mode http: mode is httpoption httplog: log is logged in http log format retries 3: check that the node server fails three times in a row, it is considered that the node is unavailable maxconn 2000: maximum number of connections contimeout 5000: connection timeout clitimeout 50000: client timeout srvtimeout 50000: server timeout

Listen configuration project I-general for configuring application module parameters

Listen appli4-backup 0.0.0.0 listen appli4-backup 10004: define an appli4-backup application option httpchk / index.html: check the index.html file of the server option persist: force the request to be sent to the server that has been dropped balance roundrobin: load balancing scheduling algorithm using polling algorithm server inst1 192. 168.114.56 check inter 80 check inter 2000 fall 3: defines the online node server inst2 192. 168.114.56 check inter 81 check inter 2000 fall 3 backup: define backup node configuration and test modify the corresponding parameters according to the actual case: listen webcluster 0.0.0.0:80option httpchk GET / index.htmlbalance roundrobinserver inst1 192. 168.10.61:80 check inter 2000 fall 3server inst2 192.168. 10.62 check inter 80 check inter 2000 fall 3 create a self-startup script and start the Haproxy service to test the Haproxy cluster test high performance can access two test websites in two different browsers, normally there should be test pages for two websites high availability to disable one of the Nginx servers and access Haproxy in the client browser Normally, there should be another Nginx server website test page Haproxy log management Haproxy logs are output to the syslog of the system by default, and the method steps defined separately in the production environment are generally defined separately. Modify the log configuration options in the Haproxy configuration file and add the configuration: log / dev/log local0 infolog / dev/log local0 notice modify rsyslog configuration Define Haproxy-related configuration to haproxy.conf independently, save configuration file under / etc/rsyslog.d/ and restart rsyslog service, complete rsyslog configuration, access Haproxy cluster test web page and test log information Haproxy parameter optimization as the load of enterprise website increases, haproxy parameter optimization is very important maxconn: maximum number of connections, adjusted according to the actual situation of the application, it is recommended to use 10240daemon: daemon mode Haproxy can be started in non-daemon mode. It is recommended to use daemon mode to start nbproc: number of concurrent processes for load balancing, equal to or twice the number of CPU cores of the current server: number of retries, mainly used for checking cluster nodes. If there are many nodes and concurrency is large, set to 2 or 3 times option http-server-close: disable http request option actively It is recommended to use this option in production environment: timeout http-keep-alive: long connection timeout. Setting long connection timeout can be set to 10stimeout http-request: http request timeout. It is recommended to set this time to 5 ~ 10s to increase the release speed of http connection. Timeout client: client timeout. If the number of visits is too large and the node responds slowly, you can set this time to be shorter. It is recommended to set it to 1min or so to operate the example experimental environment Haporxy server IP address: 192.168.144.175web1 server IP address: 192.168.144.151web2 server IP address: 192.168.144.176client testing machine in web1 Install Nginx [root @ web1 ~] # yum install-y\ / / install environment requires component package > pcre-devel\ / / Development package > zlib-devel\ / / package > gcc\ > gcc-c++\ > make [root@web1 ~] # useradd-M-s / sbin/nologin nginx / / create system user [root@ Web1 ~] # mkdir / abc # # create mount point [root@web1 ~] # mount.cifs / / 192.168.100.8/LNMP-C7 / abc/ mount Password for root@//192.168.100.3/LNMP-C7: [root@web1 ~] # cd / abc/ [root@web1 abc] # tar zxvf nginx-1.12.2.tar.gz-C / opt / / decompress [root@web1 abc] # cd / Opt/nginx-1.12.2/ [root@web1 nginx-1.12.2] #. / configure\ / / configure >-- prefix=/usr/local/nginx\ >-- user=nginx\ >-- group=nginx [root@web1 nginx-1.12.2] # make & & make install [root@web1 nginx-1.12.2] # echo "this is kgv web" > / usr/local/nginx/html/test.html / / create site web content For this is accp web [root@web1 nginx-1.12.2] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/ on web2 to facilitate system recognition [root@web1 nginx-1.12.2] # nginx- t / / check syntax [root@web1 nginx-1.12.2] # nginx / / enable the service [root@web1 nginx-1.12.2] # systemctl stop firewalld.service / / disable the firewall [root@web1 nginx-1.12.2] # setenforce 0 install the haproxy scheduling service on the haproxy server [root@haproxy ~] # yum install-y\ / / install environment component tools > pcre-devel\ > bzip2-devel\ > gcc\ > gcc-c++\ > make [root@haproxy ~] # systemctl stop firewalld.service / / close the firewall [root@haproxy ~] # setenforce 0 [root@haproxy ~] # mkdir / abc [root@haproxy ~] # mount.cifs / / 192.168.100.8/LNMP-C7 / abc/ mount [root@haproxy ~] # cd / abc/ [root@haproxy abc] # tar zxvf haproxy-1.5.19.tar.gz-C / opt/ extract [root@haproxy abc] # cd / opt/haproxy-1.5.19/ [root@haproxy haproxy-1.5.19] # make TARGET=linux26 / / compile [root@haproxy haproxy-1.5.19] # make install / / install [root@haproxy haproxy-1.5.19] # mkdir / etc/haproxy/ / create configuration file directory [root@haproxy haproxy-1.5.19] # cp examples/haproxy.cfg / etc/haproxy/ template copy to configuration directory [root@haproxy haproxy-1.5.19] # cd / etc/haproxy/ [root@haproxy haproxy] # vim haproxy.cfg / / Edit configuration file # chroot / usr/share/ Haproxy / / comment statement # redispatch / / comment statement / / Delete all listen items And add listen webcluster 0.0.0.0 test.html 80 option httpchk GET / test.html / / web page balance roundrobin / / turn on polling mode server inst1 192.168.144.151 balance roundrobin 80 check inter 2000 fall 3 / / Health check requests three times server inst2 192.168.144.176 check Inter 2000 fall 3 [root@haproxy haproxy] # cp / opt/haproxy-1.5.19/examples/haproxy.init / etc/init.d/haproxy / / Startup File [root@haproxy haproxy] # chmod + x / etc/init.d/haproxy / / Executive permissions [root@haproxy haproxy] # chkconfig-- add / etc/init.d/haproxy / / add to service [root@haproxy haproxy] # ln-s / usr / local/sbin/haproxy / usr/sbin/ make it easy for the system to identify [root@haproxy haproxy] # service haproxy start / / enable service Starting haproxy (via systemctl): [OK] [root@haproxy haproxy] # netstat-ntap | grep haproxy / / View port tcp 000.0. 0.0 80 0.0.0.0 LISTEN 39884/haproxy uses client to test the web page

Log definition Modify haproxy configuration file [root@haproxy haproxy] # vim / etc/haproxy/haproxy.cfg # # modify configuration file global log / dev/log local0 info # # add two levels of log files log / dev/log local0 notice # log loghost local0 info [root@haproxy haproxy] # service haproxy restart # # restart the service [root@haproxy haproxy] # touch / etc/ Rsyslog.d/haproxy.conf # # create Syslog haproxy configuration file [root@haproxy haproxy] # vim / etc/rsyslog.d/haproxy.confif ($programname = = 'haproxy' and $syslogseverity-text = =' info') # # create different log files then-/ var/log/haproxy/haproxy-info.log&~if ($programname = = 'haproxy' and $syslogseverity-text = =' notice') then-/ var/log/haproxy/haproxy-notice.log&~ [root@] according to level Haproxy haproxy] # systemctl restart rsyslog.service # # restart the Syslog service [root@haproxy haproxy] # cd / var/log/ # # there is no haproxy log # # revisit the web page [root@haproxy haproxy] # cd / var/log/haproxy/ [root@haproxy haproxy] # ls # # the info-level log file haproxy-info.log is generated

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