目前常见的Web集群调度器分为软件和硬件:
HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。
HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
LVS在企业应用中抗负载能力很强,但存在不足:
Haproxy是一款可提供高可用性、负载均衡、及基于TCP和HTTP应用的代理的软件
Haproxy支持多种调度算法,最常用的有三种:SH、roundrobin、lestconn
加权最小连接算法:后端具有最少连接数的服务器将接收到请求,根据后端服务器当前连接的活动个数少来调度;haproxy一般推荐使用在较长时间会话场景中;例如,LDAP,MySQL等协议;并不适用用短时间连接,不是特别适用指的是由于leastconn在计算调度权重时,还得扫描后端服务器当前会话数量,所以性能有影响,消耗cpu资源,但是带来的收益是很低的;支持权重的动态改变,支持慢启动,属于wlc调度算法;对于短时间的连接不超过5秒钟,或2秒,一般建议使用roundrobin算法;
加权轮询算法:权重可动态调整动态生效;根据权重轮流使用每个服务器,支持权重的运行时调整及慢启动机制,最大支持4095个后端主机;默认权重为1;其它使用hash调度方式的都支持动静两种方式;动态算法指的是服务器权重,可以在运行时调整,以实现实例的慢启动;所以可以加新server进来,而且可定义权重且可动态调整,不会瞬间调度大量请求,时逐渐调度请求上来的;服务器权重不同时就是加权轮询,相同时就是轮询;
默认是输出到系统的syslog中,生产环境中一般单独定义
随着企业网站负载增加,haproxy参数优化相当重要
用一台客户端,三台虚拟服务器模拟搭建一套web群集;来自用户的请求有前方的haproxy调度服务器接收并转发给后方的真实web服务器,后方真实web服务器群由两台nginx提供服务。
ip地址可自己规划
主机 | 操作系统 | ip地址 | 主要软件 |
---|---|---|---|
Haproxy服务器 | centos7.6 | 192.168.10.10 | haproxy-1.5.19.tar.gz |
Nginx服务器1 | centos7.6 | 192.168.10.20 | nginx-1.12.0.tar.gz |
Nginx服务器2 | centos7.6 | 192.168.10.30 | nginx-1.12.0.tar.gz |
客户端 | Windows 10 | 192.168.10.40 | 浏览器 |
1.两台web服务器nginx服务配置
2.haproxy服务器配置
3.haproxy服务日志管理
安装包准备
haproxy-1.5.19.tar.gz nginx-1.12.0.tar.gz
可以将各服务器改名,下面的命令将haproxy服务器改名为haproxy,其他的可自己改,不改也可以
[root@localhost ~]# hostnamectl set-hostname haproxy
[root@localhost ~]# su
[root@haproxy ~]#
web节点服务器
两个nginx服务器配置基本相同
[root@nginx01 ~]# iptables -F
[root@nginx01 ~]# setenforce 0
[root@nginx01 ~]#yum -y install \
pcre-devel \
zlib-devel \
gcc \
gcc-c++ \
make
[root@nginx01 ~]#useradd -M -s /sbin/nologin nginx
[root@nginx01 ~]#tar zxvf nginx-1.12.0.tar.gz -C /opt/
[root@nginx01 ~]#cd nginx-1.12.0/
[root@nginx01 nginx-1.12.0]#./configure \
--prefix=/usr/local/nginx \
--user=nginx
--group=nginx
[root@nginx01 nginx-1.12.0]#make && make install
[root@nginx01 nginx-1.12.0]#cd /usr/local/nginx/html
[root@nginx01 html]#echo "NJIT" > test.html ##配置另一台nginx服务器改为echo "你想修改的内容" > test.html
[root@nginx01 html]#ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
[root@nginx01 html]#nginx
[root@localhost html]# netstat -antp |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9086/nginx: master
haproxy服务器
[root@haproxy ~]# iptables -F
[root@haproxy ~]# setenforce 0
[root@haproxy ~]#yum -y install \
pcre-devel \
bzip2-devel \
gcc \
gcc-c++ \
make
[root@haproxy ~]#tar zxvf haproxy-1.5.19.tar.gz -C /opt/
[root@haproxy ~]#cd haproxy-1.5.19/
[root@haproxy haproxy-1.5.19]#make TARGET=linux26
[root@haproxy haproxy-1.5.19]#make install
[root@haproxy haproxy-1.5.19]#mkdir /etc/haproxy
[root@haproxy haproxy-1.5.19]#cp examples/haproxy.cfg /etc/haproxy
[root@haproxy haproxy-1.5.19]#cd /etc/haproxy
[root@haproxy haproxy]#vim haproxy.cfg
删除以下语句
chroot /usr/share/haproxy
redispatch
删除所有listen字段,再添加以下内容
listen webcluster 0.0.0.0:80
option httpchk GET /test.html
balance roundrobin
server inst1 20.0.0.61:80 check inter 2000 fall 3
server inst2 20.0.0.110:80 check inter 2000 fall 3
[root@haproxy haproxy]#cd
[root@haproxy ~]#cp haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
[root@haproxy ~]#chmod +x haproxy
[root@haproxy ~]#chkconfig --add /etc/init.d/haproxy
[root@haproxy ~]#ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@haproxy ~]#service haproxy start
[root@haproxy examples]# netstat -antp |grep haproxy
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8721/haproxy
到客户端去测试
访问haproxy的20.0.0.31/test.html即可访问web,按刷新按钮可看到两个nginx的页面即可
我们在之前的日志管理中,一般都是按时间来分割的,在这里我们用日志等级来分割
修改haproxy配置文件中的日志选项
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
log /dev/log local0 info
log /dev/log local0 notice
[root@haproxy ~]# service haproxy stop
[root@haproxy ~]# service haproxy start
修改rsyslog配置,将Haproxy相关的配置独立定义到haproxy.conf,放到/etc/rsyslog.d下
[root@haproxy ~]# touch /etc/rsyslog.d/haproxy.conf
[root@haproxy ~]# vim /etc/rsyslog.d/haproxy.conf
if ($programname == 'haproxy' and $syslogseverity-test == 'info')
then -/var/log/haproxy/haproxy-info.log
&~
if ($programname == 'haproxy' and $syslogseverity-test == 'notice')
then -/var/log/haproxy/haproxy-notice.log
&~
[root@haproxy ~]# systemctl restart rsyslog.service
查看日志
[root@haproxy ~]# service haproxy stop
[root@haproxy ~]# service haproxy start
[root@haproxy ~]# systemctl restart rsyslog.service
去访问下网页
[root@haproxy ~]#cd /var/log/
[root@haproxy ~]#ls
可看到haproxy目录
[root@haproxy log]# cd haproxy/
[root@haproxy haproxy]# ls
haproxy-info.log haproxy-notice.log
haproxy和nginx