●目前常见的Web集群调度器分为软件和硬件
●软件通常使用开源的LVS,Haproxy,Nginx
●硬件一般使用比较多的是F5,也有很多使用国内的一些产品,如梭子鱼,绿盟等
●LVS在企业应用中抗负载能力很强,但存在不足
●Haproxy是一款可提供高可用性,负载均衡,及基于TCP和HTTP应用的代理软件
Haproxy支持多种调度算法,最常用的有三种
●理解举例:
●理解举例
●理解举例:
●在两台网站服务器上安装Nginx,并启动服务
●在两台Nginx上配置测试网站,注意测试网页的内容应该不同,以便进行测试
●在负载均衡器上安装Haproxy
●安装步骤
●建立Haproxy的配置文件
●Haproxy配置文件分为三部分
●global配置参数
●defaults配置项配置默认参数,一般会被应用组件继承
●如果应用组件中没有特别声明,将按默认配置参数设置
●LISTEN配置项目一般为配置应用模块参数
默认是输出到系统syslog中,生产环境中一般单独定义
定义的方法步骤
●修改Haproxy配置文件中关于日志配置选项,加入配置
●修改rsyslog配置,将Haproxy相关的配置独立定义到haproxy.conf,并放到/etc/rsyslog.d下
●保存配置文件并重启rsyslog服务,完成rsylog配置
访问Haproxy集群测试网页并查看日志信息
随着企业网站负载增加,haproxy参数优化相当重要
●maxcorn:最大连接数,根据应用实际情况进行调整,推荐使用10 240
●daemon:守护进程模式,Haproxy可以使用非守护进程模式启动,建议使用守护进程模式启动
●nbproc:负载均衡的并发进程数,建议与当前服务器CPU核数相等或为其2倍
●option http-server-close:主动关闭http请求选项,建议在生产环境中使用此选项
●timeout http-keep-alive:长连接超时时间,设置长连接超时时间,可以设置为10s
●timeout http-request:http请求超时时间,建议将此时间设置为5~10s,增加http连接释放速度
●timeout client:客户端超时时间,如果访问量过大,节点响应慢,可以将此时间设置短一些,建议设置为1min左右就可以了
一.先配置haproxy虚拟机
1.修改主机名方便区分,用xftp软件将工具包放在opt目录下
[root@localhost ~]# hostnamectl set-hostname haproxy ##修改主机名方便区分
[root@localhost ~]# su ##切换主机名
[root@haproxy ~]# setenforce 0 ##关闭防护系统
[root@haproxy ~]# systemctl stop firewalld.service ##关闭防火墙
[root@haproxy ~]# cd /opt
[root@haproxy opt]# yum -y install pcre-devel bzip2-devel gcc gcc-c++ make ##支持gzip格式
[root@haproxy opt]# tar zxvf haproxy-1.5.19.tar.gz
[root@haproxy opt]# cd haproxy-1.5.19/
[root@haproxy haproxy-1.5.19]# make TARGET=linux26 ##以26的形式进行编译的
[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 ####监听检查服务器的index.html文件(节点服务器的主页)
balance roundrobin ####配置轮询算法
server inst1 192.168.148.136:80 check inter 2000 fall 3 ####定义节点,健康状态检查,每2s一次
server inst2 192.168.148.137:80 check inter 2000 fall 3
[root@haproxy haproxy]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
[root@haproxy haproxy-1.5.19]# cp examples/haproxy.init /etc/init.d/haproxy ##拷贝启动项
[root@haproxy haproxy-1.5.19]# cd /etc/init.d/
[root@haproxy init.d]# ls
functions haproxy netconsole network README
[root@haproxy init.d]# chmod +x haproxy ##增加权限
[root@haproxy init.d]# chkconfig --add /etc/init.d/haproxy
[root@haproxy init.d]# ln -s /usr/local/sbin/haproxy /usr/sbin/ ##建立软链接
二.配置节点服务器
1.修改主机名方便区分,用xftp软件将工具包放在opt目录下
[root@localhost ~]# hostnamectl set-hostname web1
[root@localhost ~]# su
[root@web1 ~]# setenforce 0 ##关闭防护功能
[root@web1 ~]# iptables -F ##清空防火墙规则
2.web虚拟机编译安装nginx,并且启动
[root@web1 opt]# yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel make
[root@web1 opt]# useradd -M -s /sbin/nologin nginx ##建立管理用户
[root@web1 opt]# tar zxvf nginx-1.12.2.tar.gz ##解压文件
[root@web1 opt]# cd nginx-1.12.2/
[root@web1 opt]# ./configure \
--prefix=/usr/local/nginx \ ##指定路径
--user=nginx \ ##管理用户
--group=nginx ##管理属组
[root@web1 nginx-1.12.2]# make&&make install ##编译安装
[root@web1 html]# cd /usr/local/nginx/html/
[root@web1 html]# vim test.html
<h1>this is test01</h1> ##写入页面信息
[root@web1 html]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ ##建立软链接方便管理
[root@web1 html]# nginx
Web02也是相同的配置,只是网页的页面信息修改方便管理
都配置好后,打开客户端win10的虚拟机输入haproxy的IP地址访问页面,测试情况
日志定义的配置:
1.修改haproxy服务虚拟机的haproxy的配置文件
[root@haproxy haproxy]# vim haproxy.cfg
global
log /dev/log local0 info
log /dev/log local0 notice
[root@haproxy haproxy]# service haproxy restart
[root@haproxy haproxy]# cd /etc/rsyslog.d/
[root@haproxy rsyslog.d]# vim 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-info.log
&~
[root@haproxy rsyslog.d]# systemctl restart rsyslog.service ##重启日志服务
[root@haproxy rsyslog.d]# service haproxy restart ##重启haproxy服务