Haproxy builds a Web cluster (theory + practical operation)

One, common web cluster scheduler

The current common Web cluster scheduler is divided into software and hardware:

  • The software usually uses open source LVS, Haproxy, Nginx
  • The most commonly used hardware is F5, and many people use some domestic products, such as Barracuda, NSFOCUS, etc.

Two, Haproxy application analysis

2.1 LVS

  • Strong anti-load ability in enterprise applications, but there are deficiencies

  • LVS does not support regular processing and cannot achieve dynamic and static separation

  • For large websites, the implementation and configuration of LVS are complicated and the maintenance cost is relatively high

2.2 Haproxy

  • It is a software that provides high availability, load balancing, and proxy based on TCP and HTTP applications
  • Suitable for Web sites with heavy loads
  • Running on hardware can support tens of thousands of concurrent connection requests

Three, Haproxy scheduling algorithm principle

Haproxy supports multiple scheduling algorithms, the most commonly used are three

3.1 RR(Round Robin)

RR algorithm is the simplest and most commonly used algorithm, namely round-robin scheduling
Example:
A, B, and C are three node servers
. 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 node C, and the
fourth user access will be assigned to node C. Continue to be assigned to node A, polling and assigning access requests to achieve load balancing effect
(that is, distributed to each node server in order)

3.2 LC(Least Connections)

The minimum number of connections algorithm dynamically allocates front-end requests according to the number of back-end node connections
Example:
Three node servers A, B, C, the number of connections of each node are 4, 5, and 6, respectively
. The first user connection request will be assigned to A, and the number of connections to node A will become 5 at this time
. User requests will continue to be allocated to A, and the number of connections will become A:6, B:5, C:6; at this time, a new request will be allocated to B, that is, each new request will be assigned to the smallest number of connections Client
But in actual conditions, the number of connections of nodes A, B, and C will be dynamically released, and it is difficult to have the same number of connections.
Compared with the rr algorithm, this algorithm is greatly improved.Is one of the most used algorithms

3.3 SH(Source Hashing)

Based on the source access scheduling algorithm, used in some scenarios where Session sessions are recorded on the server side. Cluster scheduling can be done based on the source IP, Cookie, etc.
Example:
There are three nodes A, B, C, the first user is assigned to A for the first visit, the second user is assigned to B
for the first visit, and will continue when the first user visits for the second time Assigned to A, the second user will still be assigned to B during the second visit. As long as the load balancing scheduler is not restarted, the first user will be assigned to A, and the second user will be assigned to B. Realize cluster scheduling The
advantage of this scheduling algorithm is to achieve session retention, but when some IP accesses are very large, it will cause unbalanced load, and some nodes have excessive access, which affects business use

Fourth, Haproxy builds a Web cluster experiment

4.1 Experimental environment

Host operating system IP address
Haproxy-Server CentOS7 192.168.153.10
Nginx-Server1 CentOS7 192.168.153.20
Nginx-Server2 CentOS7 192.168.153.30
Client CentOS7 192.168.153.40

4.2 Haproxy server deployment (192.168.153.10)

① Turn off the firewall and transfer the software package to the /opt directory

systemctl stop firewalld.service
setenforce 0

cd  /opt
haproxy-1.5.19.tar.gz

② Compile and install Haproxy

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

tar zxvf haproxy-1.5.19.tar.gz
cd haproxy-1.5.19/
make TARGET=linux2628 ARCH=x86_64
make install
参数说明:
TARGET=linux26  #内核版本,
#使用uname -r查看内核,如:2.6.18-371.el5,此时该参数用TARGET=linux26;
kernel大于2.6.28的用TARGET=linux2628

ARCH=x86_64   #系统位数,64位系统

③ Haproxy server configuration

mkdir /etc/haproxy
cp examples/haproxy.cfg /etc/haproxy/

cd /etc/haproxy/
vim haproxy.cfg
global

      log /dev/log local0 info     #修改4~5行,配置日志记录,local0为日志设备,默认存放到系统日志
      log /dev/log local0 notice
      #log loghost local0 info
      maxconn 4096                 #最大连接数,需考虑ulimit-n限制

      #chroot /usr/share/haproxy   #8行,chroot运行路径,为该服务自设置的根目录,一般需将此行注释掉
      uid 99                       #用户UID
      gid 99                       #用户GID
      daemon                       #守护进程模式
	  
defaults
        log       global           #定义日志为global配置中的日志定义
        mode      http             #模式为http
        option    httplog          #采用http日志格式记录日志
        option    dontlognull      #不记录健康检查日志信息
        retries 3                  #检查节点服务器失败次数,连续达到三次失败,则认为节点不可用
		redispatch                 #当服务器负载很高时,自动结束当前队列处理比较久的连接
		maxconn 2000               #最大连接数
		contimeout     5000        #连接超时时间
		clitimeout     50000       #客户端超时时间
		srvtimeout     50000       #服务器超时时间
		
删除下面的listen项(全部删除),添加以下内容:
listen webcluster 0.0.0.0:80                                     #定义一个名为webcluster的应用
		option httpchk GET /test.html                            #检查服务器的test.html文件
	    balance roundrobin                                       #负载均衡调度算法使用轮润算法roundrobin
	    server inst1 192.168.153.20:80 check inter 2000 fall 3   #定义在线节点
		server inst2 192.168.153.30:80 check inter 2000 fall 3

After modification:
Insert picture description here

4.3 Nginx node server deployment (192.168.153.20, 192.168.1853.30)

systemctl stop firewalld
setenforce 0

yum install -y pcre-devel zlib-devel gcc gcc-c++ make 

useradd -M -s /sbin/nologin nginx

cd /opt
传入软件包
tar zxvf nginx-1.12.0.tar.gz -C /opt/

cd nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx

make && make install

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

nginx      #启动nginx 服务

-----192.168.153.20-----

echo "this is test1 web" > /usr/local/nginx/html/test.html
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin

-----192.168.153.30-----

echo "this is test2 web" > /usr/local/nginx/html/test.html
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin

4.4 Start Haproxy service (192.168.153.10)

cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
chmod +x haproxy
chkconfig --add /etc/init.d/haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
service haproxy start 或 /etc/init.d/haproxy start

4.5 Browser Test Web Cluster

The client uses a browser to open http://192.168.153.10/test.html, and constantly refresh the browser to test the load balancing effect
Insert picture description here

Insert picture description here

Five, log definition

The default haproxy log is output to the system syslog, which is not very convenient to view. In order to better manage the haproxy log, we generally define it separately in the production environment. It is necessary to record the info and notice logs of haproxy into different log files.

vim /etc/haproxy/haproxy.cfg
global
	log /dev/log local0 info
	log /dev/log local0 notice

service haproxy restart

Need to modify the rsyslog configuration, in order to facilitate management. Define haproxy-related configurations independently in haproxy.conf and put them under /etc/rsyslog.d/. When rsyslog is started, all configuration files in this directory will be automatically loaded.

vim /etc/rsyslog.d/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
&~

Note:
This part of the configuration is to record the info log of haproxy to /var/log/haproxy/haproxy-info.log, and to record the notice log to /var/log/haproxy/haproxy-notice.log. "&~" means that when the log is written to the log file, rsyslog stops processing this information.

systemctl restart rsyslog.service

tail -f/var/log/haproxy/haproxy-info.log		#查看haproxy的访问请求日志信

Guess you like

Origin blog.csdn.net/weixin_51613313/article/details/112981710