阿使得肌肤· 发表于 2019-1-1 08:52:06

Haproxy介绍与基本应用初探

HAProxy是什么
  TCP代理软件:L4(伪四层)
http反向代理软件:七层应用代理
支持SSL连接:支持客户端到到Haproxy,Haproxy到后面服务器,以及全程SSL的支持
负载均衡器,支持会话粘性;
HTTP协议的修正与保护,以及内容压缩
总体来说:HAProxy提供了L4(TCP)和L7(HTTP)两种负载均衡能力(反向代理)。采用单线程事件驱动型非阻塞引擎;媲美商用负载均衡器的性能和稳定性。
haproxy常用架构:
http://i2.运维网.com/images/blog/201803/13/a7e8009ca83ef0a8b5b9a682a0fa792b.png

HAProxy的核心功能
  负载均衡: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的状态

Haproxy基本应用
  测试说明:
通过haproxy负载代理后端两台web server ,小试牛刀
测试环境:
haproxy172.16.3.152CentOS7.4_x64
node1      172.16.3.167   CentOS7.4httpd
node1      172.16.3.175   CentOS7.4httpd
  源码安装

官方下载
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=linux26ARCH=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 添加如下行

$ModLoad imudp
$UDPServerRun 514
local2.*   /var/log/haproxy.log
重启rsyslog
# systemctl restart rsyslog
  日志开放UDP 514端口
  3harpoxy服务脚本
# cp /root/haproxy-1.8.4/examples/haproxy.init /etc/init.d/haproxyd
# cat /etc/init.d/haproxyd

#!/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
# 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 $?

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 client30s
# 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:80check
server srv2 172.16.3.175:80 check

  cd /etc/init.d
./haproxyd check 检查配置是否有问题

# ./haproxyd check
Configuration file is valid
#启动
# ./haproxyd start

  但出现以上信息表示没有问题直接,再启动;注意这里有一个问题由于采用的是最新的1.8.4稳定版 这个启动脚本 在加到systemd管理时出问题,/etc/inid.d/haproxyd 运行也等同systemctl start haproxyd故直接切换到/etc/init.d下./haproxyd start运行 没有 问题;后面来排这个错!
  打开浏览器访问http://172.16.3.152
按Ctrl F5刷新
http://i2.运维网.com/images/blog/201803/13/5b9e3f734b7d38358d202a151b1f4c71.png
http://i2.运维网.com/images/blog/201803/13/6dd88bc8142bf1423489386e35d68606.png
  添加管理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 "Accesserror!" >/etc/haproxy/errorfiles/403.html
检查配置文件并重启haproxy
  访问状态页:
http://i2.运维网.com/images/blog/201803/13/3f78e19de2a058f7c44ec97729cb4eac.png
  配置文件更多项及配置示例请参考官方文档
本次测试是一个简单web负载应用 ,haproxy还可以做tcp负载,动静分离,ssl等



页: [1]
查看完整版本: Haproxy介绍与基本应用初探