Haproxy搭建Web群集——理论知识与实验部署

Haproxy介绍

1、Haproxy是目前比较流行的一种集群调度工具

2、Haproxy 与LVS、Nginx的比较LVS性能最好,但是搭建相对复杂

3、Nginx的upstream模块支持集群功能,但是对集群节点健康检查功能不强,性能没有Haproxy好

案例前置知识点

◆请求方式
GET方式:从指定的服务器上获取数据
GET请求能被缓存
GET请求会被保存在浏览器记录里
GET请求有长度的限制
主要用于获取数据
查询的字符串会显示在URL中,不安全

###############################################################################

POST方式:提交数据给指定的服务器处理
POST请求不能被缓存
POST请求不会保存在浏览器记录里
POST请求没有长度限制
查询字符串不会显示在URL中,比较安全

###############################################################################

◆返回状态码
正常的状态码为2××、3××
异常的状态码为4××、5××

Haproxy参数优化

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

maxconn: 最大连接数,根据应用实际情况进行调整,推荐使用10240
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左右就可以了

Haproxy调度算法原理

◆RR(Round Robin):轮询调度
有三个节点A、B、C 第一个用户会被指派到节点A,第二个用户会被指派到节点B,第三个
用户会被直白到节点C,第四个用户会被指派到节点A,轮询分配访问,请求实现负载均衡效果,
此算法还有一种加权轮询,即根据每个节点的权重轮询分配请求。

#########################################################################

◆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:6 B:6 C:6
此算法比较RR有很大的改进,是目前用的比较多的算法
◆SH(Source Hashing):基于来源访问调度
此算法用于一些有Session回话记录在服务器端的场景,可以基于源IP,cookie等做群集调度,
列如,使用基于源IP的调度算法,有三个节点A、B、C,
第一个用户第一次访问被指派到A,第二个用户第一次访问被指派到B,当地一个用户第二次访问
时都会被指派到A,第二个用户访问都会被指派到B。实现群集的调度。此调度算法好处会实现会话保
但某些IP访问量非常大的时候会引起负载不均衡,部分节点访问量超大,影响业务使用。

常见的WEB群集调度器

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

使用Haproxy搭建Web群集

网络环境

场景介绍:
主机 操作系统 IP地址 主要软件
Haproxy服务器 CentoS7.6 192.168.100.21 haproxy-1.5.19.tar.gz
Nginx服务器1 CentoS7.6 192.168.100.23 Nginx-1.12.2.tar.gz
Nginx服务器2 CentoS7.6 192.168.100.24 Nginx-1.12.2.tar.gz
存储服务器 CentoS7.6 192.168.100.25 nfs-utils rpcbind

调试存储服务器 192.168.100.25

rpm -q nfs-utils    ###如果没装,yum -y install nfs-utils
rpm -q rpcbind      ###如果没装,yum -y install rpcbind
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl start rpcbind

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

编译安装Nginx服务器(1) 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]# ls -l /usr/local/sbin/nginx
lrwxrwxrwx 1 root root 27 516 16:50 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx


2、Nginx 的运行控制
[root@localhost ~]# 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

###停止服务
killall -3 nginx  

如果出现: -bash: killall: command not found
yum -y install psmisc  出现上面的报错安装这个


yum -y install net-tools     给netstata使用,最小安装需要
[root@localhost ~]# nginx                                ####启动
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
7180/nginx: master

##添加 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
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 ~]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost ~]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to
/usr/lib/systemd/system/nginx.service.

killall -3 nginx                             
systemctl stop nginx.service
systemctl start nginx.service

##上面命令一刷,下面就可以正常使用了##
systemctl start nginx.service
systemctl stop nginx.service
systemctl reload nginx.service
systemctl restart nginx.service
systemctl status nginx.service

3、安装httpd 挂载测试页
[root@localhost ~]# showmount -e 192.168.100.25  
Export list for 192.168.100.25:
/opt/51xit 192.168.100.0/24(rw,sync)
/opt/52xit 192.168.100.0/24(rw,sync)

[root@localhost ~]# mount 192.168.100.25:/opt/51xit /usr/local/nginx/html/
[root@localhost ~]# vi /etc/fstab 
192.168.100.44:/opt/51xit/ /usr/local/nginx/html/ nfs     rw,tcp,intr     0 1 

[root@localhost nginx-1.12.2]# systemctl restart nginx

编译安装Nginx服务(2) 192.168.100.24

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 opt]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost opt]# 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]# ls -l /usr/local/sbin/nginx
lrwxrwxrwx 1 root root 27 516 16:50 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx


2、Nginx 的运行控制
[root@localhost ~]# 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

###停止服务
killall -3 nginx  

如果出现: -bash: killall: command not found
yum -y install psmisc  出现上面的报错安装这个


yum -y install net-tools     给netstata使用,最小安装需要
[root@localhost ~]# nginx                                ####启动
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
7180/nginx: master

##添加 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
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 ~]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost ~]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to
/usr/lib/systemd/system/nginx.service.

killall -3 nginx                             
systemctl stop nginx.service
systemctl start nginx.service

##上面命令一刷,下面就可以正常使用了##
systemctl start nginx.service
systemctl stop nginx.service
systemctl reload nginx.service
systemctl restart nginx.service
systemctl status nginx.service

3、安装httpd 挂载测试页
[root@localhost ~]# showmount -e 192.168.100.25  
Export list for 192.168.100.25:
/opt/51xit 192.168.100.0/24(rw,sync)
/opt/52xit 192.168.100.0/24(rw,sync)

[root@localhost ~]# mount 192.168.100.25:/opt/52xit /usr/local/nginx/html/
[root@localhost ~]# vi /etc/fstab 
192.168.100.44:/opt/51xit/ /usr/local/nginx/html/ nfs     rw,tcp,intr     0 1 

[root@localhost nginx-1.12.2]# systemctl restart 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.23:80 check inter 2000 fall 3
        server inst2 192.168.100.24: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
Starting haproxy (via systemctl):                          [  OK  ]

[root@localhost haproxy-1.4.24]# systemctl stop haproxy.service 

[root@localhost haproxy-1.4.24]# systemctl start haproxy.service 

配置文件解析

####上述配置文件解释###
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:定义备份节点

实验认证

192.168.100.21 切换 会发现不同的网站页面 说明已经实现了负载均衡
第一次访问后,清除历史记录后需等待5分钟,不会立马切换
在这里插入图片描述
在这里插入图片描述

Haproxy日志

[root@localhost haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg
global                       ####下面log开头的 统统去掉换下面的配置
        log /dev/log    local0 info
        log /dev/log    local1 notice
[root@localhost haproxy-1.4.24]# systemctl restart haproxy.service        
        

[root@localhost haproxy-1.4.24]# touch /etc/rsyslog.d/haproxy.conf
[root@localhost haproxy-1.4.24]# vi /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
& ~

[root@localhost haproxy-1.4.24]#  systemctl restart rsyslog.service 

[root@localhost haproxy-1.4.24]# tail -f /var/log/haproxy/haproxy-info.log 
Sep 27 00:08:33 localhost haproxy[13864]: 192.168.32.1:57172 [27/Sep/2018:00:07:43.232] webcluster webcluster/inst1 1/0/1/2/50006 404 726 - - cD-- 2/2/1/0/0 0/0 "GET /favicon.ico HTTP/1.1"
Sep 27 00:08:40 localhost haproxy[13864]: 192.168.32.1:57223 [27/Sep/2018:00:07:47.477] webcluster webcluster/inst2 0/0/1/2/52806 200 750 - - cD-- 0/0/0/0/0 0/0 "GET / HTTP/1.1"


网页打开:192.168.100.21网站 然后查看日志文件不断生成情况

实验排错集

#(1)当你在安装Nginx后
#killall -3 nginx  想使用systemctl进行管理时,不可以直接使用,需要操作流程如下
killall -3 nginx                             
systemctl stop nginx.service
systemctl start nginx.service
#不然会一直报错


#(2)Haproxy的配置文件
#将之前的删除添加我博客上面的就可以了,注意IP地址不要填写错误

#(3)关于验证
#跟之前的有点不同,不是清理了浏览器记录,就可以直接再访问看是否轮询了
#需要等待一段时间不超过5分钟,因为Nginx是有默认缓存时间的,所以要耐心
#当然若你一直不轮询就要回头去检查Haproxy的配置文件了

猜你喜欢

转载自blog.csdn.net/weixin_48190891/article/details/108798417