heartbeat+Haproxy多VIP负载均衡高可用
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://jerry12356.blog.51cto.com/4308715/1858448
主机名 | 角色 | IP地址 | 说明 |
mylinux1.contoso.com | Heartbeat+Haproxy | VIP:192.168.100.120 | |
mylinux2.contoso.com | Heartbeat+Haproxy | VIP:192.168.100.110 | |
mylinux3.contoso.com | apache | eth0:192.168.100.181 | Web:80,8001,8002 |
mylinux4.contoso.com | apache | eth0:192.168.100.182 | Web:80,8001,8002 |
1
2
3
4
5
6
7
|
[[email protected] conf] # vi /etc/ha.d/haresources
[[email protected] conf] # tail -2 /etc/ha.d/haresources
mylinux1.contoso.com IPaddr::192.168.100.120 /24/eth0
mylinux2.contoso.com IPaddr::192.168.100.110 /24/eth0
[[email protected] ~] # tail -2 /etc/ha.d/haresources
mylinux1.contoso.com IPaddr::192.168.100.120 /24/eth0
mylinux2.contoso.com IPaddr::192.168.100.110 /24/eth0
|
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
|
[[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
logglobal
mode http
retries3
option httplog
option httpclose
option dontlognull
option forwardfor
option redispatch
maxconn2000
balance roundrobin
timeout connect 5000
timeout client 50000
timeoutserver 50000
listen haproxy_stats
bind *:8000
mode http
option httplog
maxconn 20
stats enable
stats refresh 30s
stats uri /haproxy_status
stats auth admin:123456
stats hide-version
listenwebsites_01
bind 192.168.100.120:80
option forwardfor
#option httpchk GET /info.txt
#option httpchk HEAD /check.html HTTP/1.0
timeout server 15s
timeout connect 30s
server web1 192.168.100.181:8001 check port 8001 inter 2000 fall 3
server web2 192.168.100.182:8001 check port 8001 inter 2000 fall 3
listenwebsites_02
bind 192.168.100.110:80
option forwardfor
#option httpchk GET /info.txt
#option httpchk HEAD /check.html HTTP/1.0
timeout server 15s
timeout connect 30s
server web1 192.168.100.181:8002 check port 8002 inter 2000 fall 3
server web2 192.168.100.182:8002 check port 8002 inter 2000 fall 3
[[email protected] conf] # scp haproxy.cfg mylinux2:/usr/local/haproxy/conf/
[email protected]'s password:
haproxy.cfg 100% 1608 1.6KB /s 00:00
|
1
2
3
4
5
6
7
8
|
[[email protected] conf] # /etc/init.d/heartbeat start
Starting High-Availability services: INFO: Resource is stopped
INFO: Resource is stopped
Done.
[[email protected] conf] # /etc/init.d/heartbeat start
Starting High-Availability services: INFO: Resource is stopped
INFO: Resource is stopped
Done.
|
1
2
3
4
|
[[email protected] conf] # ip a |grep 120
inet 192.168.100.120 /24 brd 192.168.100.255 scope global secondary eth0
[[email protected] conf] # ip a |grep 110
inet 192.168.100.110 /24 brd 192.168.100.255 scope global secondary eth0
|
1
2
3
4
5
|
[[email protected] conf] # service haproxy start
[ALERT] 275 /163638 (2078) : Starting proxy websites_02: cannot bind socket [192.168.100.110:80]
Start haproxy failed.
[[email protected] conf] # ps -ef|grep haproxy
root 2080 1035 0 16:36 pts /0 00:00:00 grep haproxy
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[[email protected] conf] # echo "net.ipv4.ip_nonlocal_bind = 1" >>/etc/sysctl.conf
[[email protected] conf] # tail -1 /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
[[email protected] conf] # sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
fs. file -max = 2097152
fs.nr_open = 2097152
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_max_syn_backlog = 81920
net.ipv4.ip_nonlocal_bind = 1
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[[email protected] conf] # echo "net.ipv4.ip_nonlocal_bind = 1" >>/etc/sysctl.conf
[[email protected] conf] # sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key
error: "net.bridge.bridge-nf-call-iptables" is an unknown key
error: "net.bridge.bridge-nf-call-arptables" is an unknown key
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.ip_nonlocal_bind = 1
|
1
2
3
4
5
|
[[email protected] conf] # service haproxy start
Start haproxy successful.
[[email protected] conf] # ps -ef|grep haproxy
haproxy 2102 1 0 16:43 ? 00:00:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy .cfg
root 2104 1035 0 16:43 pts /0 00:00:00 grep haproxy
|
1
2
3
4
5
|
[[email protected] conf] # service haproxy start
Start haproxy successful.
[[email protected] conf] # ps -ef|grep haproxy
haproxy 3225 1 0 16:44 ? 00:00:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy .cfg
root 3227 2036 0 16:44 pts /0 00:00:00 grep haproxy
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[[email protected] conf] # for i in {1..10};do curl http://192.168.100.120/;done
web1
web3
web1
web3
web1
web3
web1
web3
web1
web3
[[email protected] conf] # for i in {1..10};do curl http://192.168.100.110/;done
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
|
1
2
3
4
5
|
[[email protected] conf] # /etc/init.d/heartbeat stop
Stopping High-Availability services: Done.
[[email protected] conf] # ip a |grep 192.168.100.120
[[email protected] conf] # service haproxy status
Haproxy (pid 2102) is running...
|
1
2
3
4
5
6
|
[[email protected] conf] # ip a |grep 192.168.100
inet 192.168.100.122 /24 brd 192.168.100.255 scope global eth0
inet 192.168.100.110 /24 brd 192.168.100.255 scope global secondary eth0
inet 192.168.100.120 /24 brd 192.168.100.255 scope global secondary eth0
[[email protected] conf] # service haproxy status
Haproxy (pid 3225) is running...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[[email protected] conf] # for i in {1..10};do curl http://192.168.100.120/;done
web1
web3
web1
web3
web1
web3
web1
web3
web1
web3
[[email protected] conf] # for i in {1..10};do curl http://192.168.100.110/;done
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
|
1
2
3
4
5
6
|
[[email protected] conf] # /etc/init.d/heartbeat start
Starting High-Availability services: INFO: Resource is stopped
INFO: Resource is stopped
Done.
[[email protected] conf] # /etc/init.d/heartbeat stop
Stopping High-Availability services: Done.
|
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
79
80
81
82
83
84
85
86
87
|
[[email protected] conf] # for i in {1..1000};do curl http://192.168.100.120/;sleep 1;done
web3
web1
web3
web1
web3
web1
web3
web1
web3
web1
web3
web1
web3
web1
web1
web3
web1
web3
web1
web3
web1
web3
web1
web3
web1
web3
web1
web3
web1
web3
web1
web3
web1
web3
[[email protected] conf] # for i in {1..1000};do curl http://192.168.100.110/;sleep 1;done
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
web2
web4
|
1
2
3
4
5
6
7
8
|
[[email protected] conf] # chkconfig --add haproxy
[[email protected] conf] # chkconfig haproxy on
[[email protected] conf] # chkconfig --list haproxy
haproxy 0:off1:off2:on3:on4:on5:on6:off
[[email protected] conf] # chkconfig --add haproxy
[[email protected] conf] # chkconfig haproxy on
[[email protected] conf] # chkconfig --list haproxy
haproxy 0:off1:off2:on3:on4:on5:on6:off
|