Haproxy配置http模式负载均衡
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://jerry12356.blog.51cto.com/4308715/1858239
主机名 | 角色 | IP地址 |
mylinux1.contoso.com | haproxy服务器 | |
mylinux3.contoso.com | web服务器1 | eth0:192.168.100.181 |
mylinux4.contoso.com | web服务器2 | eth0:192.168.100.182 |
1
2
|
yum -y install httpd
/etc/init .d /httpd start
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
[[email protected] conf] # cp haproxy.cfg haproxy.cfg.bak
[[email protected] conf] # vi haproxy.cfg
[[email protected] conf] # cat haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
#log 127.0.0.1 local0
log 127.0.0.1:514 local0 warning
pidfile /usr/local/haproxy/var/run/haproxy .pid
daemon
maxconn 4096
chroot /usr/local/haproxy/var/chroot
user haproxy
group haproxy
nbproc 1
defaults
log global
mode http #默认的模式{tcp|http|health},tcp是4层,http是7层,health只会返回OK
retries 3
option httplog #日志类别,采用httplog
option httpclose #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现
option dontlognull #不记录健康检查日志信息
option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
maxconn 2000
balance roundrobin #设置默认负载均衡方式,轮询方式
timeout connect 5000
timeout client 50000
timeout server 50000
listen haproxy_stats
bind *:8000 #绑定到本地所有IP地址的8000端口上
mode http #http的7层模式
option httplog #采用http日志格式
maxconn 20 #默认的最大连接数
stats enable #启用状态监控
stats refresh 30s #监控页面自动刷新时间
stats uri /haproxy_status #监控页面url
stats auth admin:123456 #设置监控页面的用户名和密码,可以设置多个用户名
stats hide-version #隐藏监控页面上的Haproxy版本信息
listen websites
bind 192.168.100.121:80 #绑定到192.168.100.121上的80端口
timeout server 15s
timeout connect 30s
server mylinux3 192.168.100.181:80 check port 80 inter 2000 fall 3 #检测健康端口80,检测心跳频率是2000ms,失败3次则认为服务器不可用
server mylinux4 192.168.100.182:80 check port 80 inter 2000 fall 3
|
1
2
3
4
5
6
|
[[email protected] conf] # /usr/local/haproxy/sbin/haproxy -f haproxy.cfg -c
Configuration file is valid
[[email protected] conf] # /usr/local/haproxy/sbin/haproxy -f haproxy.cfg -D
[[email protected] conf] # ps -ef|grep haproxy
haproxy 2575 1 0 23:24 ? 00:00:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy .cfg
root 2584 1015 0 23:45 pts /1 00:00:00 grep haproxy
|
1
2
3
4
5
|
[[email protected] conf] # for i in {1..100};do curl http://192.168.100.121/ >>/tmp/webtest.txt;done
[[email protected] conf] # grep mylinux3.contoso.com /tmp/webtest.txt |wc -l
50
[[email protected] conf] # grep mylinux4.contoso.com /tmp/webtest.txt |wc -l
50
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#!/bin/bash
#
###################################################
#chkconfig: 2345 20 70
#description: Start and stop haproxy service.
#Author: Jerry Zhao QQ:1217406852
#Date: 2016-09-30 19:46:42 Friday
###################################################
BASE= /usr/local/haproxy
PROG=$BASE /sbin/haproxy
PIDFILE=$BASE /var/run/haproxy .pid
CONFFILE=$BASE /conf/haproxy .cfg
RUNNING_STATUS=` ps -ef| grep "haproxy -f" | egrep - v grep | wc -l`
start(){
RUNNING_STATUS=` ps -ef| grep "haproxy -f" | egrep - v grep | wc -l`
if [ $RUNNING_STATUS - ge 1 ]; then
echo "Haproxy is already running! Exit now."
exit 1
else
$PROG -f $CONFFILE
[ $? - eq 0 ] && echo "Start haproxy successful." || echo "Start haproxy failed."
fi
}
stop(){
if [ $RUNNING_STATUS -lt 1 ]; then
echo "Haproxy is not running. Stop haproxy failed!"
else
kill -9 $( cat $PIDFILE)
[ $? - eq 0 ] && echo "Stop haproxy successful." || echo "Stop haproxy failed."
rm -rf $PIDFILE
fi
}
reload(){
if [ ! -f $PIDFILE ]; then
echo "No pid file found. Maybe you need to check haproxy status first."
exit 1
else
$PROG -f $CONFFILE -sf $( cat $PIDFILE)
fi
}
status(){
if [ $RUNNING_STATUS - ge 1 ]; then
PID_NUM=` cat $PIDFILE`
echo "Haproxy (pid $PID_NUM) is running..."
else
echo "Haproxy is stopped."
fi
}
check(){
$PROG -f $CONFFILE -c
}
case $1 in start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
reload)
reload
;;
status)
status
;;
check)
check
;;
*)
echo "USAGE: $0 start|stop|restart|reload|check ."
exit 1
;;
esac
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[[email protected] ~] # cp haproxy /etc/init.d/
[[email protected] ~] # ll /etc/init.d/haproxy
-rwxr-xr-x 1 root root 1567 Oct 1 00:06 /etc/init .d /haproxy
[[email protected] ~] # service haproxy status
Haproxy (pid 2575) is running...
[[email protected] ~] # service haproxy stop
Stop haproxy successful.
[[email protected] ~] # service haproxy start
Start haproxy successful.
[[email protected] ~] # service haproxy restart
Stop haproxy successful.
Start haproxy successful.
[[email protected] ~] # service haproxy status
Haproxy (pid 2796) is running...
[[email protected] ~] # service haproxy reload
[[email protected] ~] # service haproxy status
Haproxy (pid 2822) is running...
[[email protected] ~] # service haproxy check
Configuration file is valid
|