设为首页 收藏本站
查看: 1051|回复: 0

[经验分享] Haproxy介绍与基本应用初探

[复制链接]
发表于 2019-1-1 08:52:06 | 显示全部楼层 |阅读模式
HAProxy是什么
  TCP代理软件:L4(伪四层)
http反向代理软件:七层应用代理
支持SSL连接:支持客户端到到Haproxy,Haproxy到后面服务器,以及全程SSL的支持
负载均衡器,支持会话粘性;
HTTP协议的修正与保护,以及内容压缩
总体来说:HAProxy提供了L4(TCP)和L7(HTTP)两种负载均衡能力(反向代理)。采用单线程事件驱动型非阻塞引擎;媲美商用负载均衡器的性能和稳定性。
haproxy常用架构:


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 ,小试牛刀
测试环境:
haproxy  172.16.3.152  CentOS7.4_x64
node1      172.16.3.167   CentOS7.4  httpd
node1      172.16.3.175   CentOS7.4  httpd
  源码安装

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

$ModLoad imudp
$UDPServerRun 514
local2.*   /var/log/haproxy.log
重启rsyslog
[root@haproxy ~]# systemctl restart rsyslog
  日志开放UDP 514端口
  3harpoxy服务脚本
[root@haproxy ~]# cp /root/haproxy-1.8.4/examples/haproxy.init /etc/init.d/haproxyd
[root@haproxy ~]# 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简单负载应用

[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运行 没有 问题;后面来排这个错!
  打开浏览器访问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等




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-658126-1-1.html 上篇帖子: HAProxy入门及常用配置模拟测试 下篇帖子: 利用Haproxy实现WEB高可用性
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表