Haproxy搭建web群集

常见的web集群调度器

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

Haproxy应用分析

LVS在企业应用中抗负载能力很强,但存在不足

LVS不支持正则处理,不能实现动静分离
对于大型网站,LVS的实施配置复杂,维护成本相对较高

Haproxy是一款可提供高可用性,负载均衡,及基于TCP和HTTP应用的代理的软件

特别适用于负载特别大的web站点
运行在当前的硬件上可支持数以万计的并发连接连接请求

Haproxy调度算法原理

RR:最简单常用的,轮询调度
LC:最小连接数算法,根据后端的节点连接数大小动态分配前端请求
SH:来源访问调度算法,用于有session会话记录在服务器端,可以基于来源ip,cookie做群集调度

实验拓扑图

Haproxy搭建web群集

实验环境

Haporxy服务器 192.168.13.175
web1服务器 192.168.13.151
web2服务器 192.168.13.176
client测试机

1,在web1,web2服务器上安装Nginx

[[email protected] ~]# yum install -y \ ##安装环境需要组件包

pcre-devel \ ##开发包
zlib-devel \ ##压缩包
gcc
gcc-c++
make
[[email protected] ~]# useradd -M -s /sbin/nologin nginx ##创建系统用户
[[email protected] ~]# mkdir /abc ##创建挂载点
[[email protected] ~]# mount.cifs //192.168.100.3/LNMP-C7 /abc/ ##挂载
Password for [email protected]//192.168.100.3/LNMP-C7:
[[email protected] ~]# cd /abc/
[[email protected] abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt ##解压
[[email protected] abc]# cd /opt/nginx-1.12.2/
[[email protected] nginx-1.12.2]# ./configure \ ##进行配置
–prefix=/usr/local/nginx
–user=nginx
–group=nginx
[[email protected] nginx-1.12.2]# make && make install
[[email protected] nginx-1.12.2]# echo “this is kgv web” > /usr/local/nginx/html/test.html
##创建站点网页内容,web2上为this is accp web
[[email protected] nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##便于系统识别
[[email protected] nginx-1.12.2]# nginx -t ##检查语法
[[email protected] nginx-1.12.2]# nginx ##开启服务
[[email protected] nginx-1.12.2]# systemctl stop firewalld.service ##关闭防火墙
[[email protected] nginx-1.12.2]# setenforce 0

2,在haproxy服务器上安装haproxy调度服务

[[email protected] ~]# yum install -y \ ##安装环境组件工具

pcre-devel
bzip2-devel
gcc
gcc-c++
make
[[email protected] ~]# systemctl stop firewalld.service ##关闭防火墙
[[email protected] ~]# setenforce 0
[[email protected] ~]# mkdir /abc
[[email protected] ~]# mount.cifs //192.168.100.3/LNMP-C7 /abc/ ##挂载
[[email protected] ~]# cd /abc/
[[email protected] abc]# tar zxvf haproxy-1.5.19.tar.gz -C /opt/ ##解压
[[email protected] abc]# cd /opt/haproxy-1.5.19/
[[email protected] haproxy-1.5.19]# make TARGET=linux26 ##编译
[[email protected] haproxy-1.5.19]# make install ##安装
[[email protected] haproxy-1.5.19]# mkdir /etc/haproxy ##创建配置文件目录
[[email protected] haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/ ##模板复制到配置目录下
[[email protected] haproxy-1.5.19]# cd /etc/haproxy/
[[email protected] haproxy]# vim haproxy.cfg ##编辑配置文件
----------删除所有listen项目,并添加------------------------------------
注释以下语句
chroot /usr/share/haproxy
redispatch
添加
listen webcluster 0.0.0.0:80
option httpchk GET /test.html ##web网页
balance roundrobin ##轮询
server inst1 192.168.13.151:80 check inter 2000 fall 3 ##健康检查请求三次
server inst2 192.168.13.176:80 check inter 2000 fall 3
[[email protected] haproxy]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy ##启动文件
[[email protected] haproxy]# chmod +x /etc/init.d/haproxy ##执行权限
[[email protected] haproxy]# chkconfig --add /etc/init.d/haproxy ##添加到service
[[email protected] haproxy]# ln -s /usr/local/sbin/haproxy /usr/sbin/ ##便于系统识别
[[email protected] haproxy]# service haproxy start ##开启服务
Starting haproxy (via systemctl): [ 确定 ]
[[email protected] haproxy]# netstat -ntap | grep haproxy ##查看端口
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 39884/haproxy

3,使用client测试网页

Haproxy搭建web群集
Haproxy搭建web群集

4,日志定义,修改haproxy配置文件

[[email protected] haproxy]# vim /etc/haproxy/haproxy.cfg ##修改配置文件
global
log /dev/log local0 info ##添加两个级别的日志文件
log /dev/log local0 notice
#log loghost local0 info
[[email protected] haproxy]# service haproxy restart ##重启服务
[[email protected] haproxy]# touch /etc/rsyslog.d/haproxy.conf ##创建系统日志haproxy配置文件
[[email protected] haproxy]# vim /etc/rsyslog.d/haproxy.conf
if ($programname == ‘haproxy’ and KaTeX parse error: Expected 'EOF', got '#' at position 33: …xt == 'info') #̲#根据级别创建不同的日志文件 …programname == ‘haproxy’ and $syslogseverity-text == ‘notice’)
then -/var/log/haproxy/haproxy-notice.log
&~
[[email protected] haproxy]# systemctl restart rsyslog.service ##重启系统日志服务
[[email protected] haproxy]# cd /var/log/ ##此时是没有haproxy日志
##重新访问网页
[[email protected] haproxy]# cd /var/log/haproxy/
[[email protected] haproxy]# ls ##此时就生成了info级别的日志文件
haproxy-info.log