环境:
haproxy:10.80.11.203
web1:10.80.11.206
web2:10.80.11.207
从官方网站:http://haproxy.1wt.eu/ 下载最新版本
# wget
# tar zcvf haproxy-1.3.20.tar.gz
# cd haproxy-1.3.20
# make TARGET=linux26 PREFIX=/usr/local/haprpxy
# make install PREFIX=/usr/local/haproxy
# cd /usr/local/haproxy
# vim haproxy.cfg
global
log 127.0.0.1 local0
#log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
chroot /usr/local/haproxy
uid 99
gid 99
daemon
nbproc 1
pidfile /usr/local/haproxy/haproxy.pid
#debug
#quiet
defaults
log 127.0.0.1 local3
mode http
option httplog
option httpclose
option dontlognull
option forwardfor
option redispatch
retries 2
maxconn 2000
balance roundrobin
stats uri /haproxy-stats
stats auth hx10:123456 contimeout 5000
clitimeout 50000
srvtimeout 50000
listen web_proxy 0.0.0.0:80
option httpchk GET /check.htm
server s1 10.80.11.206:80 weight 3 check
server s2 10.80.11.207:80 weight 3 check
红色标记的意思是:
uid 99 #所属运行的用户uidgid 99 #所属运行的用户组
log 127.0.0.1 local3 #日志文件的输出定向mode http #所处理的类别option httplog #日志类别
balance roundrobin #负载均衡算法stats uri /haproxy-status #haproxy 监控页面的访问地址
stats auth hx10:123456 查看监控页,输入的用户名和密码
listen web_proxy 0.0.0.0:80 #运行的端口及主机名
option httpchk GET /check.htm #健康检测server s1 10.80.11.206:80 weight 3 check #web1
server s1 10.80.11.207:80 weight 3 check #web2
然后在web1和web2上添加/check.htm页面,内容任意
启动haproxy
# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
重启haproxy
# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg -st `cat /usr/local/haproxy/haproxy.pid`
停止haproxy
# killall haproxy
状态截图如下: