haproxy负载均衡服务器之搭建

来源:互联网 发布:linux sprintf 编辑:程序博客网 时间:2024/06/02 12:57


1.安装haproxy

1) 解压包

[root@localhost ~]# tar xzvf haproxy-1.5.3.tar.gz

2)查看内核版本

[root@localhost ~]# uname -aLinux localhost.localdomain 2.6.32-358.el6.x86_64 #1 SMP Tue Jan 29 11:47:41 EST 2013 x86_64 x86_64 x86_64 GNU/Linux


3)编译安装

<pre name="code" class="plain">[root@localhost ~]# <span style="font-family: Arial, Helvetica, sans-serif;">make TARGET=linux26 PREFIX=/usr/local/haprpxy</span>

[root@localhost ~]# make install PREFIX=/usr/local/haproxy
4)编写配置文件

[root@localhost ~]# cd /usr/local/haproxy
[root@localhost ~]# vim haproxy.cfg 

globallog 127.0.0.1local0log 127.0.0.1local1 notice#log loghostlocal0 infomaxconn 4096  #最大连接数chroot /usr/local/haproxyuid 99        #所属运行的用户UIDgid 99      #所属运行的用户组daemon        #以后台形式运行HAProxy#debug#quietinbproc      #启动1个实例,可以启多个来提高效率pidfile /var/run/haproxy.piddefaultslogglobalmodehttpoption httpclose               #每次请求完毕后主动关闭http通道optionredispatch       #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器option forwardforoption abortonclose            #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接option dontlognull       #保证HAProxy不记录上级负载均衡发送过来的用于检测状态没有数据的心跳包retries2                      #重试次数maxconn2000       #最大连接数balance source                 #如果想让HAProxy按照客户端的IP地址进行负载均衡策略,即同一IP地址的所有请求都发送到同一服务器时需要配置此选项timeout connect 5000timeout client 50000timeout server 50000listenweb_proxy 0.0.0.0:80mode httpbalanceroundrobinserverweb1 172.16.0.181:80 cookie app1inst1 check inter 2000 rise 2 fall 5serverweb2 172.16.0.182:80 cookie app1inst2 check inter 2000 rise 2 fall 5listen admin_statsbind *:8888 #监听端口        mode http    #http的7层模式option httplog  #采用http日志格式log 127.0.0.1 local0 errmaxconn 10stats refresh 30s#统计页面自动刷新时间stats uri /stats        #统计页面urlstats realm XingCloud\ Haproxy  #统计页面密码框上提示文本stats auth admin:wudi32         #统计页面用户名和密码设置stats hide-version        #隐藏统计页面上HAProxy的版本信息

5) 启动haproxy

[root@localhost haproxy]# haproxy -f /usr/local/haproxy/haproxy.cfg

启动报错

[root@localhost conf]# haproxy -f /usr/local/haproxy/conf/haproxy.cfg [WARNING] 222/220838 (1685) : parsing [/usr/local/haproxy/conf/haproxy.cfg:21]: keyword 'redispatch' is deprecated in favor of 'option redispatch', and will not be supported by future versions.[WARNING] 222/220838 (1685) : parsing [/usr/local/haproxy/conf/haproxy.cfg:24] : the 'contimeout' directive is now deprecated in favor of 'timeout connect', and will not be supported in future versions.[WARNING] 222/220838 (1685) : parsing [/usr/local/haproxy/conf/haproxy.cfg:25] : the 'clitimeout' directive is now deprecated in favor of 'timeout client', and will not be supported in future versions.[WARNING] 222/220838 (1685) : parsing [/usr/local/haproxy/conf/haproxy.cfg:26] : the 'srvtimeout' directive is now deprecated in favor of 'timeout server', and will not be supported in future versions.

原有版本的参数,在此版本中已经废弃了

contimeout 被 timeout connect取代

clitimeout 被timeout client取代

srvtimeout 被timeout server取代


6)停止haproxy

[root@localhost haproxy]# killall haproxy

7) 访问

http://172.16.0.221:1080/stats


8)编写启动脚本

[root@localhost ~] vim /etc/rc.d/init.d/haproxy
<pre name="code" class="plain">[root@localhost ~] chmod +x <span style="font-family: Arial, Helvetica, sans-serif;">/etc/rc.d/init.d/haproxy</span>

#!/bin/bash## haproxy# chkconfig: 35 85 15# description: HAProxy scripts# processname: haproxy# config:  /usr/local/haproxy/conf/haproxy.cfg# pidfile: /usr/local/haproxy/run/haproxy.pidif [ -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 0config="/usr/local/haproxy/conf/haproxy.cfg"exec="/usr/local/haproxy/sbin/haproxy"PID="/var/run/haproxy.pid"[ -f $config ] || exit 1 RETVAL=0 start() {   daemon $exec -c -q -f $config     if [ $? -ne 0 ]; then         echo "Errors found in configuration file."         return 1     fi   echo -n "Starting HAproxy: "   $exec -D -f $config -p $PID  RETVAL=$?   echo   [ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy   return $RETVAL }stop() {  echo -n "Shutting down HAproxy: "  killproc haproxy -USR1  RETVAL=$?  echo  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/haproxy  [ $RETVAL -eq 0 ] && rm -f $PID  return $RETVAL }restart() {  $exec -c -q -f $config    if [ $? -ne 0 ]; then        echo "Errors found in configuration file, check it with 'haproxy check'."        return 1    fi  stop  start }rhstatus() {  status haproxy }# See how we were called. case "$1" in  start)         start         ;;  stop)         stop         ;;  restart)         restart         ;;  status)         rhstatus         ;;  *)         echo $"Usage: haproxy {start|stop|restart|status}"         RETVAL=1 esac exit $RETVAL

[root@localhost ~] chkconfig --add haproxy


HAProxy的算法非常多,常用的算法有如下8种:
    balance roundrobin,表示简单的轮询,建议关注;
    balance static-rr,表示根据权重,建议关注;
    balance leastconn,表示最少连接者先处理,建议关注;
    balance source,表示根据请求源IP,跟Nginx的ip_hash算法相似,建议关注;
    balance uri,表示根据请求的URI;
    balance url_param,表示根据请求的URl参数;
    balance hdr(name),表示根据HTTP请求头来锁定每一次HTTP请求;
    balance rdp-cookie(name),表示根据据cookie(name)来锁定并哈希每一次TCP请求。




0 0
原创粉丝点击