理论+实验:Haproxy搭建Web群集

一、常见的WEB集群调度器

  • 目前常见的WEB集群调度器分为软件和硬件
  • 软件通常使用LVS、Haproxy、Nginx
  • 硬件一般使用比较多的是F5,也有很多人使用国内的一些产品,如梭子鱼、绿盟等。

二、Haproxy应用分析

■LVS在企业应用中抗负载能力很强,但存在不足

  • LVS不支持正则处理,不能实现动静分离
  • 对于大型网站,LVS的实施配置复杂,维护成本相对于较高

■Haproxy是一款可提供高可用性、负载均衡、及基于TCP和HTTP应用代理的软件

  • 适用于负载大的web站点
  • 运行在硬件上可支持数以万计的并发连接的连接请求

三、Haproxy调度算法原理

3.1、RR (Round Robin)

■ Haproxy支持多种调度算法,最常用的有三种

  • RR (Round Robin)
    ◆ RR算法是最简单最常用的一种算法,即轮询调度

  • 理解举例
    ◆ 有三个节点A、B、C
    ◆ 第一个用户访问会被指派到节点A
    ◆ 第二个用户访问会被指派到节点B
    ◆ 第三个用户访问会被指派到节点C
    ◆ 第四个用户访问继续指派到节点A,轮询分配访问请求实现负载均衡效果

3.2、LC (Least Connections)

■ LC (Least Connections)

  • 最小连接数算法,根据后端的节点连接数大小动态分配前端请求

■ 理解举例

  • 有三个节点A、B、C,各节点的连接数分别为A:4、B:5、C:6
  • 第一个用户连接请求,会被指派到A上,连接数变为A:5、B:5、C:6
  • 第二个用户请求会继续分配到A上,连接数变为A:6、B:5、C:6;再有新的请求会分配给B,每次将新的请求指派给连接数最小的客户端
  • 由于实际情况下A、B、C的连接数会动态释放,很难会出现一样连接数的情况
  • 此算法相比较rr算法有很大改进,是目前用到比较多的一种算法

3.3、SH(Source Hashing)

■ SH(Source Hashing)

  • 基于来源访问调度算法,用于一些有Session会话记录在服务器端的场景,可以基于来源的IP、Cookie等做集群调度

■理解举例

  • 有三个节点A、B、C,第一个用户第一次访问被指派到了A,第二个用户第一次访问被指派到了B
  • 当第一个用户第二次访问时会被继续指派到A,第二个用户第二次访问时依旧会被指派到B,只要负载均衡调度器不重启,第一个用户访问都会被指派到A,第二个用户访问都会被指派到B,实现集群的调度
  • 此调度算法好处是实现会话保持,但某些IP访问量非常大时会引起负载不均衡,部分节点访问量超大,影响业务使用

四、使用Haproxy搭建Web群集

4.1、实验配置
###########  使用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    刷新会发现来回切换两个网站页面,说明已经实现了负载均衡

注意:不要用7.4版本的做存储服务器,会出现bug不能共享!!!

猜你喜欢

转载自blog.csdn.net/weixin_44733021/article/details/108782388