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

环境就不多做介绍了,还是上一篇中用到的四台机器,这里只是之前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文件。

[[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

二、修改haproxy配置文件

[[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

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

三、同时启动heartbeat服务

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

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

[[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

四、启动haproxy服务

在mylinux1上启动haproxy服务:

[[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

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

解决方法:

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

net.ipv4.ip_nonlocal_bind = 1

[[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
[[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

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

[[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
[[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

五、测试代理访问

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

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

[[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

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

六、模拟故障切换

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

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

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

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

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

七、建议配置

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

[[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

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

#!/bin/bash

#

#chkconfig: 2345 20 70

#description: Start and stop haproxy service.

#

...

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

时间: 2024-12-23 06:40:52

heartbeat+Haproxy多VIP负载均衡高可用的相关文章

HAProxy+Keepalived实现负载均衡高可用

一.环境 5台虚拟机,分别是: 1台测试机(192.168.2.83); 2台haproxy/keepalived(192.168.2.230/192.168.2.231); 2台Web Servers(192.168.2.232/192.168.2.233); 注:VIP设置为 192.168.2.228: 二.安装配置Web Server 由于Web Server的安装与配置非常简单,根据自己喜好,安装一个即可:比如:apache.nginx.tomcat等等.在此就不再详述: 三.安装配置

linux系统下对网站实施负载均衡+高可用集群需要考虑的几点

随着linux系统的成熟和广泛普及,linux运维技术越来越受到企业的关注和追捧.在一些中小企业,尤其是牵涉到电子商务和电子广告类的网站,通常会要求作负载均衡和高可用的Linux集群方案. 那么如何实施linux集群架构,才能既有效保证网站健康运行,又能节省运维成本呢?下面依据近几年的运维经历,简单梳理下自己的一点感悟. (1)机房的选择如果有自己公司的机房那是再好不过的了:如果没有,建议放在BGP机房内托管,如果有选择的话,最好是选择带有硬件防火墙的机房,这样在安全方面也有保障:网站如若是放在

HAproxy+Keepalived负载均衡-高可用web站

haproxy+keepalived负载均衡高可用web站   OS IP 子网掩码 路由网关 Centos6.6 HAproxy Keepalived Eth0:192.168.26.210 255.255.252.0 192.168.25.3 VIP:192.168.27.210 Centos6.6 HAporxy Keepalived Eth0:192.168.26.211 255.255.252.0 192.168.25.3 VIP:192.168.27.210 Centos6.6(WE

CentOS Linux 负载均衡高可用WEB集群之LVS+Keepalived配置

CentOS Linux 负载均衡高可用WEB集群之LVS+Keepalived配置 LB集群是locd balance集群的简称.翻译成中文是:负载均衡集群的意思:集群是一组相互独立的.通过高速网络互联的计算机相互之间构成一个组合,并以单一的系统的模式加以管理.LVS是Linux Virtual Server的简写,翻译中文是Linux虚拟服务器,是一个虚拟的服务器集群系统. 负载均衡集群:是为了企业提供更为实用,性价比更高的系统机构解决方案.负载均衡集群把用户的请求尽可能的平均分发到集群的各

Nginx + Keepalived(主备模式)实现负载均衡高可用浅析

概述 目前关于负载均衡和高可用的架构方案能找到相当多且详尽的资料,此篇是自己学习相关内容的一个总结,防止将来遗忘再次重新查找资料,也避免踩相同的坑. 此次配置的负载均衡与高可用架构:Nginx + Keepalived(主备模式),Nginx 使用反向代理实现七层负载均衡. 众所周知,Nginx 是一款自由的.开源的.高性能HTTP服务器和反向代理服务器,也是一个IMAP.POP3.SMTP代理服务器. 也就是说Nginx本身就可以托管网站(类似于Tomcat一样),进行HTTP服务处理,也可以

负载均衡高可用之LVS+Keepalived(DR/主备)+apache

负载均衡高可用之LVS+Keepalived(DR/主备)+apache 介绍: LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一. LVS集群采用IP负载均衡技术和基于内容请求分发技术.调度器具有很好的吞吐率,将请求均衡地转移到不同的服务器上执行,且调度器自动屏蔽掉服务器的故障,从而将一组服务器构成一个高性能的.高可用的虚拟服务器.整个服务器集群的结构对客户

Nginx+Keepalived 实现反代 负载均衡 高可用(HA)配置

Nginx+Keepalived实现反代负载均衡高可用(HA)配置 Nginx+Keepalived实现反代负载均衡高可用配置 OS IP 子网掩码 路由网关 Centos6.6 nginx Keepalived Eth0:192.168.26.210 255.255.252.0 192.168.25.3 VIP:192.168.27.210 Centos6.6 Nginx Keepalived Eth0:192.168.26.211 255.255.252.0 192.168.25.3 VIP

CentOS Linux 负载均衡高可用WEB集群之Nginx+Keepalived配置

Nginx+Keepalived实现负载均衡高可用的WEB服务集群,nginx作为负载均衡器,keepalived作为高可用,当其中的一台负载均衡器(nginx)发生故障时可以迅速切换到备用的负载均衡器(nginx),保持业务的连续性. 1.服务器的环境配置及IP分配 操作系统:CentOS release 6.7 (Final) nginx版本:nginx/1.8.0 keepalived版本:Keepalived v1.2.13 Nginx + keepalived服务器的IP分配表 服务器

LVS+keepalived实现负载均衡&高可用

一.实验环境需求&准备 我们这次实验要完成的一个架构如下图所示,我们通过LVS-DR-MASTER,LVS-DR-BACKUP作为LVS负载均衡调度器,并且两者之间通过keepalived来两者之间的HA.keepalived本身就是为了LVS为开发的,所以说我们通过keepalived来进行LVS的配置就显得十分的方便.而且keepalived是直接操作ip_vs不用通过ipvsadm,所以更加方便. 1)实验架构图&需求表: 角色 IP地址 备注 主LVS调度器(MASTER) 192