一、熟悉Haproxy功能及常用群集调度算法
Haproxy 是目前比较流行的一种群集调度工具,同类群集调度工具有很多,如 LVS 和Nginx。相比较而言,LVS 性能最好,但是搭建相对复杂;Nginx的upstream 模块支持群集功能,但是对群集节点健康检查功能不强,高并发性能没有 Haproxy 好。Haproxy 官方网站是http:///
1. HTTP 请求
通过 URL 访问网站使用的协议是 HTTP 协议,此类请求一般称为 HTTP 请求。HTTP 请求的方式分为 GET方式和 POST方式。当使用浏览器访问某一个 URL,会根据请求 URL 返回状态码,通常正常的状态码为 2xx、3xx(如 200、301),如果出现异常会返回 4xx、5xx(如 400、500)。
2.负载均衡常用调度算法
LVS、Haproxy、Nginx 最常用的调度算法有三种,如下所述:
(1)RR(Round Robin)。RR 算法是最简单最常用的一种算法,即轮询调度。此算法还有一种加权轮询,即根据每个节点的权重轮询分配访问请求。
(2)LC(Least Connections)。LC算法即最小连接数算法,根据后端的节点连接数大小动态分配前端请求。此算法相比较rr 算法有很大改进,是目前用到比较多的一种算法。
(3)SH(Source Hashing)。SH 即基于来源访问调度算法,此算法用于一些有 Session会话记录在服务器端的场景,可以基于来源的 IP、Cookie 等做群集调度。此调度算法好处是实现会话保持,但某些IP访问量非常大时会引起负载不均衡,部分节点访问量超大,影响业务使用。
3.常见的 Web 群集调度器
目前,常见的 Web 群集调度器分为软件和硬件。软件通常使用开源的LVS、Haproxy、Nginx,硬件一般使用比较多的是 F5。也有很多人使用国内的一些产品,如梭子鱼、绿盟等
二、案例
1.编译安装nginx服务器
两个nginx网站的步骤完全一样,只有测试页面不同
[root@localhost ~]# yum -y install pcre-devel zlib-devel gcc*
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tar zxvf nginx-1.12.0.tar.gz
[root@localhost ~]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.12.0]# make && make install
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.0]# cd /usr/local/nginx/html/
[root@localhost html]# echo "test web01" > test.html
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# nginx -t
[root@localhost ~]# nginx ##开启nginx进程
[root@localhost ~]# netstat -anpt | grep nginx
2.编译安装Haproxy
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install pcre-devel bzip2-devel gcc*
[root@localhost ~]# tar zxvf haproxy-1.5.19.tar.gz
[root@localhost ~]# cd haproxy-1.5.19/
[root@localhost haproxy-1.5.19]# make TARGET=linux26
[root@localhost haproxy-1.5.19]# make install
注释:
- linux22 for Linux 2.2
- linux24 for Linux 2.4 and above (default)
- linux24e for Linux 2.4 with support for a working epoll (> 0.21)
- linux26 for Linux 2.6 and above
- solaris for Solaris 8 or 10 (others untested)
- freebsd for FreeBSD 5 to 8.0 (others untested)
- openbsd for OpenBSD 3.1 to 4.6 (others untested)
- cygwin for Cygwin
- generic for any other OS.
- custom to manually adjust every setting
3.Haproxy服务器配置
(1)建立haproxy配置文件
[root@localhost haproxy-1.5.19]# mkdir /etc/haproxy
[root@localhost haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/
(2)创建服务脚本
[root@localhost haproxy-1.5.19]# cp examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy-1.5.19]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost haproxy-1.5.19]# chmod +x /etc/init.d/haproxy
[root@localhost ~]# chkconfig --add haproxy
(3)Haproxy配置介绍
[root@localhost haproxy-1.5.19]# 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
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.1.61:80 check inter 2000 fall 3
server inst2 192.168.1.62:80 check inter 2000 fall 3
可在每个服务器后加weight设置权重值
server inst1 192.168.1.61:80 check inter 2000 fall 3 weight 1
server inst2 192.168.1.62:80 check inter 2000 fall 3 weight 2
各个语句的解释:
global
log 127.0.0.1 local0 \\配置日志记录,local0为日志设备,默认是系统日志
log 127.0.0.1 local1 notice \\日志级别为notice
#log loghost local0 info
maxconn 4096 \\最大连接数
uid 99 \\用户uid
gid 99 \\用户gid
daemon \\以守护进程的方式运行
#debug \\调试模式,输出启动信息到标准输出
#quiet \\安静模式,启动时无输出
defaults
log global \\使用globle中定义的日志
mode http \\模式为http
option httplog \\采用http的格式记录日志
option dontlognull \\保证HAProxy不记录上级负载均衡发送过来的用于检测状态数据的心跳包
retries 3 \\检查节点连接失败的次数,超过3次认为节点不可用
# redispatch \\当负载很高时,自动结束当前队列处理比较久的连接
maxconn 2000 \\最大连接数
contimeout 5000 \\连接超时时间ms
clitimeout 50000 客户端超时时间ms
srvtimeout 50000 服务器超时时间ms
listen webcluster 0.0.0.0:80 \\定义群集和监听的端口号
option httpchk GET /index.html \\检查服务器的index.html文件,心跳检测URL设置
balance roundrobin \\负载均衡的调度算法为轮询
server inst1 192.168.1.61:80 check inter 2000 fall 3 \\定义在线节点
server inst2 192.168.1.62:80 check inter 2000 fall 3
check inter 2000是检测心跳频率(每2000ms检测一次),fall 3是3次失败认为服务器不可用
chroot /usr/share/haproxy \\也就是改变程序执行时所参考的根目录位置,如果有此代码,需要创建此目录
在新版本中,超时的设置做了调整,具体如下
contimeout 被 timeout connect取代:定义haproxy将客户端请求转发至后端服务器所等待的超时时长
clitimeout 被timeout client取代:客户端非活动状态的超时时长,是 app 连接 haproxy的时间
srvtimeout 被timeout server取代:客户端与服务器端建立连接后,等待服务器端的超时时长,是haproxy 连接后端web服务器的时间.
haproxy共有八种调度算法
- 1) balance leastconn 最少连接数
- 2) balance roundrobin 轮询
- 3) balance source 根据客户端IP进行哈希的方式
- 4) static-rr 根据权重
- 5) uri 根据请求的URI
- 6) url_param 根据请求的URl参数
- 7) hdr(name) 根据HTTP请求头来锁定每一次HTTP请求
- 8) rdp-cookie(name) 根据cookie(name)来锁定并哈希每一次TCP请求
关于日志级别
- static Level DEBUG
- DEBUG Level指出细粒度信息事件对调试应用程序是非常有帮助的。
- static Level INFO
- INFO level表明 消息在粗粒度级别上突出强调应用程序的运行过程。
- static Level WARN
- WARN level表明会出现潜在错误的情形。
- static Level ERROR
- ERROR level指出虽然发生错误事件,但仍然不影响系统的继续运行。
- static Level FATAL
- FATAL level指出每个严重的错误事件将会导致应用程序的退出。
- 另外,还有两个可用的特别的日志记录级别:
- static Level ALL
- ALL Level是最低等级的,用于打开所有日志记录。
- static Level OFF
- OFF Level是最高等级的,用于关闭所有日志记录。
4.启动
[root@localhost haproxy-1.5.19]# /etc/init.d/haproxy start
5.测试web群集
http://192.168.1.60/test.html
刷新页面进行测试
或使用脚本测试
[root@localhost ~]# for i in $(seq 10); do curl http://192.168.1.60/test.html ;done
6.Haproxy的日志
haproxy在默认情况不会记录日志,除了在haproxy.conf中的global段指定日志的输出外,还需要配置系统日志的配置文件。
方法一:
[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
log /dev/log local0 info
log /dev/log local0 notice
[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]# service rsyslog restart
[root@localhost ~]#/etc/init.d/haproxy restart
[root@localhost ~]# cat /var/log/haproxy/haproxy-info.log
方法二:
(1)编辑/etc/haproxy/haproxy.conf
[root@localhost ~]# vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local3
#local3是设备,对应于 /etc/rsyslog.conf中的配置,默认回收info的日志级别
(2)编写haproxy日志文件
[root@localhost ~]# vim /etc/rsyslog.d/haproxy.conf
$ModLoad imudp
$UDPServerRun 514
local3.* /var/log/haproxy.log
&~
注释:
- $ModLoad imudp采集日志的协议UDP
- $UDPServerRun 514指定日志采集使用的端口号
- local3.* /var/log/haproxy.log指定日志存放位置
(3)配置rsyslog的主配置文件,开启远程日志(可以不配)
[root@localhost ~]# vim /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS=”-c 2 -r -m 0″
- -c 2 使用兼容模式,默认是 -c 5
- -r 开启远程日志
- -m 0 标记时间戳。单位是分钟,为0时,表示禁用该功能
(4)重启haproxy和rsyslog服务
[root@localhost ~]# systemctl restart rsyslog
[root@localhost ~]# systemctl restart haproxy
(5)访问网站后查看日志
[root@localhost ~]# cat /var/log/haproxy.log
扩展:代理mysql
listen mysql 0.0.0.0:3306
server mysql1 192.168.10.205:3306 check port 3306 maxconn 300