Haproxy安装、tcp/ip & http的负载均衡

HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点, 这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。


1、下载&安装haproxy:

[plain]  view plain copy print ?
  1. wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.26.tar.gz  
  2.   
  3. tar zxvf haproxy-1.4.26.tar.gz  
  4.   
  5. cd haproxy-1.4.26  
  6.   
  7. uname -a //查看linux内核版本  
  8.   
  9. make TARGET=linux26 PREFIX=/usr/local/haproxy  
  10.   
  11. make install PREFIX=/usr/local/haproxy  

2、编写haproxy.cfg文件:

[plain]  view plain copy print ?
  1. vi /usr/local/haproxy/haproxy.cfg  

粘贴如下内容至haproxy.cfg:

[plain]  view plain copy print ?
  1. global  
  2.     maxconn 51200  
  3.     chroot /usr/local/haproxy  
  4.     uid 99  
  5.     gid 99  
  6.     daemon  
  7.     #quiet  
  8.     nbproc 2 #进程数  
  9.     pidfile /usr/local/haproxy/haproxy.pid  
  10.   
  11. defaults  
  12.         mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK  
  13.         #retries 2 #两次连接失败就认为是服务器不可用,也可以通过后面设置  
  14.         option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器  
  15.         option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接  
  16.         timeout connect 5000ms #连接超时  
  17.         timeout client 30000ms #客户端超时  
  18.         timeout server 30000ms #服务器超时  
  19.         #timeout check 2000 #=心跳检测超时  
  20.         log 127.0.0.1 local0 err #[err warning info debug]  
  21.         balance roundrobin                     #负载均衡算法  
  22. #        option  httplog                        #日志类别,采用httplog  
  23. #        option  httpclose   #每次请求完毕后主动关闭http通道,ha-proxy不支持keep-alive,只能模拟这种模式的实现  
  24. #        option  dontlognull  
  25. #        option  forwardfor  #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip  
  26.   
  27. listen admin_stats  
  28.         bind 0.0.0.0:8888 #监听端口  
  29.         option httplog #采用http日志格式  
  30.         stats refresh 30s #统计页面自动刷新时间  
  31.         stats uri /stats #统计页面url  
  32.         stats realm Haproxy Manager #统计页面密码框上提示文本  
  33.         stats auth admin:admin #统计页面用户名和密码设置  
  34.         #stats hide-version #隐藏统计页面上HAProxy的版本信息  
  35.   
  36. listen test1  
  37.         bind :12345  
  38.         mode tcp  
  39.         server t1 192.168.1.101:8881  
  40.         server t2 192.168.1.102:8881  
  41.   
  42. listen test2 :80  
  43.        option httpclose  
  44.        option forwardfor  
  45.         server s1 192.168.1.101:8080 check weight 1 minconn 1 maxconn 3 check inter 40000        
  46.         server s2 192.168.1.102:8080 check weight 1 minconn 1 maxconn 3 check inter 40000  

3、启动haproxy:

[plain]  view plain copy print ?
  1. /usr/local/haproxy/haproxy -f /usr/local/haproxy/haproxy.cfg  

4、查看是否启动:
[plain]  view plain copy print ?
  1. ps -e|grep haproxy  
  2.     7593 ?        00:00:00 haproxy  
  3.     7594 ?        00:00:00 haproxy  

看到上面这些,表明haproxy已经正常启动了,可以在t1,t2,s1,s2上面创建一些文件,通过指定的端口号访问一下看看结果如何!(t1,t2是tcp/ip协议)

要想关闭就kill pid 即可,虽然不是很优雅。


ok ,接下来我们来点优雅的,通过脚本来启动与关闭haproxy

1、编写启动脚本:

[plain]  view plain copy print ?
  1. vi /etc/rc.d/init.d/haproxy  

贴入如下内容:

[plain]  view plain copy print ?
  1. #!/bin/bash  
  2. BASE_DIR="/usr/local/haproxy"  
  3. ARGV="$@"  
  4.   
  5. start()  
  6. {  
  7. echo "START HAPoxy SERVERS"  
  8. $BASE_DIR/sbin/haproxy -f $BASE_DIR/conf/haproxy.cfg  
  9. }  
  10.   
  11. stop()  
  12. {  
  13. echo "STOP HAPoxy Listen"  
  14. kill -TTOU $(cat $BASE_DIR/haproxy.pid)  
  15. echo "STOP HAPoxy process"  
  16. kill -USR1 $(cat $BASE_DIR/haproxy.pid)  
  17. }  
  18. case $ARGV in  
  19.   
  20. start)  
  21. start  
  22. ERROR=$?  
  23. ;;  
  24.   
  25. stop)  
  26. stop  
  27. ERROR=$?  
  28. ;;  
  29.   
  30. restart)  
  31. stop  
  32. start  
  33. ERROR=$?  
  34. ;;  
  35.   
  36. *)  
  37. echo "hactl.sh [start|restart|stop]"  
  38. esac  
  39. exit $ERROR  

2、让脚本随系统自启动:

[plain]  view plain copy print ?
  1. chmod +x /etc/rc.d/init.d/haproxy  
  2. chkconfig --add haproxy  
  3. chkconfig  haproxy on  

3、启动与停止haproxy:

[plain]  view plain copy print ?
  1. service haproxy start  
  2. service haproxy stop  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值