LVS—DR模式+keepalived实现负载均衡集群

负载均衡集群的实现

一.简介

1.LVS

  • LVS(Linux Virtual Server),意即Linux虚拟服务器,是一个虚拟的服务器集群系统
  • 宗旨是使用集群技术和Linux操作系统实现一个高性能、高可用的服务器.
  • 一般来说,LVS集群采用三层结构,其主要组成部分为:

    A、负载调度器(load balancer),它是整个集群对外面的前端机,负责将客户的请求发送到一组服务器上执行,而客户认为服务是来自一个IP地址(可称之为虚拟IP地址)上的。

    B、服务器池(server pool),是一组真正执行客户请求的服务器,执行的服务有WEB、MAIL、FTP和DNS等。

    C、共享存储(shared storage),它为服务器池提供一个共享的存储区,这样很容易使得服务器池拥有相同的内容,提供相同的服务。

2.VIP

  • VIP为调度器和服务器组共享,调度器配置的VIP是对外可见的,用于接收虚拟服务的请求报文

二.LVS—DR模式

  • DR模式原理图:

LVS—DR模式+keepalived实现负载均衡集群

  • DR模式的数据传输过程

        当客户端请求VIP时,会将请求先发给Director(调度器),调度器发现请求的是一组集群服务,根据调度算法将这一请求转发给RealServer,注意在转发的过程中,仅仅是修改了数据报文中的MAC地址(所以这也是为什么我们要求DR和RS必须在同一个物理网络内,就是为了保证可以通过修改MAC地址而进行数据报文的转发。)当RealServer处理请求,响应数据,发送响应数据给客户端,按理说此时的数据包的源IP为RIP,目标IP为CIP,虽然能找到客户端,但是客户端是不收该数据包的,因为并没有请求该RIP ,现在的做法就是进行IP欺骗,即就是修改源 IP 为 VIP,但是不可以将VIP设置在出口网卡上,否则会响应客户端的arp request,造成client/gateway arp table紊乱,以至于整个load balance都不能正常工作。要在lo接口上配置VIP,并将此 VIP 屏蔽,但是出去时候的数据包被路由转换,转换后的 IP不再是 VIP,所以要重新设置路由。

  • 特点

DR模式是通过改写请求报文的目标MAC地址,将请求发给真实服务器的,而真实服务器响应后的处理结果直接返回给客户端用户。同TUN模式一样,DR模式可以极大的提高集群系统的伸缩性。而且DR模式没有IP隧道的开销,对集群中的真实服务器也没有必要必须支持IP隧道协议的要求。但是要求调度器LB与真实服务器RS都有一块网卡连接到同一物理网段上,必须在同一个局域网环境。DR模式是互联网使用比较多的一种模式。

1.实验环境

主机名 ip 组成部分
server1 172.25.14.1 负载调度器LB
server2 172.25.14.2 RealServer
server3 172.25.14.3 RealServer
  • rhel6.5
  • iptables and selinux  disabled
  • VIP:172.25.14.100  
  • 网络yum源http://172.25.14.250/rhel6.5
  • server2,server3开启httpd服务

LVS—DR模式+keepalived实现负载均衡集群

 

配置步骤:

1)配置yum仓库


[[email protected] ~]# vim /etc/yum.repos.d/rhel-source.repo
[rhel-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.14.250/rhel6.5
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

[LoadBalancer]
name=LoadBalancer
baseurl=http://172.25.14.250/rhel6.5/LoadBalancer
enabled=1
gpgcheck=0
[[email protected] ~]# yum clean all

2)安装ipvsadm

[[email protected] ~]# yum install ipvsadm -y

3)添加调度策略

[[email protected] ~]# ipvsadm -A -t 172.25.14.100:80 -s rr  ##-A表示添加虚拟主机,-t表示tcp连接,-s表示负载均衡工作模式为轮询模式
[[email protected] ~]# ipvsadm -a -t 172.25.14.100:80 -r 172.25.14.2:80 -g  ##-a表示往一条记录中添加真实的服务主机,-g直连模式,-r表示指定真实主机的ip
[[email protected] ~]# ipvsadm -a -t 172.25.14.100:80 -r 172.25.14.3:80 -g  
[[email protected] ~]# /etc/init.d/ipvsadm save    ##保存添加的策略
ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm:      [  OK  ]
[[email protected] ~]# ipvsadm -l    ##查看调度策略
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.25.14.100:http rr
  -> server2:http                 Route   1      0          0         
  -> server3:http                 Route   1      0          0         

 

此时虽然调度器添加策略成功,但是因为调度器与后端服务器不能通信,虽然server1确实调度了,但是主机中不存在这个虚拟ip

172.25.14.100,所以不能发挥调度作用

4)在调度器中添加虚拟ip 172.25.14.100/24

[[email protected] ~]# ip addr add 172.25.14.100/24 dev eth0

5)在真实主机server2与server3添加虚拟主机ip172.25.14.100,使调度器可以与其通信

server2:

[[email protected] ~]# ip addr add 172.25.14.100/32 dev eth0     ##/32是为了让客户不能直接访问后端服务器
[[email protected] ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:38:17:dc brd ff:ff:ff:ff:ff:ff
    inet 172.25.14.2/24 brd 172.25.14.255 scope global eth0
    inet 172.25.14.100/32 scope global eth0          ##虚拟ip添加成功
    inet6 fe80::5054:ff:fe38:17dc/64 scope link 
       valid_lft forever preferred_lft forever

server3:

[[email protected] ~]# ip addr add 172.25.14.100/32 dev eth0
[[email protected] ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:31:6f:ca brd ff:ff:ff:ff:ff:ff
    inet 172.25.14.3/24 brd 172.25.14.255 scope global eth0
    inet 172.25.14.100/32 scope global eth0              ##虚拟ip添加成功
    inet6 fe80::5054:ff:fe31:6fca/64 scope link 
       valid_lft forever preferred_lft forever

6)测试:

此时,调度器与两台真实主机都有ip 172.25.14.100,所以此时访问会出现随机的两种情况

1.一种是访问调度器

LVS—DR模式+keepalived实现负载均衡集群

 

2.一种是访问真实主机中的某一个

LVS—DR模式+keepalived实现负载均衡集群

可以通过下面的方法查看访问的主机究竟是哪台:

[[email protected] ~]$ arp -an | grep 100    ##检测访问的网卡号

LVS—DR模式+keepalived实现负载均衡集群

可以看到此时访问的网卡号一直是与server1(调度器)相同

LVS—DR模式+keepalived实现负载均衡集群

 

也可以执行命令清除物理地址缓存,

[[email protected] kiosk]# arp -d 172.25.14.100

如果访问的是调度器

[[email protected] ~]# ipvsadm -l    ##查看调度记录,server2与server3轮询工作
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.25.14.100:http rr
  -> server2:http                 Route   1      0          2         
  -> server3:http                 Route   1      0          2   

7)编写调度器,是访问虚拟主机ip时只能连接到调度器

方法:arptables使用防火墙策略来限制用户访问服务器

server2:

[[email protected] ~]# yum install arptables_jf -y    ##安装arptables
[[email protected] ~]# arptables -A IN -d 172.25.14.100 -j DROP   ##-A表示添加策略,IN表示在表IN中操作,-d表示destation,-j表示指定的处理方式,DROP 表示拒绝访问不回应
[[email protected] ~]# arptables -A OUT -s 172.25.14.100 -j mangle --mangle-ip-s 172.25.14.2     ##管理输出,以ip172.25.14.2输出
[[email protected] ~]# arptables -L    ##查看arptables策略
Chain IN (policy ACCEPT)
target     source-ip            destination-ip       source-hw          destination-hw     hlen   op         hrd        pro       
DROP       anywhere             172.25.14.100        anywhere           anywhere           any    any        any        any       

Chain OUT (policy ACCEPT)
target     source-ip            destination-ip       source-hw          destination-hw     hlen   op         hrd        pro       
mangle     172.25.14.100        anywhere             anywhere           anywhere           any    any        any        any       --mangle-ip-s server2 

Chain FORWARD (policy ACCEPT)
target     source-ip            destination-ip       source-hw          destination-hw     hlen   op         hrd        pro       
[[email protected] ~]# /etc/init.d/arptables_jf save    ##保存arptables的更改
Saving current rules to /etc/sysconfig/arptables:          [  OK  ]

 

server3:

[[email protected] ~]# yum install arptables_jf -y     
[[email protected] ~]# arptables -A IN -d 172.25.14.100 -j DROP
[[email protected] ~]# arptables -A OUT -s 172.25.14.100 -j mangle --mangle-ip-s 172.25.14.3
[[email protected] ~]# arptables -L
Chain IN (policy ACCEPT)
target     source-ip            destination-ip       source-hw          destination-hw     hlen   op         hrd        pro       
DROP       anywhere             172.25.14.100        anywhere           anywhere           any    any        any        any       

Chain OUT (policy ACCEPT)
target     source-ip            destination-ip       source-hw          destination-hw     hlen   op         hrd        pro       
mangle     172.25.14.100        anywhere             anywhere           anywhere           any    any        any        any       --mangle-ip-s server3 

Chain FORWARD (policy ACCEPT)
target     source-ip            destination-ip       source-hw          destination-hw     hlen   op         hrd        pro       
[[email protected] ~]# /etc/init.d/arptables_jf save
Saving current rules to /etc/sysconfig/arptables:          [  OK  ]

 

测试:

负责测试的主机重复进行下面的操作
arp -d 172.25.14.100  ##清除物理地址缓存
curl 172.25.14.100   ##多次访问虚拟主机,此时可以看到每次访问的都是调度器的轮询工作模式

[[email protected] kiosk]# curl 172.25.14.100
<h1>172.25.14.2--server2</h1>
[[email protected] kiosk]# curl 172.25.14.100
<h1> 172.25.14.3--server3</h1>
[[email protected] kiosk]# curl 172.25.14.100
<h1>172.25.14.2--server2</h1>
[[email protected] kiosk]# curl 172.25.14.100
<h1> 172.25.14.3--server3</h1>
[[email protected] kiosk]# arp -d 172.25.14.100
[[email protected] kiosk]# curl 172.25.14.100
<h1>172.25.14.2--server2</h1>
[[email protected] kiosk]# curl 172.25.14.100
<h1> 172.25.14.3--server3</h1>
[[email protected] kiosk]# curl 172.25.14.100
<h1>172.25.14.2--server2</h1>
[[email protected] kiosk]# arp -d 172.25.14.100
[[email protected] kiosk]# curl 172.25.14.100
<h1> 172.25.14.3--server3</h1>
[[email protected] kiosk]# curl 172.25.14.100
<h1>172.25.14.2--server2</h1>
[[email protected] kiosk]# curl 172.25.14.100
<h1> 172.25.14.3--server3</h1>
[[email protected] kiosk]# arp -d 172.25.14.100
[[email protected] kiosk]# curl 172.25.14.100
<h1>172.25.14.2--server2</h1>
[[email protected] kiosk]# curl 172.25.14.100
<h1> 172.25.14.3--server3</h1>

模拟问题:将server3的httpd服务关闭,模拟server3宕机

可以查看到,在此后访问调度器,server3损坏,不能提供服务,但是调度器依然调用

[[email protected] ~]# /etc/init.d/httpd stop
Stopping httpd:                                            [  OK  ]
[[email protected] kiosk]# curl 172.25.14.100
curl: (7) Failed connect to 172.25.14.100:80; Connection refused
[[email protected] kiosk]# curl 172.25.14.100
<h1>172.25.14.2--server2</h1>
[[email protected] kiosk]# curl 172.25.14.100
curl: (7) Failed connect to 172.25.14.100:80; Connection refused
[[email protected] kiosk]# curl 172.25.14.100
<h1>172.25.14.2--server2</h1>

问题模拟完,将server3的http服务开启,还原实验环境

三.LVS(DR模式)+健康检查(ldirectord)

通过健康检查,可以在真实服务器服务出现问题后,调度器自动将它移出服务器池,直到该服务器修复,自动加入工作。

方法:ldirectord实现自动检测服务器的状态

配置步骤:

1)安装软件ldirectord-3.9.5-3.1.x86_64

[[email protected] ~]# ls    ##下载好的ldirectord软件包
anaconda-ks.cfg  install.log  install.log.syslog  ldirectord-3.9.5-3.1.x86_64.rpm
[[email protected] ~]# vim /etc/yum.repos.d/rhel-source.repo    ##在yum源中添加高可用的安装包
[HighAvailability]
name=HighAvailability
baseurl=http://172.25.14.250/rhel6.5/HighAvailability
enabled=1                    
[[email protected] ~]# yum clean all   ##重新加载yum源
[[email protected] ~]# yum repolist    ##此时可以查看多出56个高可用的安装包
Loaded plugins: product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
HighAvailability                                             | 3.9 kB     00:00     
HighAvailability/primary_db                                  |  43 kB     00:00     
LoadBalancer                                                 | 3.9 kB     00:00     
LoadBalancer/primary_db                                      | 7.0 kB     00:00     
rhel-source                                                  | 3.9 kB     00:00     
rhel-source/primary_db                                       | 3.1 MB     00:00     
repo id               repo name                                               status
HighAvailability      HighAvailability                                           56
LoadBalancer          LoadBalancer                                                4
rhel-source           Red Hat Enterprise Linux 6Server - x86_64 - Source      3,690
[[email protected] ~]# yum install ldirectord-3.9.5-3.1.x86_64.rpm -y   ##安装该数据包,解决依赖性

2)修改ldirectord配置文件

[[email protected] ~]# rpm -ql ldirectord-3.9.5-3.1.x86_64     ##查看ldirectord的所有配置文件
/etc/ha.d
/etc/ha.d/resource.d
/etc/ha.d/resource.d/ldirectord
/etc/init.d/ldirectord
/etc/logrotate.d/ldirectord
/usr/lib/ocf/resource.d/heartbeat/ldirectord
/usr/sbin/ldirectord
/usr/share/doc/ldirectord-3.9.5
/usr/share/doc/ldirectord-3.9.5/COPYING
/usr/share/doc/ldirectord-3.9.5/ldirectord.cf
/usr/share/man/man8/ldirectord.8.gz
[[email protected] ~]# cd /etc/ha.d/
[[email protected] ha.d]# cp /usr/share/doc/ldirectord-3.9.5/ldirectord.cf .   ##将配置文件拷贝到本目录下可以生效
[[email protected] ha.d]# vim ldirectord.cf    ##编辑配置文件
 11 # Global Directives           ##全局设置
 12 checktimeout=3              ##指定real server出错的时间间隔
 13 checkinterval=1             ##指定ldirectord在两次检查之间的间隔
 14 #fallback=127.0.0.1:80   
 15 #fallback6=[::1]:80
 16 autoreload=yes               ##选择yes时,当配置文件发生变化,自动载入配置信息
 17 #logfile="/var/log/ldirectord.log"   
 18 #logfile="local0"
 19 #emailalert="[email protected]"
 20 #emailalertfreq=3600
 21 #emailalertstatus=all
 22 quiescent=no  
 23 #当一个节点在checktimeout设置的时间周期内没有响应它是‘静止’(它的权重是0)的,当你设置了这个选项之后,ldirectord将会从IPVS表中移除真实服务器而不是停止它,从IPVS表移除节点将中断现有的客户连接,并是LVS丢失所有的连接跟踪记录和持续连接模板,如果你不将这个参数设置为no,当某个节点崩溃时,对某些客户端而言,可能会显示为集群都关闭了,因为在这个节点崩溃之前,这些客户端计算机被分配给它了,而连接跟踪记录和程序连接模板仍然保留在Directord上。
 24 # Sample for an http virtual service
 25 virtual=172.25.14.100:80            ##这是虚拟主机的ip与接口号
 26         real=172.25.14.2:80 gate    ##真实主机server2的ip与接口号,同时设定LVS工作模式,gate为DR模式,ipip表示隧道模式,masq表示NAT模式
 27         real=172.25.14.3:80 gate    ##真实主机server3的ip与接口号
 28         fallback=127.0.0.1:80 gate   ##如果失败,就返回自己的主机
 29         service=http                 ##指定服务类型,对HTTP服务做负载均衡
 30         scheduler=rr   ##轮询模式
 31         #persistent=600
 32         #netmask=255.255.255.255
 33         protocol=tcp                 ##指出该服务使用的协议为tcp
 34         checktype=negotiate          ##指定检测类型
 35         checkport=80    ##80端口
 36         request="index.html"   
 37         #receive="Test Page"   ##将测试页定义注释
 38         #virtualhost=www.x.y.z  ##虚拟服务器的名称,本次实验没有用到,注释
39行之后都是注释,不用更改

3)开启ldirectord服务

[[email protected] ha.d]# /etc/init.d/ldirectord start
Starting ldirectord... success

4)开启调度器的httpd服务

[[email protected] ha.d]# yum install httpd -y
[[email protected] ha.d]# cd /var/www/html/
[[email protected] html]# ls
[[email protected] html]# vim index.html   ##配置本机的http发布文件
网站维护中
[[email protected] html]# vim /etc/httpd/conf/httpd.conf 
[[email protected] html]# /etc/init.d/httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.14.1 for ServerName
                                                           [  OK  ]

 

测试:

将server3的httpd服务关闭,此时调度器自动将server3移出服务器池

LVS—DR模式+keepalived实现负载均衡集群

将server2的httpd服务也关闭,此时服务器自己担任被访问的服务器

LVS—DR模式+keepalived实现负载均衡集群

将server2的httpd服务恢复,调度器自动将server2投入使用

LVS—DR模式+keepalived实现负载均衡集群

 

四.LVS(DR模式)+高可用(keepalived)

1.简介

  • 负载均衡服务器的高可用性

       为了屏蔽负载均衡服务器的失效,需要建立一个备份机。主服务器和备份机上 都运行High Availability监控程序,通过传送诸如“I am alive”这样的信息来监控对方的运行状况。当备份机不能在一定的时间内收到这样的信息时,它就接管主服务器的服务IP并继续提供服务;当备份管理器又 从主管理器收到“I am alive”这样的信息时,它就释放服务IP地址,这样的主管理器就开始再次进行集群管理的工作了。为在主服务器失效的情况下系统能正常工作,我们在主、备份机之间实现负载集群系统配置信息的同步与备份,保持二者系统的基本一致。

  • keepalived的作用

        Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统 中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人 工干涉,需要人工做的只是修复故障的服务器。

        主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现。

  • keepalived的高可用性通过VRRP协议实现

        VRRP(Virtual Router Redundancy Protocol,虚拟路由器冗余协议)是一种容错协议,保证当主机的下一条路由器出现故障时,由另一台路由器来代替出现故障的路由器进行工作,从而保持网络通信的连续性和可靠性。

  • keepalived的负载均衡依赖于IPVS实现

        Ipvs(IP Virtual Server)是整个负载均衡的基础,如果没有这个基础,故障隔离与失败切换就毫无意义了。在大部分linux发行版中,ipvs被默认安装,注意:只有执行ipvsadm以后,才会在内核加载ip_vs模块;不能以查进程的方式判断ipvs是否运行。
        在采用keepalived的方案里,只要ipvsadm被正确的安装,简单的配置唯一的文件keepalived就行了。

 

2.配置步骤

环境:

server1 172.25.14.1 负载调度器LB
server2 172.25.14.2 Realserver
server3 172.25.14.3 Realserver
server4 172.25.14.4 负载调度器备用

 

  • rhel6.5
  • iptables and selinux  disabled
  • VIP:172.25.14.100  
  • 网络yum源http://172.25.14.250/rhel6.5
  • server2,server3开启httpd服务
  • LVS—DR模式配置完成,不附带健康检查,具体操作参照上文

 

1)server1配置yum源

[[email protected] ~]# vim /etc/yum.repos.d/rhel-source.repo 
  1 [rhel-source]
  2 name=Red Hat Enterprise Linux $releasever - $basearch - Source
  3 baseurl=http://172.25.14.250/rhel6.5
  4 enabled=1
  5 gpgcheck=1
  6 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
  7 
  8 [LoadBalancer]
  9 name=LoadBalancer
 10 baseurl=http://172.25.14.250/rhel6.5/LoadBalancer
 11 enabled=1
 12 gpgcheck=0
 13 
 14 [HighAvailability]              ##在yum源中添加高可用相关软件包
 15 name=HighAvailability
 16 baseurl=http://172.25.14.250/rhel6.5/HighAvailability
 17 enabled=1
                                                    
[[email protected] ~]# yum clean all
[[email protected] ~]# yum repolist

2)server1下载keepalived并且进行编译安装

[[email protected] ~]# ls
anaconda-ks.cfg  install.log.syslog
install.log      keepalived-2.0.6.tar.gz
[[email protected] ~]# tar zxf keepalived-2.0.6.tar.gz     ##解压下载好的压缩包
[[email protected] ~]# yum install openssl-devel gcc -y   ##编译安装keepalived还需要解决依赖性
[[email protected] ~]# ls
install.log.syslog  keepalived-2.0.6  keepalived-2.0.6.tar.gz
[[email protected] ~]# cd keepalived-2.0.6    ##进入解压目录
[[email protected] keepalived-2.0.6]# ./configure --prefix=/usr/local/keepalived --with-init=SYSV       ##添加系统启动方式模块
[[email protected] keepalived-2.0.6]# make && make install

3)server1编辑keepalived配置文件

[[email protected] keepalived-2.0.6]# cd /usr/local/keepalived/etc/rc.d/init.d/
[[email protected] init.d]# ll
total 4
-rw-r--r-- 1 root root 1308 Feb 26 09:42 keepalived
[[email protected] local]# ip addr del 172.25.14.100/24 dev eth0
[[email protected] init.d]# chmod +x keepalived      ##启动脚本没有执行权限,添加权限
[[email protected] init.d]# ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived  /etc/init.d/        ##给系统启动脚本制作软链接
[[email protected] init.d]# ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/    ##给配置文件做软链接,便于管理
[[email protected] init.d]# ln -s /usr/local/keepalived/etc/keepalived/ /etc/   ##给配置文件做软链接
[[email protected] init.d]# ln -s /usr/local/keepalived/sbin/keepalived /sbin   ##给环境变量做软链接
[[email protected] init.d]# cd /etc/keepalived/
[[email protected] keepalived]# vim keepalived.conf   ##编辑keepalived的主配置文件 
  1 ! Configuration File for keepalived
  2 ##全局配置
  3 global_defs {      ##全局定义块
  4    notification_email {         ##邮件通知
  5      [email protected]
  6    }
  7    notification_email_from [email protected]    ##自定义的邮件发送地址
  8    smtp_server 127.0.0.1       ##设置接收邮件server的端口
  9    smtp_connect_timeout 30     ##设置超时时间
 10    router_id LVS_DEVEL           ##load blanacer的标识,用于警报
 11    vrrp_skip_check_adv_addr
 12    #vrrp_strict
 13    vrrp_garp_interval 0
 14    vrrp_gna_interval 0
 15 }
 16 
 17 vrrp_instance VI_1 {
 18     state MASTER     ##设置这台主机是主负载调度器,备机改为 BACKUP,此状态是由 priority 的值来决定的,当前priority 的值小于备机的值,那么将会失去 MASTER 状态
 19     interface eth0   ##高可用监测接口是eth0
 20     virtual_router_id 51    ##主备必须相同,去值范围1~255
 21     priority 100       ##优先级,数字越大,优先级越高,取值范围1~255
 22     advert_int 1       ##主备之间通告间隔时间
 23     authentication {   
 24         auth_type PASS      ##验证类型,有PASS与AH
 25         auth_pass 1111  
 26     }  
 27     virtual_ipaddress {              ##设置VIP,在主开启时,VIP在主上,若主服务关闭,那么VIP漂移到备上
 28         172.25.14.100
 29     }
 30 }
 31 
 32 virtual_server 172.25.14.100 80 {   ##定义虚拟服务器,该服务器用于接收外来的数据包
 33     delay_loop 3
 34     lb_algo rr         ##lvs调度算法,轮询模式
 35     lb_kind DR         ##lvs采用的是DR模式
 36     #persistence_timeout 50  ##需要注释
 37     protocol TCP       ##指定转发协议类型
 38 
 39     real_server 172.25.14.2 80 {   ##负载均衡的真实服务器
 40         TCP_CHECK {                ##健康检查的方式是TCP
 41         weight 1                ##默认为1,0为失效
 42             connect_timeout 3     ##检查的超时时间为3秒,3秒无回应,就移出服务器池
 43             retry 3            ##重试次数
 44             delay_before_retry 3    ##重试间隔
 45         }
 46     }
 47     real_server 172.25.14.3 80 {   ##负载均衡的真实服务器
 48         weight 1
 49         TCP_CHECK {
 50             connect_timeout 3
 51             retry 3
 52             delay_before_retry 3
 53         }
 54     }
 55 }


4)将server1编辑好的keepalived文件拷贝到备用负载均衡器server4,并且server4同样做软链接,便于管理

[[email protected] local]# scp -r keepalived [email protected]:/usr/local
[[email protected] keepalived]# ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[[email protected] keepalived]# ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[[email protected] keepalived]# ln -s /usr/local/keepalived/etc/keepalived/ /etc/
[[email protected] keepalived]# ln -s /usr/local/keepalived/sbin/keepalived /sbin

5)更改server4的keepalived配置文件,只修改两个地方

 1 ! Configuration File for keepalived
  2 
  3 global_defs {
  4    notification_email {
  5      [email protected]
  6    }
  7    notification_email_from [email protected]
  8    smtp_server 127.0.0.1
  9    smtp_connect_timeout 30
 10    router_id LVS_DEVEL
 11    vrrp_skip_check_adv_addr
 12    #vrrp_strict
 13    vrrp_garp_interval 0
 14    vrrp_gna_interval 0
 15 }
 16 
 17 vrrp_instance VI_1 {
 18     state BACKUP           ##这是备用的负载调度器,所以状态设置为BACKUP
 19     interface eth0
 20     virtual_router_id 51
 21     priority 50            ##设置优先级为50,一般与其他服务器相差50,若有其他的备用,那么在主调度器无响应后,优先级高的备用备推举为临时的主调度器
 22     advert_int 1
 23     authentication {
 24         auth_type PASS
 25         auth_pass 1111
 26     }
 27     virtual_ipaddress {
 28         172.25.14.100
 29     }
 30 }
 31 
 32 virtual_server 172.25.14.100 80 {
 33     delay_loop 3
 34     lb_algo rr
 35     lb_kind DR
 36     #persistence_timeout 50
 37     protocol TCP
 38 
 39     real_server 172.25.14.2 80 {
 40         TCP_CHECK {
 41         weight 1
 42             connect_timeout 3
 43             retry 3
 44             delay_before_retry 3
 45         }
 46     }
 47     real_server 172.25.14.3 80 {
 48         TCP_CHECK {
 49         weight 1
 50             connect_timeout 3
 51             retry 3
 52             delay_before_retry 3
 53         }
 54     }
 55 }

6)开启keepalived服务

[[email protected] local]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]
[[email protected] keepalived]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]

 

测试1:调度器server1正常工作,自带健康检查

此时VIP在主负载均衡器(server1)上

LVS—DR模式+keepalived实现负载均衡集群

另开主机进行访问,真实服务器server2与server3轮询工作

LVS—DR模式+keepalived实现负载均衡集群

模拟问题,将真实服务器server3的http的服务关闭,调度器自带健康检查功能

LVS—DR模式+keepalived实现负载均衡集群

将server3的http服务恢复,server3重新进入工作状态

LVS—DR模式+keepalived实现负载均衡集群

 

测试2:调度器server1停止服务,备用调度器server4自动接管工作

模拟问题,将server1的keepalived服务关闭,VIP自动漂移至备用的server4上

LVS—DR模式+keepalived实现负载均衡集群

另开主机进行访问,负载均衡功能正常,server2与server3轮询工作

LVS—DR模式+keepalived实现负载均衡集群

模拟问题,将server3的http服务关闭,健康检查功能正常

LVS—DR模式+keepalived实现负载均衡集群