前言:

    Keepalived是一个基于VRRP协议来实现的WEB 服务高可用方案,可以利用其来避免单点故障。一个WEB服务至少会有2台服务器运行Keepalived,一台为主服务器(MASTER),一台为备份服 务器(BACKUP),但是对外表现为一个虚拟IP,主服务器会发送特定的消息给备份服务器,当备份服务器收不到这个消息的时候,即主服务器宕机的时候, 备份服务器就会接管虚拟IP,继续提供服务,从而保证了高可用性。

        +-------------VIP(192.168.10.150)------------------+
        |                                   |                           |  
        |                                   |                           |  
  server(MASTER) <----keepalived----> server(BACKUP)
   (192.168.10.122)                                      (192.168.10.123)


它可提供vrrp以及health-check功能,可以只用它提供双机浮动的vip(vrrp虚拟路由功能),这样可以简单实现一个双机热备高可用功能。

keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层、第4层和第5层交换。Keepalived的作用是检测web 服务器的状态。 Layer3,4&5工作在IP/TCP协议栈的IP层,TCP层,及应用层,原理分别如下:

  Layer3:Keepalived使用Layer3的方式工作式时,Keepalived会定期向服务器群中的服务器

  发送一个ICMP的数据包(既我们平时用的Ping程序),如果发现某台服务的IP地址没有激活,Keepalived便报告这台服务器失效, 并将它从服务器群中剔除,这种情况的典型例子是某台服务器被非法关机。Layer3的方式是以服务器的IP地址是否有效作为服务器工作正常与否的标准。

  Layer4:如果您理解了Layer3的方式,Layer4就容易了。Layer4主要以TCP端口的状态来决定服务器工作正常与否。如 web server的服务端口一般是80,如果Keepalived检测到80端口没有启动,则Keepalived将把这台服务器从服务器群中剔除。

  Layer5:Layer5就是工作在具体的应用层了,比Layer3,Layer4要复杂一点,在网络上占用的带宽也要大一些。 Keepalived将根据用户的设定检查服务器程序的运行是否正常,如果与用户的设定不相符,则Keepalived将把服务器从服务器群中剔除。

vip即虚拟ip,是附在主机网卡上的,即对主机网卡进行虚拟,此IP仍然是占用了此网段的某个IP。

一,平台环境
Centos5.5(2.6.18-8.el5)
VIP:192.168.10.150
Rs1:192.168.10.122
Rs2:192.168.10.123

二,平台安装
1,keepalived的安装

root@centos:~$ tar xvf keepalived-1.1.15.tar.gz

root@centos:~$ cd keepalived-1.1.15

root@centos:~$ ./configure --prefix=/usr/local/keepalived --with-kernel-dir= /usr/src/kernels/2.6.18-8.el5-i686/

root@centos:~$ make && make install

2,keepalived添加系统管理项

root@centos:~$ cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

root@centos:~$  cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

root@centos:~$  /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

root@centos:~$ chkconfig --level 35 keepalived on

3,keepalived 配置

root@centos:~$ cd /etc/keepalived/

root@centos:~$ vi keepalived.conf

主机配置(MASTER):

! Configuration File for keepalived

global_defs {
        router_id test
           }
        vrrp_instance VI_1 {
                state BACKUP
                interface eth0
                virtual_router_id 51
                priority 100
                advert_int 1
                authentication {
                        auth_type PASS
                        auth_pass 1111
                        }
                virtual_ipaddress {
                       192.168.10.150
                            }
           }

备机配置(BACKUP):

! Configuration File for keepalived

global_defs {
        router_id test
           }
        vrrp_instance VI_1 {
                state BACKUP
                interface eth0
                virtual_router_id 51
                priority 80
                advert_int 1
                authentication {
                        auth_type PASS
                        auth_pass 1111
                        }
                virtual_ipaddress {
                        192.168.10.150
                            }
           }

在两机可得出出下信息:

root@centos:/etc/keepalived$ ip a |grep inet
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
    inet 192.168.10.122/24 brd 192.168.10.255 scope global eth0
    inet 192.168.10.150/32 scope global eth0

root@centos:/etc/keepalived$ ip a |grep inet
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
    inet 192.168.10.123/24 brd 192.168.10.255 scope global eth0
 
三,测试

1.拔掉主服务器的网线 在备机上运行ip addr |grep inet 确认vip是否已经漂移过来

  root@centos:/etc/keepalived$ ip a |grep inet
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
    inet 192.168.10.122/24 brd 192.168.10.255 scope global eth0
    inet 192.168.10.150/32 scope global eth0

2.重新接入网线在主服务器上运行ip addr|grep inet确认vip是否已经从备机抢夺过来

  root@centos:/etc/keepalived$ ip a |grep inet
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
    inet 192.168.10.122/24 brd 192.168.10.255 scope global eth0
    inet 192.168.10.150/32 scope global eth0

 

在两机分别安装apache,测试