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 use Haproxy to build web Cluster

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

Share

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

How to use Haproxy to build a web cluster? I believe that most people have not yet learned this skill, in order to let you learn, to give you a summary of the following content, do not say much, let's move on.

Common Web cluster scheduler at present common Web cluster scheduler is divided into software and hardware, software usually uses open source LVS,Haproxy,Nginx, hardware generally uses F5, and many people use some domestic products, such as Shuttle, Green Alliance and other Haproxy application analysis.

LVS has a strong load capacity in enterprise applications, but it has some shortcomings.

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 an agent software that provides high availability, load balancing, and based on TCP and HTTP applications.

It is especially suitable for Web sites with a particularly heavy load, three algorithms that can support tens of thousands of concurrent connection requests Haproxy running on current hardware

RR (Round Robin)

RR algorithm is the simplest and most commonly used algorithm, polling scheduling.

To give examples to understand

There are three nodes. The first user access will be assigned to node A, the second user access will be assigned to node B, and the third user access will be assigned to the node.

The fourth user access continues to be assigned to node A, polling the allocation access request to achieve load balancing

LC (Least Connections)

The LC algorithm is the minimum connection algorithm, which dynamically allocates requests according to the number of node connections at the back end.

To give examples to understand

There are three nodes, An and B, C, and the number of connections of each node is A, 4, and 5, respectively. At this time, if there is the first user connection request, it will be assigned to A, and the number of connections will be A, 5, and the number of connections will be 5.

The second user request will continue to be assigned to A, and the number of connections will be changed to Aburex 6, Borex 5, Magne6, and then a new request will be assigned to B, each time a new request will be assigned to the customer segment with the least number of connections.

Due to the fact that the number of connections of A _ Magi B ~ C will be released dynamically, it is difficult to have the same number of connections, so the algorithm is much better than the rr algorithm, and it is a widely used algorithm at present.

SH (Source Hashing)

SH is a source-based access scheduling algorithm. This algorithm is used in some scenarios where Session sessions are recorded in the server segment, and cluster scheduling can be done based on the source IP.Cookie, etc.

Understand examples

There are three nodes, An and B, 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 An on the second visit, and the second user will still be assigned to B on 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 to realize the scheduling of the cluster.

The advantage of this scheduling algorithm is to achieve session persistence, but when some IP visits are very large, it will cause load imbalance, and the visit volume of some nodes is too large, which affects business usage.

Three parts of the configuration file for Haproxy

Global: global configuration

Defaults: default configuration

Listen: application component configuration

Global configuration parameter log 127.0.0.1 local: configure logging, configure logging, local0 is the logging device, stored in the system log by default log 127.0.0.1 local notice: notice is the log level, usually has 24 to level maxconn 4096 most connections uid: 99 user uid D99: user giddefaults configuration item configuration default parameter, generally will be inherited by the application component, if there is no special declaration in the application component Set the default configuration parameter log global: define the log as the log in the global configuration define mode http mode for httpoption httplog to log in http log format retries 3 check node server failure for 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 is generally to configure application module parameters Listen appli4-backup 0.0.0.0index.html 10004 defines an appli4-backup application option httpchk / index.html check server's index.html file option persist forces requests to be sent to servers that have been down dropped balance roundrobin load balancing scheduling algorithm uses polling algorithm server inst1 192.168.100.201index.html 80 check inter 2000 fall 3 defines an online node server inst2 192.168.100.202 option httpchk 80 check inter 2000 fall 3 defines a backup node Start the experiment: we need three virtual machines

4 nginx server 192.168.100.201

5 nginx server 192.168.100.202

7 Haproxy Server 192.168.100.210

7Haproxy server, configuring Nic in host mode only

Yum install bzip2-devel pcre-devel gcc gcc-c++ make-y

Separate the log from the system log and create an empty file from the original 2dd4 log/ dev/log local0 info 5 log/ dev/log local0 notice# to facilitate the management of the log file [root@localhost haproxy] # cd / etc/rsyslog.d/ [root@localhost 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&~ [root@localhost rsyslog.d] # systemctl restart rsyslog.service then go to the client to refresh the web page and go to server 7 to see if the logs have been separated

[root@localhost rsyslog.d] # cd / var/log/

[root@localhost log] # cd haproxy/ [root@localhost haproxy] # lshaproxy-info.log [root@localhost haproxy] # cat haproxy-info.log Nov 29 16:23:01 localhost haproxy [18770]: 192.168.100.50 root@localhost haproxy 49418 [29/Nov/2019:16:23:01.526] webcluster webcluster/inst1 1 to 0 GET / chen.html HTTP/1.1

After reading the above, have you mastered the method of using Haproxy to build web cluster? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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