在CentOS上可以通过yum直接安装Keepalvied

yum install keepalived


这里我们使用源码安装Keepalvied

wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz

tar zxvf keepalived-1.2.15.tar.gz 

源码安装任何一个软件都要养成查看源码包文档的习惯,比如INSTALL,README,doc等文档,可以获得很多有用的信息


INSTALL文档中有描述

Kernel needing
==============

  Compile a kernel with the following options :

  Kernel/User netlink socket
  Network firewalls (for Kernel 2.2)
  LinuxVirtualServer

  Keepalived support all LVS code : including IPVS code for kernel 2.2
  and kernel 2.4

Libraries dependency
====================

  In order to compile Keepalived needs the following libraries :

  * OpenSSL, <www.openssl.org>
  * popt

Installation
============

  1. uncompress the tarball
  2. cd into the directory
  3. './configure'
  4. 'make'
  5. 'make install'. This will install keepalived on your system,
     binaries and configuration file :
     * keepalived : The keepalived daemon program.
     * genhash : The MD5 url digest generator. You need it to 
                 configure HTTP GET check and SSL GET check in
                 order to compute MD5SUM digest etalon.
     * /etc/keepalived/keepalived.conf
  6. link keepalived.init into your runlevel directory. On redhat systems :
     ln -s /etc/rc.d/init.d/keepalived.init /etc/rc.d/rc3.d/S99keepalived

  By default configure script use /usr/local as base directory. You can change
  this value to your own by passing --prefix value to configure script

  eg: './configure --prefix=/usr/'

Configuration
=============

  Just take a look to the /etc/keepalived/keepalived.conf file installed.
  It will give you all the informations needed. If you want more informations
  considering keepalived, please refer to the keepalived homepage into the
  documentation section.

  http://www.keepalived.org


Have fun with it !





安装Keepalived前需要安装popt和openssl

yum -y install popt openssl openssl-devel  popt-devel

mkdir -p /data/app_platform/keepalived/

cd keepalived-1.2.15

./configure --prefix=/data/app_platform/keepalived/

make

make install

mkdir -p /data/app_platform/keepalived/conf

编辑/data/app_platform/keepalived/conf/keepalived.conf文件


这里需要特别注意的是keepalived默认使用的配置文件是/etc/keepalived/keepalived.conf

如果要使用其他配置文件,在启动keepalived的时候需要通过-f 参数指定

Usage: /data/app_platform/keepalived/sbin/keepalived [OPTION...]
  -f, --use-file=FILE          Use the specified configuration file
  -P, --vrrp                   Only run with VRRP subsystem
  -C, --check                  Only run with Health-checker subsystem
  -l, --log-console            Log messages to local console
  -D, --log-detail             Detailed log messages
  -S, --log-facility=[0-7]     Set syslog facility to LOG_LOCAL[0-7]
  -V, --dont-release-vrrp      Don't remove VRRP VIPs and VROUTEs on daemon stop
  -I, --dont-release-ipvs      Don't remove IPVS topology on daemon stop
  -R, --dont-respawn           Don't respawn child processes
  -n, --dont-fork              Don't fork the daemon process
  -d, --dump-conf              Dump the configuration data
  -p, --pid=FILE               Use specified pidfile for parent process
  -r, --vrrp_pid=FILE          Use specified pidfile for VRRP child process
  -c, --checkers_pid=FILE      Use specified pidfile for checkers child process
  -v, --version                Display the version number
  -h, --help                   Display this help message




! Configuration File for keepalived
 
global_defs {
   notification_email {
     admin@example.com                  设置通知接收邮件地址
   }
   notification_email_from lb1@example.com    设置通知发送邮件地址 
   smtp_server 127.0.0.1                     
   smtp_connect_timeout 30
   router_id LB1_MASTER
}
 
 vrrp_script chk_haproxy {                  检查HAProxy是否存在
        script "killall -0 haproxy"    
        interval 2                     每2秒检查一下HAProxy进程
    weight 2                        
    }
 
 
 
 
vrrp_instance VI_1 {
    state MASTER                    LB1上设置为MASTER,LB2上设置为BACKUP
    interface eth0
    virtual_router_id 51            
    priority 100             MASTER设置为100,BACKUP设置为99,MASTER的值必须大于BACKUP
    advert_int 1             设置MASTER和BACKUP之间同步检查时间间隔
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    192.168.1.1/24  brd 192.168.1.255  dev eth0 label eth0:ha
             设置虚拟IP地址,为了便于查看虚拟IP,可以设置一个
    }
     
    track_script {
        chk_haproxy 
                 }
}

  

    

添加启动脚本

/etc/init.d/keepalived

#!/bin/sh
#
# keepalived   High Availability monitor built upon LVS and VRRP
#
# chkconfig:   - 86 14
# description: Robust keepalive facility to the Linux Virtual Server project \
#              with multilayer TCP/IP stack checks.

### BEGIN INIT INFO
# Provides: keepalived
# Required-Start: $local_fs $network $named $syslog
# Required-Stop: $local_fs $network $named $syslog
# Should-Start: smtpdaemon httpd
# Should-Stop: smtpdaemon httpd
# Default-Start: 
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: High Availability monitor built upon LVS and VRRP
# Description:       Robust keepalive facility to the Linux Virtual Server
#                    project with multilayer TCP/IP stack checks.
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

exec="/data/app_platform/keepalived/sbin/keepalived"
prog="keepalived"
config="/data/app_platform/keepalived/conf/keepalived.conf"

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/keepalived

start() {
    [ -x $exec ] || exit 5
    [ -e $config ] || exit 6
    echo -n $"Starting $prog: "
    daemon $exec -D -f $config
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    echo -n $"Reloading $prog: "
    killproc $prog -1
    retval=$?
    echo
    return $retval
}

force_reload() {
    restart
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status &>/dev/null
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?


注意这里

daemon $exec -D -f $config




参考文档:

http://www.keepalived.org/download.html