设为首页 收藏本站
查看: 818|回复: 0

[经验分享] Heartbeat+Haproxy实现负载均衡高可用

[复制链接]

尚未签到

发表于 2019-1-6 15:29:05 | 显示全部楼层 |阅读模式
  环境说明:
主机名角色IP地址
mylinux1.contoso.comheartbeat+haproxy  eth0:192.168.100.121
  eth1:172.16.100.121
mylinux2.contoso.comheartbeat+haproxy  eth0:192.168.100.122
  eth1:172.16.100.122
mylinux3.contoso.comweb server 1eth0:192.168.100.181
mylinux4.contoso.comweb server 2eth0:192.168.100.182
  一、环境准备
  在mylinux1和mylinux2上安装heartbeat,可以手动安装,当然也可以使用前面发布的一篇heartbeat脚本的博文,里面有heartbeat快速安装脚本。
  然后在mylinux2上安装haproxy,并将mylinux1上的haproxy配置文件和启停脚本都拷贝到mylinux2上。
  要保证mylinux1和mylinux2的heartbeat+haproxy环境一致。
[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的资源文件
[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配置,如下:
[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服务

[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服务
[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的状态

[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的状态

[root@mylinux2 haproxy]# ip a |grep 192.168.100.120
[root@mylinux2 haproxy]# service haproxy status
Haproxy is stopped.  3、查看监控界面

  五、测试故障切换
  1、停止mylinux1上的heartbeat服务
[root@mylinux1 ~]# /etc/init.d/heartbeat stop
Stopping High-Availability services: Done.  2、检查mylinux1和mylinux2的状态
[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、查看监控页面

  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. :

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-660036-1-1.html 上篇帖子: DRBD+HeartBeat+NFS 架构 下篇帖子: Heartbeat3.x安装、错误记录
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表