Haproxy 搭建web 群集(理论+实战部署)

一、Haproxy 应用分析

1.1、常见的Web 集群调度器

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

1.2、Haproxy 应用分析

(1)LVS虽然在企业应用中抗负载能力很强,但存在不足

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

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

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

二、Haproxy 调度算法

Haproxy支持多种调度算法,最常用的有三种:RR(Round Robin),LC(Least Connections),SH(Source Hashing)

2.1、RR(Round Robin)

RR算法是最简单最常用的一种算法,即轮询调度

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

2.2、LC(Least Connections)

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

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

2.3、SH(Source Hashing)

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

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

三、Haproxy 群集配置

3.1、实验环境

VMware软件
两台centos7.6虚拟机作为Nginx服务器(IP地址:192.168.100.22;IP地址:192.168.100.23)
一台centos7.6虚拟机作为Haproxy服务器(IP地址:192.168.100.21)
一台centos7.6虚拟机作为存储服务器 (IP地址:192.168.100.24)

3.2、配置存储服务器

首先查看nfs-utils 和rpcbind 是否安装,若没有用yum安装即可
安装好后启动两个服务

[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# mkdir /opt/51xit /opt/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 restart rpcbind
[root@localhost ~]# systemctl restart nfs
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# echo "this is www.51xit.top" > /opt/51xit/index.html
[root@localhost ~]# echo "this is www.52xit.top" > /opt/52xit/index.html

3.3、配置nginx 服务器

3.3.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 -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
[root@localhost nginx-1.12.2]# nginx
[root@localhost nginx-1.12.2]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      25182/nginx: master 

两台nginx 服务器同样步骤编译安装nginx服务

3.3.2、安装httpd 挂载测试页

192.168.100.22

[root@localhost ~]# showmount -e 192.168.100.24
Export list for 192.168.100.24:
/opt/52xit 192.168.100.0/24
/opt/51xit 192.168.100.0/24
[root@localhost ~]# mount 192.168.100.24:/opt/51xit /usr/local/nginx/html/
[root@localhost ~]# vi /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Aug  6 12:23:03 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=a1c935eb-f211-43a5-be35-2a9fef1f6a89 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/cdrom /mnt iso9660 defaults 0 0
192.168.100.24:/opt/51xit/ /usr/local/nginx/html/ nfs defaults,_netdev 0 0
[root@localhost ~]# killall -1 nginx

测试登录是否正常
在这里插入图片描述
192.168.100.23

[root@localhost ~]# showmount -e 192.168.100.24
Export list for 192.168.100.24:
/opt/52xit 192.168.100.0/24
/opt/51xit 192.168.100.0/24
[root@localhost ~]# mount 192.168.100.24:/opt/52xit /usr/local/nginx/html/
[root@localhost ~]# vi /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Aug  6 12:23:03 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=a1c935eb-f211-43a5-be35-2a9fef1f6a89 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/cdrom /mnt iso9660 defaults 0 0
192.168.100.24:/opt/52xit/ /usr/local/nginx/html/ nfs defaults,_netdev 0 0
[root@localhost ~]# killall -1 nginx

测试登录是否正常
在这里插入图片描述

3.4、配置Haproxy 服务器

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

3.4.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]# systemctl start haproxy.service

3.5、验证

真机浏览器输入192.168.100.21
在这里插入图片描述

过一分钟重新输入
在这里插入图片描述
会发现不同的网站页面 说明已经实现了负载均衡

3.6、Haproxy 配置文件详解

1、Haproxy配置文件通常分为三个部分
global:为全局配置
defaults:为默认配置
listen:为应用组件配置
global配置参数
log 127.0.0.1 local0:配置日志记录,配置日志记录,local0为日志设备,默认存放到系统日志
log 127.0.0.1 local1 notice:notice为日志级别,通常有24个级别
maxconn 4096:最大连接数
uid 99:用户uid gid 99:用户gid

2、defaults配置项配置默认参数,一般会被应用组件继承,如果在应用组件中没有特别声明,将安装默认配置参数设置
log global:定义日志为global配置中的日志定义
mode http:模式为http
option httplog:采用http日志格式记录日志
option dontlognull :保证HAProxy不记录上级负载均衡发送过来的用于检测状态没有数据的心跳包
retries 3:检查节点服务器失败连续达到三次则认为节点不可用
maxconn 2000:最大连接数
contimeout 5000:连接超时时间
clitimeout 50000:客户端超时时间
srvtimeout 50000:服务器超时时间

3、listen配置项目一般为配置应用模块参数
listen appli4-backup 0.0.0.0:10004:定义一个appli4-backup的应用
option httpchk /index.html:检查服务器的index.html文件
option persist :强制将请求发送到已经down掉的服务器
balance roundrobin:负载均衡调度算法使用轮询算法
server inst1 192.168.114.56:80 check inter 2000 fall 3:定义在线节点
server inst2 192.168.114.56:81 check inter 2000 fall 3 backup:定义备份节点

四、Haproxy 日志管理

Haproxy的日志默认是输出到系统的 syslog中,在生产环境中一般单独定义出来

定义的方法步骤:
修改 Haproxy配置文件中关于日志配置的选项,加入配置:
log /dev/log local0 info
log /dev/log local0 notice
修改 rsyslog配置,将 Haproxy相关的配置独立定义到
haproxy.conf,并放到/etc/rsyslog.d/下
保存配置文件并重启 rsyslog服务,完成 rsyslog配置

[root@localhost haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg	'//编辑haproxy配置文件'
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log /dev/log    local0 info	
        log /dev/log    local1 notice
    ...省略内容
[root@localhost haproxy-1.4.24]# systemctl restart haproxy.service	'//重启haproxy服务'
[root@localhost haproxy-1.4.24]# touch /etc/rsyslog.d/haproxy.conf	'//创建一个新haproxy配置文件'
[root@localhost haproxy-1.4.24]# vi /etc/rsyslog.d/haproxy.conf 	'//编写haproxy配置文件脚本'
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 haproxy-1.4.24]# vi /etc/rsyslog.d/haproxy.conf systemctl restart rsyslog.service 	'//重启日志服务'

访问 Haproxy集群测试网页并测试日志信息

'//未访问网页,查看/var/log'
[root@localhost ~]# cd /var/log
[root@localhost log]# ls
发现没有haproxy文件
'//查看网页之后,再次查看/var/log'
[root@localhost log]# ls
已经生成haproxy文件了,可以进去查看
[root@localhost log]# cd haproxy/
[root@localhost haproxy]# ls
haproxy-info.log
[root@localhost haproxy]# cat haproxy-info.log 

五、Haproxy 参数优化

随着企业网站负载增加, haproxy参数优化相当重要

•maxconn:最大连接数,根据应用实际情况进行调整,推荐使用10 240

•daemon:守护进程模式, Haproxy可以使用非守护进程模式启动,建议使用守护进程模式启动

•nbproc:负载均衡的并发进程数,建议与当前服务器CPU核数相等或为其2倍
retries:重试次数,主要用于对集群节点的检查,如果节点多,且并发量大,设置为2次或3次

•option http-server-close:主动关闭http请求选项,建议在生产环境中使用此选项

•timeout http-keep-alive:长连接超时时间,设置长连接超时时间,可以设置为10s

•timeout http-request:http请求超时时间,建议将此时间设置为5~10s,增加http连接释放速度

•timeout client:客户端超时时间,如果访问量过大,节点响应慢,可以将此时间设置短一些,建议设置为1min左右就可以了

猜你喜欢

转载自blog.csdn.net/weixin_48191211/article/details/108776372