Keepalived实现双击热备的功能,最少两个服务器,功能实现方式是虚拟出一个IP地址,并将服务器设置Master和Slave,基于优先级控制,当主服务器Down机后备机会立即接替,当Master恢复后又重取回主服务器运行权利。


1、官网下载地址:

http://www.keepalived.org/download.html   #截止目前最新版本为1.2.16


环境:主节点192.168.10.206  node6.a.com

    从节点192.168.10.204  node4.a.com

    虚拟IP:192.168.10.16

2、解压安装包:

[root@node6 ~]# tar xvf keepalived-1.2.16.tar.gz

3、进入安装目录:

[root@node6 ~]# cd keepalived-1.2.16

4、安装

解决依赖:

[root@node6 keepalived-1.2.16]# yum install ipvsadm

安装keepalived:

[root@node6 keepalived-1.2.16]# ./configure

[root@node6 keepalived-1.2.16]# make && make install 

5、复制脚本和文件:

[root@node6 keepalived-1.2.16]# cp  /usr/local/etc/rc.d/init.d/keepalived  /etc/rc.d/init.d/

[root@node6 keepalived-1.2.16]# cp /usr/local/etc/sysconfig/keepalived  /etc/sysconfig/

[root@node6 keepalived-1.2.16]# mkdir /etc/keepalived

[root@node6 keepalived-1.2.16]# cp /usr/local/etc/keepalived/keepalived.conf  /etc/keepalived/

[root@node6 keepalived-1.2.16]# cp /usr/local/sbin/keepalived  /usr/sbin/


6、主节点配置:

global_defs {

   notification_email {

     root@localhost  #邮件通知

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   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 {

        192.168.10.16

    }

}


7、从节点配置:

global_defs {

   notification_email {

     root@localhost

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1

   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 {

        192.168.10.16

    }

}


8、启动服务:

[root@node6 keepalived-1.2.16]# /etc/init.d/keepalived   restart

Stopping keepalived:                                       [FAILED]

Starting keepalived:                                       [  OK  ]

9、验证:

主节点查看IP:

wKioL1VHg3yzJ3mbAAIW7p1j_dk581.jpg

从节点查看IP:

wKiom1VHgkSjAeTVAAFfTx0EQw4263.jpg

可以看到虚拟IP192.168.10.16在主节点的网卡上。

10.将主节点Keepalived关闭,验证IP是否会切换到从节点,并在从节点查看日志是否有信息输出:

主节点关闭keepalived:

wKiom1VHg3TR_zg5AAByxUCLUKU826.jpg

从节点日志:

wKioL1VHhL-wqcqbAAF5YwnG0ys349.jpg

从节点查看IP:

wKiom1VHg7uSruLsAAIFYv6MWWI433.jpg

11、主节点重新启动keepalive,查看IP是否会重新绑定在主节点网卡,并查看从接地那日志是否有信息输出:

主节点启动服务:

wKiom1VHhBuQprBhAABNS_wlwRM591.jpg

从节点查看日志:

wKiom1VHhEOQui3cAAFB95a-SxM227.jpg主节点查看IP信息:

wKioL1VHhfHgBiWgAAFAHv3VIfg558.jpg

由此可见IP地址又重新绑定在了主节点网卡了,即达到了双击热备的目的。

下次基于此环境安装haproxy,以实现高可用加负载均衡的效果。