Theory + experiment: Haproxy builds a Web cluster

1. Common WEB cluster scheduler

  • The current common WEB cluster scheduler is divided into software and hardware
  • Software usually uses LVS, Haproxy, Nginx
  • The most commonly used hardware is F5, and many people use some domestic products, such as Barracuda and NSFOCUS.

Two, Haproxy application analysis

■LVS has 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

■Haproxy is a software that provides high availability, load balancing, and application proxy based on TCP and HTTP

  • Suitable for web sites with heavy loads
  • Running on hardware can support tens of thousands of concurrent connection requests

Three, Haproxy scheduling algorithm principle

3.1、RR (Round Robin)

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

  • RR (Round Robin)
    ◆ RR algorithm is the simplest and most commonly used algorithm, namely round robin scheduling

  • Understanding example
    ◆ There are 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 node C
    ◆ Fourth A user access continues to be assigned to node A, polling to allocate access requests to achieve load balancing

3.2、LC (Least Connections)

■ LC (Least Connections)

  • The minimum number of connections algorithm dynamically allocates front-end requests according to the number of back-end node connections

■ Understanding examples

  • There are three nodes A, B, and C, and the number of connections of each node is A: 4, B: 5, C: 6
  • The first user connection request will be assigned to A, and the number of connections will become A:5, B:5, C:6
  • The second user request will continue to be allocated to A, and the number of connections will become A:6, B:5, C:6; another new request will be allocated to B, and each new request will be assigned to the smallest number of connections Client
  • Because the number of connections of A, B, and C will be dynamically released in actual situations, it is difficult to have the same number of connections
  • Compared with the rr algorithm, this algorithm is greatly improved, and it is an algorithm that is currently used more.

3.3、SH(Source Hashing)

■ 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.

■Understanding examples

  • There are three nodes A, B, and C. The first user is assigned to A for the first visit, and the second user is assigned to B for the first visit.
  • When the first user visits for the second time, it will continue to be assigned to A, and the second user will still be assigned to B for the second visit. As long as the load balancing scheduler does not restart, the first user’s access will be assigned to A, the second user access will be assigned to B to achieve cluster scheduling
  • The advantage of this scheduling algorithm is to achieve session retention, but when some IP visits are very large, it will cause unbalanced load, and some nodes have excessive visits, which affects business use

Fourth, use Haproxy to build a web cluster

4.1, experimental configuration
###########  使用Haproxy搭建Web群集  #######
场景介绍:
     主机           操作系统                 IP地址                         主要软件
Haproxy服务器       CentoS7.6             192.168.100.21                haproxy-1.5.19.tar.gz
Nginx服务器1        CentoS7.6             192.168.100.22                Nginx-1.12.2.tar.gz
Nginx服务器2        CentoS7.6             192.168.100.23                Nginx-1.12.2.tar.gz
存储服务器          CentoS7.6             192.168.100.24                nfs-utils   rpcbind
######调试存储服务器  192.168.100.24 #####
[root@localhost ~]#rpm -q nfs-utils    ###如果没装,yum -y install nfs-utils
[root@localhost ~]#rpm -q rpcbind      ###如果没装,yum -y install rpcbind

[root@localhost ~]# mkdir /opt/51xit /opt/52xit   ###创建51和52目录
[root@localhost ~]# echo "this is www.51xit.com" >/opt/51xit/index.html  ###建一个51xit首页并写入东西
[root@localhost ~]# echo "this is www.52xit.com" >/opt/52xit/index.html  ###建一个52xit首页并写入东西
[root@localhost ~]# vi /etc/exports   ###发布出去
/opt/51xit 192.168.100.0/24 (rw,sync)   
/opt/52xit 192.168.100.0/24 (rw,sync)

[root@localhost ~]# systemctl start rpcbind   ###启动rpcbind 先启动rpcbind在启动nfs
[root@localhost ~]# systemctl start nfs 
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl enable rpcbind
######编译安装Nginx服务器1  192.168.100.22 ######
1、编译安装 Nginx
Nginx 安装文件可以从官方网站 http://www.nginx.org/下载。
下面以稳定版 Nginx 1.12.2为例   上传至/opt下
[root@localhost ~]#yum -y install pcre-devel zlib-devel gcc-c++   ### 依赖环境装一下
[root@localhost ~]# useradd -M -s /sbin/nologin nginx       #### 创建账户
[root@localhost ~]# cd /opt
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]# 
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx

[root@localhost nginx-1.12.2]# make && make install   ###编译安装

[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.2]# nginx                 ### 启动nginx
[root@localhost nginx-1.12.2]# netstat -anutp |grep nginx        ### 查看启动情况
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      21135/nginx: master 

■启动、 停止 Nginx
[root@localhost nginx-1.12.2]# killall -3 nginx        ###停止服务    -1是安全重启
如果出现: -bash: killall: command not found

[root@localhost nginx-1.12.2]#  yum -y install psmisc    ###如果出现上面报错就代表没有安装killall功能,yum安装一下
[root@localhost nginx-1.12.2]# killall -3 nginx    ###然后停止nginx服务
[root@localhost nginx-1.12.2]# nginx -t                 ###对配置文件检查一下
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

■添加 Nginx 系统服务
[root@localhost ~]# vim /lib/systemd/system/nginx.service  ###注意目录别进错了
[Unit]
Description=nginx                                                 ####描述
After=network.target                                            ####描述服务类别
[Service]
Type=forking                                                          ####后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid                ####PID 文件位置
ExecStart=/usr/local/nginx/sbin/nginx                  ####启动服务
ExecReload=/usr/bin/kill -s HUP $MAINPID         ####根据 PID 重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID           ####根据 PID 终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target

################下面是刷的#############
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking 
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

[root@localhost nginx-1.12.2]# systemctl start nginx       ### 启动nginx
[root@localhost nginx-1.12.2]# systemctl enable nginx    ### 开机自启nginx
[root@localhost nginx-1.12.2]# chmod 754 /lib/systemd/system/nginx.service   ### 权限也要给一下

[root@localhost nginx-1.12.2]# yum -y install nfs-utils  ### 把nfs装一下,有的已经装了的就不用装了
[root@localhost nginx-1.12.2]# showmount -e 192.168.100.24    ###测试一下

[root@localhost ~]# mount 192.168.100.24:/opt/51xit    /usr/local/nginx/html/  ###临时挂载,可以直接永久挂载

[root@localhost ~]# vi /etc/fstab             ###编辑永久挂载
192.168.100.24:/opt/51xit/ /usr/local/nginx/html/    nfs    defaults,_netdev 0 0        ###开机自动挂载,注意格式对齐
[root@localhost nginx-1.12.2]# init6  ###重启服务器
[root@localhost nginx-1.12.2]# systemctl restart nginx       ###重启nginx
######编译安装Nginx服务器2  192.168.100.23 ######
1、编译安装 Nginx
Nginx 安装文件可以从官方网站 http://www.nginx.org/下载。
下面以稳定版 Nginx 1.12.2为例   上传至/opt下
[root@localhost ~]#yum -y install pcre-devel zlib-devel gcc-c++   ### 依赖环境装一下
[root@localhost ~]# useradd -M -s /sbin/nologin nginx       #### 创建账户
[root@localhost ~]# cd /opt
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]# 
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx

[root@localhost nginx-1.12.2]# make && make install   ###编译安装

[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.2]# nginx                 ### 启动nginx
[root@localhost nginx-1.12.2]# netstat -anutp |grep nginx        ### 查看启动情况
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      21135/nginx: master 

■启动、 停止 Nginx
[root@localhost nginx-1.12.2]# killall -3 nginx        ###停止服务    -1是安全重启
如果出现: -bash: killall: command not found

[root@localhost nginx-1.12.2]#  yum -y install psmisc    ###如果出现上面报错就代表没有安装killall功能,yum安装一下
[root@localhost nginx-1.12.2]# killall -3 nginx    ###然后停止nginx服务
[root@localhost nginx-1.12.2]# nginx -t                 ###对配置文件检查一下
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

■添加 Nginx 系统服务
[root@localhost ~]# vim /lib/systemd/system/nginx.service  ###注意目录别进错了
[Unit]
Description=nginx                                                 ####描述
After=network.target                                            ####描述服务类别
[Service]
Type=forking                                                          ####后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid                ####PID 文件位置
ExecStart=/usr/local/nginx/sbin/nginx                  ####启动服务
ExecReload=/usr/bin/kill -s HUP $MAINPID         ####根据 PID 重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID           ####根据 PID 终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target

################下面是刷的#############
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking 
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

[root@localhost nginx-1.12.2]# systemctl start nginx       ### 启动nginx
[root@localhost nginx-1.12.2]# systemctl enable nginx    ### 开机自启nginx
[root@localhost nginx-1.12.2]# chmod 754 /lib/systemd/system/nginx.service   ### 权限也要给一下

[root@localhost nginx-1.12.2]# yum -y install nfs-utils  ### 把nfs装一下,有的已经装了的就不用装了
[root@localhost nginx-1.12.2]# showmount -e 192.168.100.24    ###测试一下

[root@localhost ~]# mount 192.168.100.24:/opt/51xit    /usr/local/nginx/html/  ###临时挂载,可以直接永久挂载

[root@localhost ~]# vi /etc/fstab             ###编辑永久挂载
192.168.100.24:/opt/52xit/ /usr/local/nginx/html/    nfs    defaults,_netdev 0 0        ###开机自动挂载,注意格式对齐
[root@localhost nginx-1.12.2]# init 6  ###重启服务器
[root@localhost nginx-1.12.2]# systemctl restart nginx       ###重启nginx
#####配置Haproxy 服务器  192.168.100.21  #####
1、编译安装 Haproxy  
上传 haproxy-1.4.24.tar.gz 到/opt目录下
[root@localhost ~]# yum -y install pcre-devel bzip2-devel gcc gcc-c++
[root@localhost ~]# cd /opt
[root@localhost opt]# tar xzvf haproxy-1.4.24.tar.gz 
[root@localhost opt]# cd haproxy-1.4.24/
[root@localhost haproxy-1.4.24]# make TARGET=linux26
[root@localhost haproxy-1.4.24]# make install

2、配置Haproxy 服务
[root@localhost haproxy-1.4.24]# mkdir /etc/haproxy
[root@localhost haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg 
global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        #log loghost    local0 info
        maxconn 4096
        #chroot /usr/share/haproxy
        uid 99
        gid 99
        daemon
        #debug
        #quiet

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        #redispatch
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

listen  webcluster 0.0.0.0:80
        option httpchk GET /index.html
        balance roundrobin
        server inst1 192.168.100.22:80 check inter 2000 fall 3
        server inst2 192.168.100.23:80 check inter 2000 fall 3



[root@localhost haproxy-1.4.24]# cp examples/haproxy.init /etc/init.d/haproxy   ###拷贝一下
[root@localhost haproxy-1.4.24]# chmod 755 /etc/init.d/haproxy   ###给权限
[root@localhost haproxy-1.4.24]# chkconfig --add haproxy   ###把这个服务加到系统里
[root@localhost haproxy-1.4.24]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy   ###创建软连接
[root@localhost haproxy-1.4.24]# service haproxy start  ###启动一下
[root@localhost haproxy-1.4.24]# systemctl stop haproxy.service   ###另一种启动方法 先关闭
[root@localhost haproxy-1.4.24]# systemctl start haproxy.service    ###另一种启动方法 在启动
##########测试网站#######
浏览器输入192.168.100.21    刷新会发现来回切换两个网站页面,说明已经实现了负载均衡

Note: Do not use version 7.4 as a storage server, there will be bugs that cannot be shared! ! !

Guess you like

Origin blog.csdn.net/weixin_44733021/article/details/108782388