下载keepalived

http://www.keepalived.org/software/keepalived-1.2.7.tar.gz

解压以及编译

tar xzf keepalived-1.2.7.tar.gz

./configure --prefix=/opt/keepalived make && make install

配置keepalived

创建keepalived配置文件keepalived.conf(只有虚拟ip功能,其他功能,例如监控服务、发送邮件等功能由其他脚本完成)

PS:网卡必须是内网网卡

主机配置

!  主机配置

vrrp_instance VI_1 {
        state MASTER                 /* 设置为主 */
        interface eth0               /* 监控网卡 */
        virtual_router_id 51         /* 这个两台服务器必须一样 */
        mcast_src_ip 192.168.0.100    /* 多播的源IP,设置为本机IP,与VIP同一网卡*/
        priority 100                 /* 权重值 MASTRE 一定要高于 BAUCKUP */
        advert_int 1                 /* VRRP Multicast广播周期秒数*/
        smtp_alert

        authentication {
                auth_type   PASS     /* 加密 */
                auth_pass 123456     /* 加密的密码,两台服务器一定要一样,不然会出错 */
        }

        virtual_ipaddress {
                192.168.0.119
        }
}

备机配置

! 备机配置

vrrp_instance VI_1 {
        state BACKUP                 /* 设置为备 */
        interface eth0               /* 监控网卡 */
        virtual_router_id 51         /* 这个两台服务器必须一样 */
        mcast_src_ip 192.168.0.101    /* 多播的源IP,设置为本机IP,与VIP同一网卡*/
        priority 50                /* 权重值 BACKUP 一定要低于 MASTER */
        advert_int 1                 /* VRRP Multicast广播周期秒数*/
        smtp_alert

        authentication {
                auth_type   PASS     /* 加密 */
                auth_pass 123456     /* 加密的密码,两台服务器一定要一样,不然会出错 */
        }

        virtual_ipaddress {
               192.168.0.119
        }
}

启动keepalived

将keepalived设置成为服务式启动及停止

#!/bin/bash

MY_ROOT=$(cd "$(dirname "$0")"; pwd)
ln -s $MY_ROOT/sbin/keepalived /usr/sbin/keepalived
ln -s $MY_ROOT/etc/sysconfig/keepalived /etc/sysconfig/keepalived
ln -s $MY_ROOT/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
mkdir /etc/keepalived
ln -s $MY_ROOT/keepalived.conf /etc/keepalived/keepalived.conf

启动keepalived

/etc/init.d/keepalived start

可以tail系统日志/var/log/messages来查看服务启动状态

Keepalived 的详细介绍:请点这里
Keepalived 的下载地址:请点这里

推荐阅读:

CentOS 6.3下Haproxy+Keepalived+Apache配置笔记 http://www.linuxidc.com/Linux/2013-06/85598.htm

Haproxy + KeepAlived 实现WEB群集 on CentOS 6 http://www.linuxidc.com/Linux/2012-03/55672.htm

Keepalived+Haproxy配置高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/56748.htm

Haproxy+Keepalived构建高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/55880.htm