| 
	一、拓扑 
	  
	 
	  
	二、安装负载均衡服务器软件 
	        wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.18.tar.gztar zxvf haproxy-1.4.18.tar.gz
 cd haproxy-1.4.18
 uname -a
 make TARGET=linux26 PREFIX=/usr/local/haproxy
 make install PREFIX=/usr/local/haproxy
 
	三、配置haproxy 
	     vi haproxy.cfg#######################################################
 global
 maxconn  5120
 chroot   /usr/local/haproxy
 uid      100
 gid      100
 daemon
 quiet
 nbproc   2
 pidfile  /usr/local/haproxy/haproxy.pid
 ########################################################
 
	defaultslog     global
 log     127.0.0.1  local3
 mode    http
 option  httplog
 option  dontlognull
 option  redispatch
 retries 3
 
	   maxconn 3000contimeout 5000
 clitimeout 50000
 srvtimeout 50000
 ##########################################################
 
	listen web 192.168.1.250:80mode    http
 #   mode    tcp
 balance roundrobin
 option  forwardfor
 option  httpclose
 option  httpchk GET /index.html
 server web1 192.168.1.251:80 check inter 5000 fall 1 rise 2 weight 1
 server web2 192.168.1.252:80 check inter 5000 fall 1 rise 2 weight 1
 ##########################################################
 
	listen status 192.168.1.250:8080stats enable
 stats uri /admin
 stats auth admin:admin
 stats realm Haproxy \ statistic
 ##########################################################
 
	###################到此haproxy简单配置成功################# 
	四、启动: /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg 
	       由于需要方便使用我便写成shell启动脚本: 
	vi  /etc/rc.d/init.d/haproxy#!/bin/bash
 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/haproxy/sbin
 servername=haproxy
 serverdir=/usr/local/haproxy
 daemon=/usr/local/haproxy/sbin/haproxy
 config=/usr/local/haproxy/haproxy.cfg
 pidfile=/usr/local/haproxy/haproxy.pid
 
	./etc/rc.d/init.d/functions 
	case "$1" in
 start)
 $daemon -f $config
 echo "......"
 ;;
 
	    stop)server_id=`cat $pidfile`
 kill $server_id
 echo "......."
 ;;
 
	    restart)$daemon -f $config -p $pidfile -sf $(cat $pidfile)
 echo "......."
 ;;
 
 *)
 echo "Usage:$0 {start|stop|restart}"
 exit 1
 esac
 
	########################################################################## 
	五、测试 
	       web1-------->ip:192.168.1.251------------>index.html------------->the is web1 
	       web2-------->ip:192.168.1.252------------>index.html------------->the is web2 
	  
	 
	  
	 
	  
	后台监控管理: 
	  
	 
	当web1服务器断开时: 
	  
	 
	当web1服务器恢复时: 
	  
	 
	#################################到些测试完毕###################### 
	六、日志的配置: 
	##########添加下面语句######### 
	vi /etc/syslog.conflocal3.*         /var/log/haproxy.log
 local0.*         /var/log/haproxy.log
 
	##########修改下面语句########## 
	vi /etc/sysconfig/syslogSYSLOGD_OPTIONS="-r -m 0"
 
	############重启服务############# 
	service syslog restartservice haproxy restart
 
	  
	 
	  
	############################################################################# 
	简单讲解配置文件: 
	   maxconn  5120               ###最大连接数chroot   /usr/local/haproxy ###工作目录
 uid      100                ###运行用户uid
 gid      100                ###这个就是用户组
 daemon                      ###在后台运行
 quiet                       ###使用的模式(启动时没有输出),可以使用debug模式
 nbproc   2                  ###启动实例设置
 pidfile  /usr/local/haproxy/haproxy.pid  ###这个是PID
 
	
 log     127.0.0.1  local3   ###日志文件
 mode    http                ###使用http模式,还有一个tcp模式,这个是七层和四层的区别
 option  httplog             ###日志类型
 retries 3                   ###检查连接服务器失败的次数
 
	contimeout 5000             ###连接超时
 clitimeout 50000            ###客户端连接超时
 srvtimeout 50000            ###服务器端连接超时
 
	   balance roundrobin          ###算法option  httpchk GET /index.html  ###检查的页面
 
 #####################以下两句源服务器#####################
 server web1 192.168.1.251:80 check inter 5000 fall 1 rise 2 weight 1
 server web2 192.168.1.252:80 check inter 5000 fall 1 rise 2 weight 1
 
	   ########################监控页面##########################listen status 192.168.1.250:8080
 stats enable
 stats uri /admin
 stats auth admin:admin
 stats realm Haproxy \ statistic
 
	本文出自 “游造技术博客” 博客,请务必保留此出处http://youzao.blog.51cto.com/3946111/752211(责任编辑:IT) |