Lvs+keepalived 能实现的功能: 利用 lvs 控制器主备模式避免单点故障以及自动删除故障 WEB 服务器结点并当它恢复后再自动添加到群集中。

 

拓扑图:

LVS+keepliaved_第1张图片

 

IP 规划:

 

LVS+keepliaved_第2张图片

 

1 、在 LVS SRV 上安装 lvs 、 keepalived 软件( LVS-MASTER & LVS-BACKUP ) 
lvs install--------------------------------

[root@LVS-MASTER ~]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

[root@LVS-MASTER ~]# ln -s /usr/src/kernels/2.6.18-238.el5-i686/ /usr/src/linux/

[root@LVS-MASTER ~]# tar zxvf ipvsadm-1.24.tar.gz

[root@LVS-MASTER ~]# cd ipvsadm-1.24

[root@LVS-MASTER ipvsadm-1.24]# make && make install

 

Keepalived install------------------------------------

[root@LVS-MASTER ~]# wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz

[root@LVS-MASTER ~]# tar zxvf keepalived-1.1.15.tar.gz

[root@LVS-MASTER ~]# cd keepalived-1.1.15

[root@LVS-MASTER keepalived-1.1.15]# ./configure && make && make install

 

######### 将 keepalived 做成启动服务,方便管理 ##########

[root@LVS-MASTER ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/

[root@LVS-MASTER ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

[root@LVS-MASTER ~]# mkdir /etc/keepalived/

[root@LVS-MASTER ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

[root@LVS-MASTER ~]# cp /usr/local/sbin/keepalived /usr/sbin/

[root@LVS-MASTER ~]# service keepalived start | stop

 

2 、 keepalived 的配置

============================================================================

###################              MASTER             ######################

============================================================================

! Configuration File for keepalived

global_defs {

   notification_email {

        [email protected]

   }

   notification_email_from [email protected]

   smtp_server 127.0.0.1

   router_id LVS_DEVEL

}

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        172.16.201.188

    }

}

virtual_server 172.16.201.188 80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    persistence_timeout 50

    protocol TCP

 

    real_server 172.16.201.211 80 {

        weight 3

        TCP_CHECK {

        connect_timeout 10

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

        }

    }

    real_server 172.16.201.212 80 {

        weight 3

        TCP_CHECK {

        connect_timeout 10

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

        }

    }

}

============================================================================

#########################            BACKUP          ######################

============================================================================

! Configuration File for keepalived

global_defs {

   notification_email {

        [email protected]

   }

   notification_email_from [email protected]

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 99

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        172.16.201.188

    }

}

virtual_server 172.16.201.188 80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

 

    real_server 172.16.201.211 80 {

        weight 3

        TCP_CHECK {

        connect_timeout 10

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

        }

    }

    real_server 172.16.201.212 80 {

        weight 3

        TCP_CHECK {

        connect_timeout 10

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

        }

    }

}

3 、在 realserver SRV 上执行 realserver.sh 脚本,为 lo:0 绑定 VIP 地址 172.16.201.188 、抑制 ARP 广播。

LVS+keepliaved_第3张图片

 

 

4 、分别在 LVS-MASTER 、 LVS-BACKUP SRV 上执行 service keepalived start 启动 keepalived 就可实现负载均衡及高可用集群了;

[root@LVS-MASTER ~]# service keepalived start

LVS+keepliaved_第4张图片

 

日志信息:

 

LVS+keepliaved_第5张图片

 

 

――――――――――――――高可用性测试―――――――――――――――

(为了模拟故障,将 LVS-MASTER 上的 keepalived 服务停掉,然后观察 LVS-BACKUP SRV 上的日志,信息如下  )

LVS+keepliaved_第6张图片

    (从日志中可知,主机出现故障后,备机立刻检测到,此时备机变为 MASTER 角色,并且接管了主机的虚拟 IP资源,最后将虚拟 IP 绑定在 etho 设备上。)

 

将 LVS - MASTER 上的 keepalived 服务开启后 ,LVS-BACKUP 的日志状态。

 

  (从日志可知,备机在检测到主机重新恢复正常后,释放了虚拟 IP 资源重新成为 BACKUP 角色)

 

―――――――――――――故障切换测试――――――――――――――――――――

---- 故障切换是测试当某个节点出现故障后, Keepalived 监制模块是否能及时发现然后屏蔽故障节点,同时将服务器转移到正常节点来执行。

将 REAL-SERVER2 节点服务停掉,假设这个节点出现故障,然后主、备机日志信息如下 …….

 

LVS+keepliaved_第7张图片

 

 ( 从以上可以看出, Keepalived 监控模块检测到 172.16.201.212 这台主机出现故障后,将些节点 2 从集群系统中剔除掉了。 此时访问 http://172.16.201.188 只能看到“ This is realserver1 !!!” 了 )

 

 

重新启动 REAL-SERVER2 节点的服务,日志信息如下:

LVS+keepliaved_第8张图片

 

( Keepalived 监控模块检测到 172.16.201.212 这台主机恢复正常后,又将此节点加入集群系统中,再次访问就可以访问到“ This is realserver2 !!!” 页面了, 嘿嘿 …….. )