TCP代理软件:L4(伪四层)
http反向代理软件:七层应用代理
支持SSL链接:支持客户端到到Haproxy,Haproxy到后面服务器,以及全程SSL的支持
负载均衡器,支持会话粘性;
HTTP协议的修正与保护,以及内容压缩
整体来讲:HAProxy提供了L4(TCP)和L7(HTTP)两种负载均衡能力(反向代理)。采用单线程事件驱动型非阻塞引擎;媲美商用负载均衡器的性能和稳定性。
haproxy经常使用架构:html
负载均衡:L4(伪四层)和L7两种模式,支持RR/静态RR/LC/IP Hash/URI Hash/URL_PARAM Hash/HTTP_HEADER Hash等丰富的负载均衡算法
健康检查:支持TCP和HTTP两种健康检查模式
会话保持:对于未实现会话共享的应用集群,可经过Insert Cookie/Rewrite Cookie/Prefix Cookie,以及上述的多种Hash方式实现会话保持
SSL:HAProxy能够解析HTTPS协议,并可以将请求解密为HTTP后向后端传输
HTTP请求重写与重定向
监控与统计:HAProxy提供了基于Web的统计信息页面,展示健康状态和流量数据。基于此功能,使用者能够开发监控程序来监控HAProxy的状态node
测试说明:
经过haproxy负载代理后端两台web server ,小试牛刀
测试环境:
haproxy 172.16.3.152 CentOS7.4_x64
node1 172.16.3.167 CentOS7.4 httpd
node1 172.16.3.175 CentOS7.4 httpd linux
源码安装git
官方下载 wget http://www.haproxy.org/download/1.8/src/haproxy-1.8.4.tar.gz 编译安装 tar xvf haproxy-1.8.4.tar.gz cd haproxy-1.8.4 make TARGET=linux26 ARCH=X86_64 PREFIX=/usr/local/haproxy make install PREFIX=/usr/local/haproxy cp /root/haproxy-1.8.4/examples/haproxy.init /etc/init.d/haproxy chmod +x /etc/init.d/haproxy 并修改BIN=/usr/local/haproxy/sbin/$BASENAME 建立配置文件 mkdir /etc/haproxy cp option-http_proxy.cfg /etc/haproxy/haproxy.cfg
日志配置
为haproxy配置本地日志记录
配置本地的log
vim /etc/rsyslog.conf 添加以下行github
$ModLoad imudp $UDPServerRun 514 local2.* /var/log/haproxy.log 重启rsyslog [root@haproxy ~]# systemctl restart rsyslog
日志开放UDP 514端口web
3harpoxy服务脚本
[root@haproxy ~]# cp /root/haproxy-1.8.4/examples/haproxy.init /etc/init.d/haproxyd
[root@haproxy ~]# cat /etc/init.d/haproxydredis
#!/bin/bash # # chkconfig: - 85 15 # description: HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited \ # for high availability environments. # processname: haproxy # config: /etc/haproxy/haproxy.cfg # pidfile: /var/run/haproxy.pid # Script Author: Simon Matter <simon.matter@invoca.ch> # Version: 2004060600 # 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=`basename $0` if [ -L $0 ]; then BASENAME=`find $0 -name $BASENAME -printf %l` BASENAME=`basename $BASENAME` fi BIN=/usr/local/haproxy/sbin/haproxy CFG=/etc/haproxy/haproxy.cfg [ -f $CFG ] || exit 1 PIDFILE=/var/run/$BASENAME.pid LOCKFILE=/var/lock/subsys/$BASENAME RETVAL=0 start() { quiet_check if [ $? -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 $PIDFILE RETVAL=$? echo [ $RETVAL -eq 0 ] && touch $LOCKFILE return $RETVAL } stop() { echo -n "Shutting down $BASENAME: " killproc $BASENAME -USR1 RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f $LOCKFILE [ $RETVAL -eq 0 ] && rm -f $PIDFILE return $RETVAL } restart() { quiet_check if [ $? -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 [ $? -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 $?
[root@haproxy ~]# egrep -v '(^$|^#)' /etc/haproxy/haproxy.cfg global maxconn 4000 #最大链接 log 127.0.0.1 local2 #日志记录 user haproxy group haproxy nbproc 1 #开启进程数 pidfile /var/run/haproxy.pid #pid文件 daemon defaults ####默认配置 mode http #默认运行模式 http 也支持tcp log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 if-none option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 frontend myproxy #定义frontend myproxy bind *:80 #绑定80端口 log 127.0.0.1 local2 option nolinger option http_proxy maxconn 3000 timeout client 30s # layer3: Valid users acl allow_host src 172.16.3.0/16 http-request deny if !allow_host #容许172.16.3.0网段访问 # layer7: prevent private network relaying acl forbidden_dst url_ip 192.168.0.0/24 http-request deny if forbidden_dst #禁止192.168.0.0网段访问 default_backend test-proxy-srv #使用test-proxy-srv backend组服务器响应 backend test-proxy-srv balance roundrobin #负载算法轮循 server srv1 172.16.3.167:80 check server srv2 172.16.3.175:80 check
cd /etc/init.d
./haproxyd check 检查配置是否有问题算法
[root@localhost init.d]# ./haproxyd check Configuration file is valid #启动 [root@localhost init.d]# ./haproxyd start
但出现以上信息表示没有问题直接,再启动;注意这里有一个问题因为采用的是最新的1.8.4稳定版 这个启动脚本 在加到systemd管理时出问题,/etc/inid.d/haproxyd 运行也等同systemctl start haproxyd 故直接切换到/etc/init.d下./haproxyd start运行 没有 问题;后面来排这个错!vim
打开浏览器访问http://172.16.3.152
按Ctrl F5刷新 后端
添加管理haproxy状态页
在/etc/haproxy/haproxy.cfg中添加以下内容
listen stats bind *:9099 acl allowstats src 172.16.3.140 #容许访问的主机 acl all src 0.0.0.0/0.0.0.0 http-request allow if allowstats http-request deny if all # errorloc 403 http://172.16.3.102:10080/errorloc/403.html #其余服务器上提供 errorfile 403 /etc/haproxy/errorfiles/403.html #本的错误提示页 stats enable #开启状态页 stats uri /myproxy?admin stats realm "Haproxy status Page" stats auth admin:admin #登陆的用户名密码 stats admin if TRUE
mkdir /etc/haproxy/errorfiles
echo "Access error!" >/etc/haproxy/errorfiles/403.html
检查配置文件并重启haproxy
访问状态页:
配置文件更多项及配置示例请参考官方文档本次测试是一个简单web负载应用 ,haproxy还能够作tcp负载,动静分离,ssl等