Heartbeat+Haproxy实现负载均衡高可用

简介:

环境说明:

主机名 角色 IP地址
mylinux1.contoso.com heartbeat+haproxy

eth0:192.168.100.121

eth1:172.16.100.121

mylinux2.contoso.com heartbeat+haproxy

eth0:192.168.100.122

eth1:172.16.100.122

mylinux3.contoso.com web server 1 eth0:192.168.100.181
mylinux4.contoso.com web server 2 eth0:192.168.100.182

一、环境准备

在mylinux1和mylinux2上安装heartbeat,可以手动安装,当然也可以使用前面发布的一篇heartbeat脚本的博文,里面有heartbeat快速安装脚本。

然后在mylinux2上安装haproxy,并将mylinux1上的haproxy配置文件和启停脚本都拷贝到mylinux2上。

要保证mylinux1和mylinux2的heartbeat+haproxy环境一致。

1
2
3
4
5
6
7
8
9
10
11
12
[root@mylinux1 ~] # scp /etc/init.d/haproxy mylinux2:/etc/init.d/
root@mylinux2's password: 
Permission denied, please try again.
root@mylinux2's password: 
haproxy                                       100% 1567     1.5KB /s    00:00    
[root@mylinux1 ~] # scp /usr/local/haproxy/conf/haproxy.cfg mylinux2:/usr/local/haproxy/conf/haproxy.cfg
root@mylinux2's password: 
haproxy.cfg                                   100% 1194     1.2KB /s    00:00   
[root@mylinux2 haproxy] # ll /etc/init.d/haproxy 
-rwxr-xr-x 1 root root 1567 Oct  1 02:11  /etc/init .d /haproxy
[root@mylinux2 haproxy] # ll /usr/local/haproxy/conf/haproxy.cfg 
-rw-r--r-- 1 root root 1194 Oct  1 02:13  /usr/local/haproxy/conf/haproxy .cfg

二、修改heartbeat的资源文件

1
2
3
4
[root@mylinux1 ~] # tail -1 /etc/ha.d/haresources 
mylinux1.contoso.com IPaddr::192.168.100.120 /24/eth0  haproxy
[root@mylinux2 haproxy] # tail -1 /etc/ha.d/haresources 
mylinux1.contoso.com IPaddr::192.168.100.120 /24/eth0  haproxy

这里还是贴出两个heartbeat节点的ha.cf配置,如下:

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
[root@mylinux1 ~] # egrep -v "#|^$" /etc/ha.d/ha.cf
debugfile  /var/log/ha-debug
logfile /var/log/ha-log
logfacilitylocal1
keepalive 2
deadtime 30
warntime 10
initdead 120
udpport694
mcast eth1 225.0.0.1 694 1 0
auto_failback on
nodemylinux1.contoso.com
nodemylinux2.contoso.com
respawn hacluster  /usr/lib64/heartbeat/ipfail
[root@mylinux2 haproxy] # egrep -v "#|^$" /etc/ha.d/ha.cf 
debugfile  /var/log/ha-debug
logfile /var/log/ha-log
logfacilitylocal1
keepalive 2
deadtime 30
warntime 10
initdead 120
udpport694
mcast eth1 225.0.0.1 694 1 0
auto_failback on
node    mylinux1.contoso.com
node    mylinux2.contoso.com
respawn hacluster  /usr/lib64/heartbeat/ipfail

三、启动heartbeat服务

1、首先在mylinux1和mylinux2上先关闭haproxy服务

1
2
3
4
[root@mylinux1 ~] # service haproxy stop
Stop haproxy successful.
[root@mylinux2 haproxy] # service haproxy stop
Haproxy is not running. Stop haproxy failed!

2、依次启动mylinux1和mylinux2上的heartbeat服务

1
2
3
4
5
6
[root@mylinux1 ~] # /etc/init.d/heartbeat start
Starting High-Availability services: INFO:  Resource is stopped
Done.
[root@mylinux2 haproxy] # /etc/init.d/heartbeat start
Starting High-Availability services: INFO:  Resource is stopped
Done.

四、测试haproxy的高可用

1、首先,检查mylinux1的状态

1
2
3
4
[root@mylinux1 ~] # ip a|grep 192.168.100.120
     inet 192.168.100.120 /24  brd 192.168.100.255 scope global secondary eth0
[root@mylinux1 ~] # service haproxy status
Haproxy (pid  3600) is running...

2、其次,检查mylinux2的状态

1
2
3
[root@mylinux2 haproxy] # ip a |grep 192.168.100.120
[root@mylinux2 haproxy] # service haproxy status
Haproxy is stopped.

3、查看监控界面

wKiom1fusjWzvOP8AAKGAPq71lk888.png-wh_50

五、测试故障切换

1、停止mylinux1上的heartbeat服务

1
2
[root@mylinux1 ~] # /etc/init.d/heartbeat stop
Stopping High-Availability services: Done.

2、检查mylinux1和mylinux2的状态

1
2
3
4
5
6
7
[root@mylinux1 ~] # ip a |grep 192.168.100.120
[root@mylinux1 ~] # service haproxy status
Haproxy is stopped.
[root@mylinux2 haproxy] # ip a |grep 192.168.100.120
     inet 192.168.100.120 /24  brd 192.168.100.255 scope global secondary eth0
[root@mylinux2 haproxy] # service haproxy status
Haproxy (pid  3997) is running...

3、查看监控页面

wKiom1fusybiVMthAAKLJ2hGum8420.png-wh_50

4、查看mylinux2上的heartbeat日志

[root@mylinux2 ~]# >/var/log/ha-log 

[root@mylinux2 ~]# tail -f /var/log/ha-log 

Oct 01 02:44:31 mylinux2.contoso.com heartbeat: [3534]: info: Received shutdown notice from 'mylinux1.contoso.com'.

Oct 01 02:44:31 mylinux2.contoso.com heartbeat: [3534]: info: Resources being acquired from mylinux1.contoso.com.

Oct 01 02:44:31 mylinux2.contoso.com heartbeat: [3655]: info: acquire local HA resources (standby).

Oct 01 02:44:31 mylinux2.contoso.com heartbeat: [3656]: info: No local resources [/usr/share/heartbeat/ResourceManager listkeys mylinux2.contoso.com] to acquire.

Oct 01 02:44:31 mylinux2.contoso.com heartbeat: [3655]: info: local HA resource acquisition completed (standby).

Oct 01 02:44:31 mylinux2.contoso.com heartbeat: [3534]: info: Standby resource acquisition done [all].

harc(default)[3681]:2016/10/01_02:44:31 info: Running /etc/ha.d//rc.d/status status

mach_down(default)[3698]:2016/10/01_02:44:31 info: Taking over resource group IPaddr::192.168.100.120/24/eth0

ResourceManager(default)[3725]:2016/10/01_02:44:31 info: Acquiring resource group: mylinux1.contoso.com IPaddr::192.168.100.120/24/eth0 haproxy

/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.100.120)[3753]:       2016/10/01_02:44:31 INFO:  Resource is stopped

ResourceManager(default)[3725]:2016/10/01_02:44:31 info: Running /etc/ha.d/resource.d/IPaddr 192.168.100.120/24/eth0 start

IPaddr(IPaddr_192.168.100.120)[3878]:2016/10/01_02:44:32 INFO: Adding inet address 192.168.100.120/24 with broadcast address 192.168.100.255 to device eth0

IPaddr(IPaddr_192.168.100.120)[3878]:2016/10/01_02:44:32 INFO: Bringing device eth0 up

IPaddr(IPaddr_192.168.100.120)[3878]:2016/10/01_02:44:32 INFO: /usr/libexec/heartbeat/send_arp -i 200 -r 5 -p /var/run/resource-agents/send_arp-192.168.100.120 eth0 192.168.100.120 auto not_used not_used

/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.100.120)[3852]:       2016/10/01_02:44:32 INFO:  Success

ResourceManager(default)[3725]:2016/10/01_02:44:32 info: Running /etc/init.d/haproxy  start

mach_down(default)[3698]:2016/10/01_02:44:32 info: /usr/share/heartbeat/mach_down: nice_failback: foreign resources acquired

mach_down(default)[3698]:2016/10/01_02:44:32 info: mach_down takeover complete for node mylinux1.contoso.com.

Oct 01 02:44:32 mylinux2.contoso.com heartbeat: [3534]: info: mach_down takeover complete.

Oct 01 02:45:02 mylinux2.contoso.com heartbeat: [3534]: WARN: node mylinux1.contoso.com: is dead

Oct 01 02:45:02 mylinux2.contoso.com heartbeat: [3534]: info: Dead node mylinux1.contoso.com gave up resources.

Oct 01 02:45:02 mylinux2.contoso.com heartbeat: [3534]: info: Link mylinux1.contoso.com:eth1 dead.

Oct 01 02:45:02 mylinux2.contoso.com ipfail: [3543]: info: Status update: Node mylinux1.contoso.com now has status dead

Oct 01 02:45:03 mylinux2.contoso.com ipfail: [3543]: info: NS: We are dead. :<

Oct 01 02:45:03 mylinux2.contoso.com ipfail: [3543]: info: Link Status update: Link mylinux1.contoso.com/eth1 now has status dead

Oct 01 02:45:04 mylinux2.contoso.com ipfail: [3543]: info: We are dead. :<

Oct 01 02:45:04 mylinux2.contoso.com ipfail: [3543]: info: Asking other side for ping node count.



本文转自 jerry1111111 51CTO博客,原文链接:http://blog.51cto.com/jerry12356/1858243,如需转载请自行联系原作者
相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
相关文章
|
3天前
|
负载均衡 监控 网络协议
使用haproxy实现负载均衡集群
【4月更文挑战第14天】HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,快速并且可靠的一种解决方案。
8 1
|
2月前
|
存储 缓存 运维
解密一致性哈希算法:实现高可用和负载均衡的秘诀
解密一致性哈希算法:实现高可用和负载均衡的秘诀
160 0
|
3月前
|
Kubernetes 负载均衡 监控
Kubernetes高可用集群二进制部署(一)主机准备和负载均衡器安装
Kubernetes高可用集群二进制部署(一)主机准备和负载均衡器安装
|
4月前
|
tengine Kubernetes Cloud Native
Tengine-Ingress 高性能高可用的云原生网关
Tengine-Ingress 高性能高可用的云原生网关
|
5月前
|
负载均衡 算法 网络协议
Keepalived+LVS搭建高可用负载均衡
Keepalived+LVS搭建高可用负载均衡
183 1
|
5月前
|
负载均衡 关系型数据库 PostgreSQL
Pgpool-II实现高可用+读写分离+负载均衡(八)---- 维护工具
Pgpool提供了一些维护工具,用于日常观察Pgpool运行状态、上线、下线节点等操作。主要有:pcp_stop_pgpool,pcp_node_count,pcp_node_info,pcp_health_check_stats,pcp_proc_count,pcp_proc_info,pcp_detach_node,pcp_attach_node,pcp_recovery_node,pcp_promote_node,pcp_pool_status,pcp_watchdog_info,pcp_reload_config
188 0
|
5月前
|
负载均衡 前端开发 网络协议
Keepalived+HAProxy 搭建高可用负载均衡(二)
Keepalived+HAProxy 搭建高可用负载均衡
|
17天前
|
负载均衡 算法 应用服务中间件
面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
字节跳动面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
32 0
|
3月前
|
负载均衡 应用服务中间件 nginx
百度搜索:蓝易云【Nginx和tomcat实现负载均衡教程】
至此,你已经成功地使用Nginx和Tomcat实现了负载均衡。Nginx将根据配置的负载均衡策略将客户端请求分发到多个Tomcat服务器上,以提高系统的性能和可用性。请注意,在实际生产环境中,还需要进行其他配置和优化,如健康检查、会话保持等,以满足具体的需求。
34 0
|
7天前
|
负载均衡 应用服务中间件 nginx
Nginx 负载均衡
Nginx 负载均衡
21 2