HAProxy实现7层上的负载均衡

HAProxy概述

HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。根据官方数据,其最高极限支持10G的并发。
HAProxy特别适用于那些负载特大的web站点, 这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
其支持从4层至7层的网络交换,即覆盖所有的TCP协议。就是说,Haproxy 甚至还支持 Mysql的均衡负载。
相同点:在功能上,proxy通过反向代理方式实现 WEB均衡负载。和 Nginx,ApacheProxy,lighttpd,Cheroke 等一样。
不同点:Haproxy 并不是 web 服务器。以上提到所有带反向代理均衡负载的产品,都清一色是 WEB 服务器。简单说,就是他们能处理解析页面的。而Haproxy 仅仅是一款的用于均衡负载的应用代理。其自身并不能提供web服务。但其配置简单,拥有非常不错的服务器健康检查功能还有专门的系统状态监控页面,当其代理的后端服务器出现故障, HAProxy会自动将该服务器摘除,故障恢复后再自动将该服务器加入。
https://github.com/haproxy/haproxy/releases/ 在github 可以下载

拓扑图:
HAProxy实现7层上的负载均衡

1 安装
[[email protected] ~]# tar -zxvf haproxy-1.7.9.tar.gz
[[email protected] haproxy-1.7.9]# cd /root/haproxy-1.7.9
[[email protected] haproxy-1.7.9]# uname -r #查看内核版本

[[email protected] haproxy-1.7.9]# make TARGET=linux2628 PREFIX=/usr/local/haproxy #指定操作系统内核类型和安装的路径。也可以直接修改Makefile配置文件中这两个变量的值。如下:
[[email protected] haproxy-1.7.9]# vim Makefile
HAProxy实现7层上的负载均衡
[[email protected] haproxy-1.7.9]#make install PREFIX=/usr/local/haproxy
#如果没有修改Makefile配置文件中PREFIX变量的值,就必须在此重新对,PREFIX=/usr/local/haproxy赋值,否则直接执行 make install 时,make install会直接读取Makefile文件中PREFIX的变量值。
[[email protected] haproxy-1.7.9]# ls /usr/local/haproxy/
doc sbin share

  1. 没有生成配置文件,自己手动写一个HAproxy配置文件
    [[email protected] etc]# mkdir /usr/local/haproxy/etc
    [[email protected] ~]# vim /usr/local/haproxy/etc/haproxy.cfg #手动创建配置文件

global

log 127.0.0.1 local0

#log 127.0.0.1 local1 notice

#log loghost local0 info

maxconn 4096

chroot /usr/local/haproxy

uid 99 #所属运行的用户uid

gid 99 #所属运行的用户组

daemon #以后台形式运行haproxy

nbproc 1 #启动1个haproxy实例。# #工作进程数量(CPU数量) ,实际工作中,应该设置成和CPU核心数一样。 这样可以发挥出最大的性能。

pidfile /usr/local/haproxy/run/haproxy.pid #将所有进程写入pid文件

#debug #调试错误时用

#quiet #安静

defaults
log global
log 127.0.0.1 local3 #日志文件的输出定向。产生的日志级别为local3. 系统中local1-7,用户自己定义
mode http #工作模式,所处理的类别,默认采用http模式,可配置成tcp作4层消息转发
option httplog #日志类别,记载http日志
option httpclose #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现
option dontlognull #不记录空连接,产生的日志
option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
option redispatch #当serverid对应的服务器挂掉后,强制定向到其他健康服务器
retries 2 #2次连接失败就认为服务器不可用,主要通过后面的check检查
maxconn 2000 #最大连接数
balance roundrobin #负载均衡算法
stats uri /haproxy-stats #haproxy 监控页面的访问地址 # 可通过
http://localhost:80/haproxy-stats 访问
timeout connect 5000 #连接超时时间。 单位:ms 毫秒
timeout client 50000 #客户端连接超时时间
timeout server 50000 #服务器端连接超时时间
mode http
option httpchk GET /index.html #健康检测#注意实际工作中测试时,应该下载某一个页面来进行测试,因此这个页面应该是个小页面,而不要用首页面。这里是每隔一秒检查一次页面。

frontend http #前端配置,http名称可自定义
bind 0.0.0.0:80 #发起http请求80端口,会被转发到设置的ip及端口
default_backend http_back #转发到后端 写上后端名称

backend http_back #后端配置,名称上下关联
server s1 10.10.10.68:80 weight 3 check #后端的主机 IP &权衡
server s2 10.10.10.69:80 weight 3 check #后端的主机 IP &权衡
#server node1 192.168.179.131:8081 check inter 2000 rise 3 fall 3 weight 30
#inter 2000 健康检查时间间隔2秒
#rise 3 检测多少次才认为是正常的
#fall 3 失败多少次才认为是不可用的
#weight 30 权重

关于负载均衡算法
#source 根据请求源IP
#static-rr 根据权重
#leastconn 最少连接者先处理
#uri 根据请求的uri
#url_param 根据请求的url参数
#rdp-cookie 据据cookie(name)来锁定并哈希每一次请求
#hdr(name) 根据HTTP请求头来锁定每一次HTTP请求
#roundrobin 轮询方式

使用nobody用户运行haproxy
[[email protected] haproxy-1.7.9]# id nobody
uid=99(nobody) gid=99(nobody) groups=99(nobody) #id 为99
复制haproxy启动脚本,到/etc/init.d下
[[email protected] ~]# cp ./haproxy-1.7.9/examples/haproxy.init /etc/init.d/haproxy
[[email protected] ~]# chmod 755 /etc/init.d/haproxy
[[email protected] ~]# vim /etc/init.d/haproxy #此脚本需修改地方较多
#!/bin/sh
#chkconfig: - 85 15
#description: HA-Proxy server
#processname: haproxy
#config: /usr/local/haproxy/etc/haproxy.cfg
#pidfile: /usr/local/haproxy/run/haproxy.pid

#Source function library.
if [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
. /etc/rc.d/init.d/functions
else
exit 0
fi

#Source networking configuration.
. /etc/sysconfig/network
#Check that networking is up.
[ “$NETWORKING” = “no” ] && exit 0

#This is our service name
BASENAME='haproxy

BIN=/usr/sbin/haproxy

CFG=/usr/local/haproxy/etc/haproxy.cfg
[ -f $CFG ] || exit 1

PIDFILE=/usr/local/haproxy/run/haproxy.pid
LOCKFILE=/usr/local/haproxy/run/haproxy

RETVAL=0

start() {
quiet_check
if [ ?ne0];thenecho"Errorsfoundinconfigurationfile,checkitwith? -ne 0 ]; then echo "Errors found in configuration file, check it with 'BASENAME check’."
return 1
fi

echo -n "Starting $BASENAME: "
daemon $BIN -D -f $CFG -p PIDFILERETVAL=PIDFILE RETVAL=?
echo
[ $RETVAL -eq 0 ] && touch $LOCKFILE
return $RETVAL
}

stop() {
echo -n "Shutting down $BASENAME: "
killproc BASENAMEUSR1RETVAL=BASENAME -USR1 RETVAL=?
echo
[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
[ $RETVAL -eq 0 ] && rm -f $PIDFILE
return $RETVAL
}

restart() {
quiet_check
if [ ?ne0];thenecho"Errorsfoundinconfigurationfile,checkitwith? -ne 0 ]; then echo "Errors found in configuration file, check it with 'BASENAME check’."
return 1
fi
stop
start
}

reload() {
if ! [ -s $PIDFILE ]; then
return 0
fi

quiet_check
if [ ?ne0];thenecho"Errorsfoundinconfigurationfile,checkitwith? -ne 0 ]; then echo "Errors found in configuration file, check it with 'BASENAME check’."
return 1
fi
$BIN -D -f $CFG -p $PIDFILE -sf $(cat $PIDFILE)
}

check() {
$BIN -c -q -V -f $CFG
}

quiet_check() {
$BIN -c -q -f $CFG
}

rhstatus() {
status $BASENAME
}

condrestart() {
[ -e $LOCKFILE ] && restart || :
}

#See how we were called.
case “$1” in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
condrestart)
condrestart
;;
status)
rhstatus
;;
check)
check
;;
*)
echo $“Usage: $BASENAME {start|stop|restart|reload|condrestart|status|check}”
exit 1
esac
exit $?

复制haproxy文件到/usr/sbin下
因为上面的haproxy.init启动脚本默认会去/usr/sbin下找
[[email protected] ~]#cp /usr/local/haproxy/sbin/haproxy /usr/sbin/
创建目录和权限
[[email protected] ~]# mkdir -p /usr/local/haproxy/run
[[email protected] ~]# chown nobody /usr/local/haproxy/ -R

配置日志收集
[[email protected] ~]# vim /etc/rsyslog.conf #打开以下两行的注释,不打开收不到日志
$ModLoad imudp #取消注释
$UDPServerRun 514 #取消注释
local7.* /var/log/boot.log #下面添加两行
local3.* /var/log/haproxy.log
local0.* /var/log/haproxy.log
[[email protected] ~]# systemctl restart rsyslog

  1. 启动和停止服务
    特殊启动方法1
    [[email protected] etc]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
    查看状态:
    [[email protected] etc]# ps -axu | grep haproxy
    nobody 3871 0.0 0.0 12228 1036 ? Ss 21:53 0:00 /usr/local/haproxy/sbin/haproxy
    -f /usr/local/haproxy/etc/haproxy.cfg
    [[email protected] etc]# netstat -antup | grep 80
    tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3871/haproxy
    ##停止
    [[email protected] etc]# killall haproxy #没有killall命令?安装yum -y install psmisc

HAproxy脚本启动方法2
[[email protected] ~]# /etc/init.d/haproxy start 或 systemctl restart haproxy

配置后端服务器: yunzu62
配置web服务器:
[[email protected] html]# yum install httpd php -y
生成测试文件:
[email protected] html]#echo 192.168.1.62 > /var/www/html/index.html
启动apache服务器:
[[email protected] html]# service httpd restart
配置后端服务器: yunzu64
IP: 192.168.1.64
配置web服务器
[[email protected] html]# yum install httpd php -y
生成测试文件:
echo 192.168.1.64 > /var/www/html/index.html
[[email protected] html]# service httpd restart

  1. 查看HAproxy的监控页面
    http://192.168.1.63/haproxy-stats

注:
相关配置文件和启动脚本可以从这个配置模版中获得
[[email protected] haproxy-1.7.9]# cd /root/haproxy-1.7.9/examples/ [[email protected] examples]# ls
配置随机启动
[[email protected] examples]# chkconfig --add haproxy
[[email protected] examples]# chkconfig haproxy on