Haproxy支持多种调度算法,最常用的有三种
RR算法是最简单最常用的一种算法,即轮询调度
理解举例:
◆有三个节点A、B、C
◆第一个用户访问会被指派到节点A
◆第二个用户访问会被指派到节点B
◆第三个用户访问会被指派到节点C
◆第四个用户访问继续指派到节点A,轮询分配访问请求实现负载均衡效果
最小连接数算法,根据后端的节点连接数大小动态分配前端请求
理解举例:
◆ 有三个节点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算法有很大改进,是目前用到比较多的一种算法
基于来源访问调度算法,用于一些有Session会话记录在服务器端的场景,可以基于来源的IP、Cookie等做集群调度
理解举例:
◆ 有三个节点A、B、C,第一个用户第一次访问被指派到了A,第二个用户第一次访问被指派到了B
◆ 当第一个用户第二次访问时会被继续指派到A,第二个用户第二次访问时依旧会被指派到B,只要负载均衡调度器不重启,第一个用户访问都会被指派到A,第二个用户访问都会被指派到B,实现集群的调度
◆ 此调度算法好处是实现会话保持,但某些IP访问量非常大时会引起负载不均衡部分节点访问量超大,影响业务使用
安装步骤
Haproxy配置文件通常分为三个部分
global配置参数
参数 | 说明 |
---|---|
log 127.0.0.1 local0 | 配置日志记录,local0为日志设备,默认存放到系统日志 |
log 127.0.0.1 local1 | noticenotice为日志级别,通常有24个级别 |
maxconn 4096 | 最大连接数 |
uid 99 | 用户uid |
gid 99 | 用户gid |
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.30.44 netmask 255.255.255.0 broadcast 192.168.30.255
inet6 fe80::a52a:406e:6512:1c66 prefixlen 64 scopeid 0x20<link>
[root@localhost ~]# route -n //查看路由表,看网关
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.30.11 0.0.0.0 UG 100 0 0 ens33
192.168.30.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@localhost ~]# rpm -q nfs-utils //查看nfs是否安装
nfs-utils-1.3.0-0.61.el7.x86_64
[root@localhost ~]# rpm -q rpcbind //查看rpcbind是否安装
rpcbind-0.2.0-47.el7.x86_64
[root@localhost ~]# yum -y install nfs-utils //确实安装了
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Package 1:nfs-utils-1.3.0-0.61.el7.x86_64 already installed and latest version
Nothing to do
[root@localhost ~]# yum -y install rpcbind
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Package rpcbind-0.2.0-47.el7.x86_64 already installed and latest version
Nothing to do
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# vi /etc/exports
/opt/web1 192.168.30.0/24(rw,sync) //注意,这里的ip与授权的“()”之间不能有空格
/opt/web2 192.168.30.0/24(rw,sync)
[root@localhost ~]# systemctl restart nfs
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# showmount -e //查看共享目录
Export list for localhost.localdomain:
/opt/web2 192.168.30.0/24
/opt/web1 192.168.30.0/24
[root@localhost web2]# exportfs -vr //发布共享目录
exporting 192.168.30.0/24:/opt/web2
exporting 192.168.30.0/24:/opt/web1
[root@localhost ~]# mkdir /opt/web1/ /opt/web1/
[root@localhost ~]# vi /opt/web1/index.html
<html>
<title>I'm Web1</title>
<body><h1>I'm Web1</h1></body>
<img src="web1.jpg" />
</html>
[root@localhost ~]# vi /opt/web2/index.html
<html>
<title>I'm Web2</title>
<body><h1>I'm Web2</h1></body>
<img src="web2.png" />
</html>
[root@localhost nginx-1.15.9]# tar xzvf nginx-1.15.9.tar.gz -C /opt
[root@localhost system]# useradd -s /sbin/nologin -M nginx //-M不创建家目录
[root@localhost opt]# yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl \
zlib-devel
[root@localhost nginx-1.15.9]# cd /opt/nginx-1.15.9/
[root@localhost nginx-1.15.9]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@localhost nginx-1.15.9]# make && make install
[root@localhost nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.15.9]# killall -s QUIT nginx
[root@localhost nginx-1.15.9]# cd /lib/systemd/system/
[root@localhost system]# vim nginx.service //注意,配置执行服务文件前关闭nginx进程
[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/ki11 -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target //允许多用户
[root@localhost system]# chmod 754 nginx.service //给权限,让组用户也可以执行
[root@localhost system]# systemctl start nginx.service
[root@localhost system]# systemctl enable nginx.service
[root@localhost system]# systemctl status nginx.service
● nginx.service - nginx
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2020-09-06 05:20:11 CST; 1s ago
Process: 13374 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
……省略部分
[root@localhost system]# yum -y install nfs-utils rpcbind
[root@localhost system]# showmount -e 192.168.30.44
Export list for 192.168.30.44:
/opt/web2 192.168.30.0/24
/opt/web1 192.168.30.0/24
[root@localhost system]# mount 192.168.30.44:/opt/web1 /usr/local/nginx/html/
[root@localhost system]# vi /etc/fstab
192.168.30.44:/opt/web1 /usr/local/nginx/html nfs defaults,_netdev 0 0
[root@localhost nginx-1.15.9]# tar xzvf nginx-1.15.9.tar.gz -C /opt
[root@localhost system]# useradd -s /sbin/nologin -M nginx //-M不创建家目录
[root@localhost opt]# yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl \
zlib-devel
[root@localhost nginx-1.15.9]# cd /opt/nginx-1.15.9/
[root@localhost nginx-1.15.9]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@localhost nginx-1.15.9]# make && make install
[root@localhost nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.15.9]# killall -s QUIT nginx
[root@localhost nginx-1.15.9]# cd /lib/systemd/system/
[root@localhost system]# vim nginx.service //注意,配置执行服务文件前关闭nginx进程
[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/ki11 -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target //允许多用户
[root@localhost system]# chmod 754 nginx.service //给权限,让组用户也可以执行
[root@localhost system]# systemctl start nginx.service
[root@localhost system]# systemctl enable nginx.service
[root@localhost system]# systemctl status nginx.service
● nginx.service - nginx
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2020-09-06 05:20:11 CST; 1s ago
Process: 13374 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
……省略部分
[root@localhost system]# yum -y install nfs-utils rpcbind
[root@localhost system]# showmount -e 192.168.30.44
Export list for 192.168.30.44:
/opt/web2 192.168.30.0/24
/opt/web1 192.168.30.0/24
[root@localhost system]# mount 192.168.30.44:/opt/web2 /usr/local/nginx/html/
[root@localhost system]# vi /etc/fstab
192.168.30.44:/opt/web2 /usr/local/nginx/html nfs defaults,_netdev 0 0
[root@localhost ~]# yum -y install pcre-devel bzip2-devel gcc gcc-c++ //安装环境
[root@localhost ~]# tar xzvf haproxy-1.4.24.tar.gz -C /opt
[root@localhost ~]# cd /opt
[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
# this config needs haproxy-1.1.28 or haproxy-1.2.1
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 appli1-rewrite 0.0.0.0:80
# cookie SERVERID rewrite
balance roundrobin
server inst1 192.168.30.22:80 check inter 2000 fall 3
server inst2 192.168.30.33:80 check inter 2000 fall 3
###############################上述配置文件解释#############################
(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:定义备份节点
[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]# chkconfig --list haproxy
Note: This output shows SysV services only and does not include native
……省略部分
haproxy 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[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
[root@localhost haproxy-1.4.24]# netstat -anupt |grep haproxy
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 20379/haproxy
udp 0 0 0.0.0.0:46295 0.0.0.0:* 20379/haproxy
4.测试haproxy轮询
[root@localhost ~]# vi /etc/haproxy/haproxy.cfg
global
log /dev/log local0 //将log有关的配置换成这两条
log /dev/log local1 notice
……省略部分
[root@localhost ~]# systemctl restart haproxy.service //重启haproxy服务
[root@localhost ~]# 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 ~]# systemctl restart rsyslog.service
[root@localhost ~]# tail -f /var/log/haproxy/haproxy-info.log
Sep 25 02:19:05 localhost haproxy[21757]: 192.168.30.1:53145 [25/Sep/2020:02:18:51.585] webcluster webcluster/inst1 0/0/0/1/13874 304 1083 - - CD-- 2/2/0/0/0 0/0 "GET / HTTP/1.1"
Sep 25 02:20:09 localhost haproxy[21757]: 192.168.30.1:53212 [25/Sep/2020:02:19:16.966] webcluster webcluster/inst2 0/0/2/1/52421 404 3550 - - cD-- 2/2/1/0/0 0/0 "GET /favicon.ico HTTP/1.1"
Sep 25 02:20:10 localhost haproxy[21757]: 192.168.30.1:53233 [25/Sep/2020:02:19:20.189] webcluster webcluster/inst1 0/0/1/1/50248 200 637 - - cD-- 0/0/0/0/0 0/0 "GET / HTTP/1.1"
随着企业网站负载增加,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左右就可以了 |