heartbeat+Haproxy多VIP负载均衡高可用

时间:2023-02-10 03:20:14
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://jerry12356.blog.51cto.com/4308715/1858448

环境就不多做介绍了,还是上一篇中用到的四台机器,这里只是之前Heartbeat+Haproxy实现负载均衡高可用的补充罢了,废话少说,进入正题。

本文的目的将实现heartbeat绑定多个VIP,多个VIP又将分别代理多个不同的web服务,这些web服务之间做负载均衡,而VIP是高可用,进而实现haproxy的高可用。

主机名 角色 IP地址 说明
mylinux1.contoso.com Heartbeat+Haproxy

eth0:192.168.100.121

eth1:172.16.100.121

VIP:192.168.100.120
mylinux2.contoso.com Heartbeat+Haproxy

eth0:192.168.100.122

eth1:172.16.100.122

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

这里heartbeat服务将产生两个VIP,mylinux1上默认启动VIP 192.168.100.120,而mylinux2上默认启动VIP 192.168.100.110,当某一台发生故障时,另一台将接管故障服务器的VIP。Haproxy两个服务器的配置相同,都将绑定192.168.100.110和192.168.100.120两个IP地址,从而达到高可用的目的。

注意:大家应该注意到,如果将两个VIP都绑定到同一台服务器上,然后让heartbeat控制haproxy服务,也可以达到上面的目的,但是这样的话,无论何时必定有一台主机获得两个VIP,且提供代理服务,而另外一个主机可能什么服务都没有,完全处于备用状态,为了充分利用服务器资源,所以不采用这种方式,因此才有了本文的介绍。

一、配置heartbeat

heartbeat的配置就不多介绍了,这里主要是修改haresources文件。

1234567 [root@mylinux1 conf]# vi /etc/ha.d/haresources [root@mylinux1 conf]# tail -2 /etc/ha.d/haresources mylinux1.contoso.com IPaddr::192.168.100.120/24/eth0mylinux2.contoso.com IPaddr::192.168.100.110/24/eth0[root@mylinux2 ~]# tail -2 /etc/ha.d/haresources mylinux1.contoso.com IPaddr::192.168.100.120/24/eth0mylinux2.contoso.com IPaddr::192.168.100.110/24/eth0

二、修改haproxy配置文件

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 [root@mylinux1 conf]# cat haproxy.cfg# this config needs haproxy-1.1.28 or haproxy-1.2.1global        #log 127.0.0.1  local0log 127.0.0.1:514  local0  warning        pidfile /usr/local/haproxy/var/run/haproxy.pid        daemonmaxconn 4096chroot /usr/local/haproxy/var/chrootuser haproxygroup haproxy        nbproc 1defaultslogglobal        mode    httpretries3        option  httplog        option  httpclose        option  dontlognull        option  forwardforoption  redispatchmaxconn2000        balance roundrobintimeout connect 5000timeout client  50000timeoutserver          50000listen  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-versionlistenwebsites_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 3listenwebsites_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[root@mylinux1 conf]# scp haproxy.cfg mylinux2:/usr/local/haproxy/conf/root@mylinux2's password: haproxy.cfg                                   100% 1608     1.6KB/s   00:00

注意,要保证mylinux1和mylinux2上的配置文件一模一样。

三、同时启动heartbeat服务

12345678 [root@mylinux1 conf]# /etc/init.d/heartbeat startStarting High-Availability services: INFO:  Resource is stoppedINFO:  Resource is stoppedDone.[root@mylinux2 conf]# /etc/init.d/heartbeat startStarting High-Availability services: INFO:  Resource is stoppedINFO:  Resource is stoppedDone.

最后,要确保VIP成功绑定:

1234 [root@mylinux1 conf]# ip a |grep 120    inet 192.168.100.120/24 brd 192.168.100.255 scope global secondary eth0[root@mylinux2 conf]# ip a |grep 110    inet 192.168.100.110/24 brd 192.168.100.255 scope global secondary eth0

四、启动haproxy服务

在mylinux1上启动haproxy服务:

12345 [root@mylinux1 conf]# service haproxy start[ALERT] 275/163638 (2078) : Starting proxy websites_02: cannot bind socket [192.168.100.110:80]Start haproxy failed.[root@mylinux1 conf]# ps -ef|grep haproxyroot       2080   1035  0 16:36 pts/0    00:00:00 grep haproxy

发现无法启动,错误是因为无法绑定IP地址192.168.100.110,所以启动不成功。同样的,在mylinux2上也因为无法绑定IP地址192.168.100.120而无法启动。

解决方法:

在/etc/sysctl.conf中添加如下配置:

net.ipv4.ip_nonlocal_bind = 1

123456789101112131415161718192021222324 [root@mylinux1 conf]# echo "net.ipv4.ip_nonlocal_bind = 1" >>/etc/sysctl.conf[root@mylinux1 conf]# tail -1 /etc/sysctl.conf net.ipv4.ip_nonlocal_bind = 1[root@mylinux1 conf]# sysctl -pnet.ipv4.ip_forward = 1net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.default.accept_source_route = 0kernel.sysrq = 0kernel.core_uses_pid = 1net.ipv4.tcp_syncookies = 1kernel.msgmnb = 65536kernel.msgmax = 65536kernel.shmmax = 68719476736kernel.shmall = 4294967296fs.file-max = 2097152fs.nr_open = 2097152net.ipv4.tcp_syncookies = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_fin_timeout = 30net.ipv4.tcp_keepalive_time = 1200net.ipv4.ip_local_port_range = 1024 65000net.ipv4.tcp_max_syn_backlog = 81920net.ipv4.ip_nonlocal_bind = 1
12345678910111213141516 [root@mylinux2 conf]# echo "net.ipv4.ip_nonlocal_bind = 1" >>/etc/sysctl.conf[root@mylinux2 conf]# sysctl -pnet.ipv4.ip_forward = 1net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.default.accept_source_route = 0kernel.sysrq = 0kernel.core_uses_pid = 1net.ipv4.tcp_syncookies = 1error: "net.bridge.bridge-nf-call-ip6tables" is an unknown keyerror: "net.bridge.bridge-nf-call-iptables" is an unknown keyerror: "net.bridge.bridge-nf-call-arptables" is an unknown keykernel.msgmnb = 65536kernel.msgmax = 65536kernel.shmmax = 68719476736kernel.shmall = 4294967296net.ipv4.ip_nonlocal_bind = 1

然后再次尝试启动haproxy服务:

12345 [root@mylinux1 conf]# service haproxy startStart haproxy successful.[root@mylinux1 conf]# ps -ef|grep haproxyhaproxy    2102      1  0 16:43 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfgroot       2104   1035  0 16:43 pts/0    00:00:00 grep haproxy
12345 [root@mylinux2 conf]# service haproxy startStart haproxy successful.[root@mylinux2 conf]# ps -ef|grep haproxyhaproxy    3225      1  0 16:44 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfgroot       3227   2036  0 16:44 pts/0    00:00:00 grep haproxy

五、测试代理访问

heartbeat+Haproxy多VIP负载均衡高可用

heartbeat+Haproxy多VIP负载均衡高可用

访问192.168.100.120,是转发给http://192.168.100.181:8001/和http://192.168.100.182:8001/,没有问题。

heartbeat+Haproxy多VIP负载均衡高可用

heartbeat+Haproxy多VIP负载均衡高可用

访问192.168.100.110,是转发给http://192.168.100.181:8002/和http://192.168.100.182:8002/,这里也显示正常。

12345678910111213141516171819202122 [root@mylinux1 conf]# for i in {1..10};do curl http://192.168.100.120/;doneweb1web3web1web3web1web3web1web3web1web3[root@mylinux1 conf]# for i in {1..10};do curl http://192.168.100.110/;doneweb2web4web2web4web2web4web2web4web2web4

在linux上进行测试,代理访问也正常。

六、模拟故障切换

这里将mylinux1上的heartbeat服务关闭,然后再进行代理访问测试。

12345 [root@mylinux1 conf]# /etc/init.d/heartbeat stopStopping High-Availability services: Done.[root@mylinux1 conf]# ip a |grep 192.168.100.120[root@mylinux1 conf]# service haproxy statusHaproxy (pid  2102) is running...
123456 [root@mylinux2 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[root@mylinux2 conf]# service haproxy statusHaproxy (pid  3225) is running...
12345678910111213141516171819202122 [root@mylinux1 conf]# for i in {1..10};do curl http://192.168.100.120/;doneweb1web3web1web3web1web3web1web3web1web3[root@mylinux1 conf]# for i in {1..10};do curl http://192.168.100.110/;doneweb2web4web2web4web2web4web2web4web2web4

然后将mylinux1的heartbeat服务开启,同时将mylinux2的heartbeat服务关闭,再次进行代理访问测试。

123456 [root@mylinux1 conf]# /etc/init.d/heartbeat startStarting High-Availability services: INFO:  Resource is stoppedINFO:  Resource is stoppedDone.[root@mylinux2 conf]# /etc/init.d/heartbeat stopStopping High-Availability services: Done.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 [root@mylinux3 conf]# for i in {1..1000};do curl http://192.168.100.120/;sleep 1;doneweb3web1web3web1web3web1web3web1web3web1web3web1web3web1web1web3web1web3web1web3web1web3web1web3web1web3web1web3web1web3web1web3web1web3[root@mylinux4 conf]# for i in {1..1000};do curl http://192.168.100.110/;sleep 1;doneweb4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4web2web4

在客户端上的测试发现,VIP的转移基本没有造成服务的中断,说明haproxy代理服务高可用设置成功。

七、建议配置

因为heartbeat的停止或者服务器宕机都会影响VIP的切换,但是haproxy服务需要自动启动,而不能由heartbeat控制,所以建议在开机启动项中把haproxy设置为开机启动,至于heartbeat服务,不建议设置开机启动,以防止出现裂脑现象。

12345678 [root@mylinux1 conf]# chkconfig --add haproxy[root@mylinux1 conf]# chkconfig haproxy on[root@mylinux1 conf]# chkconfig --list haproxyhaproxy        0:off1:off2:on3:on4:on5:on6:off[root@mylinux2 conf]# chkconfig --add haproxy[root@mylinux2 conf]# chkconfig haproxy on[root@mylinux2 conf]# chkconfig --list haproxyhaproxy        0:off1:off2:on3:on4:on5:on6:off

注意:要让haproxy脚本能添加到chkconfig列表中去,需要添加如下内容:

#!/bin/bash

#

#chkconfig: 2345 20 70

#description: Start and stop haproxy service.

#

...

这样,以后只需要在重启服务器后手动开启heartbeat服务即可,即使出现脑裂,也只需要人为的处理一下,从而避免了haproxy服务的维护,提高了工作效率。


本文出自 “IT小二郎” 博客,请务必保留此出处http://jerry12356.blog.51cto.com/4308715/1858448