Keepalived安装与配置

Keepalived工作原理是VRRP,所以只能在同一局域网的主机中使用


Ipvs(IP Virtual Server)是整个负载均衡的基础,如果没有这个基础,故障隔离与失败切换就毫无意义了。

先安装ipvsadm

yum -y install ipvsadm

执行

ipvsadm

只有执行ipvsadm以后,才会在内核加载ip_vs模块。

检查当前加载的内核模块,看是否存在ip_vs模块
lsmod|grep ip_vs

-----------------------------------

wget www.keepalived.org/software/keepalived-1.3.5.tar.gz

tar -zxvf keepalived-1.3.5.tar.gz

cd keepalived-1.3.5

./configure --prefix=/usr/local/keepalived

系统提示警告 *** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.

yum -y install libnl libnl-devel

再次执行

./configure --prefix=/usr/local/keepalived

系统提示错误 configure: error: libnfnetlink headers missing

yum install -y libnfnetlink-devel

再次执行

./configure --prefix=/usr/local/keepalived

Keepalived安装与配置_第1张图片


make && make install

配置成服务

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

mkdir /etc/keepalived

使用软连接或者直接拷贝执行文件
ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
 
或者cp /usr/local/keepalived/sbin/keepalived /usr/sbin/


#加入开机启动项

vi /etc/init.d/keepalived

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="/usr/sbin/keepalived"
prog="keepalived"
config="/etc/keepalived/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 $KEEPALIVED_OPTIONS
    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 $?



chmod a+x /etc/init.d/keepalived

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/


检查配置文件


vi /etc/keepalived/keepalived.conf

chkconfig --add /etc/init.d/keepalived
chkconfig keepalived on


至此可以使用如下快捷方式操作

service keepalived start
service keepalived restart
service keepalived reload
service keepalived stop

      

你可能感兴趣的:(Linux)