Keepalived 安装与配置

2019年4月10日 0 条评论 94 次阅读 0 人点赞
首先安装依赖包
yum install -y libnfnetlink-devel zlib zlib-devel gcc gcc-c++ openssl openssl-devel openssh libnl* 
下载并解压安装Keepalived
wget https://www.keepalived.org/software/keepalived-2.0.15.tar.gz && tar -zxvf keepalived-2.0.15.tar.gz && cd keepalived-2.0.15 && ././configure --prefix=/usr/local/keepalived && make && make install 

配置

1、 keepalived安装完成后默认不会注册为系统服务,所以需要手工添加系统服务脚本。在/etc/init.d目录下新建keepalived文件,并更改权限其即可。

cp /root/keepalived-2.0.15/keepalived/etc/init.d/keepalived /etc/init.d/ && chmod 755 /etc/init.d/keepalived && chkconfig --add keepalived && chkconfig keepalived on

cp /usr/local/keepalived/sbin/keepalived /usr/sbin

2、调整启动配置文件如下:

#!/bin/bash
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /etc/keepalived/keepalived.conf
# chkconfig: - 21 79
# description: Start and stop Keepalived

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

# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /usr/local/keepalived/etc/sysconfig/keepalived

keepalived=/usr/local/keepalived/sbin/keepalived
keepalived_config=/usr/local/keepalived/etc/keepalived/keepalived.conf
keepalived_pid=/usr/local/keepalived/run/keepalived.pid

RETVAL=0

prog="keepalived"

start() {
    echo -n $"Starting $prog: "
    daemon $keepalived -f ${keepalived_config} ${KEEPALIVED_OPTIONS}
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}

stop() {
    echo -n $"Stopping $prog: "
    killproc keepalived
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
}

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

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    reload)
        reload
        ;;
    restart)
        stop
        start
        ;;
    condrestart)
        if [ -f /var/lock/subsys/$prog ]; then
            stop
            start
        fi
        ;;
    status)
        status keepalived
        RETVAL=$?
        ;;
    *)
        echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
        RETVAL=1
esac

exit $RETVAL

3、自定义日志输出

由于在默认状态下keepalived的日志会写入到/var/log/message中,我们需要将此剥离出来;在centos 6下可以:

(1)首先修改/etc/sysconfig/keepalived文件,注释掉如下,添加如下:

#KEEPALIVED_OPTIONS="-D"
KEEPALIVED_OPTIONS="-D -d -S 0" 

(2)其次修改 /etc/rsyslog.conf 文件,添加如下:

local0.* /var/log/keepalived.log

在centos 7 下,还需要修改/lib/systemd/system/keepalived.service 文件:

centos 7使用。因为centos 7使用systemctl,通过systemctl调用service,所以需要修改/lib/systemd/system/keepalived.service文件。

将里面的:

EnvironmentFile=-/usr/local/keepalived/etc/sysconfig/keepalived
ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS

修改成:

EnvironmentFile=/etc/sysconfig/keepalived
ExecStart=/sbin/keepalived $KEEPALIVED_OPTIONS

然后重新加载service,使其生效:

systemctl daemon-reload

4、 使用配置文件简介

vim  keepalived.conf

global_defs {
   router_id lb01
  }

vrrp_instance VI_1 {       定义的VRRP热备实例
    state MASTER           热备状态,MASTER表示主服务器
    interface eth0         承载VIP地址的物理接口
    virtual_router_id 51   虚拟路由器的id号,每个热备组保持一致
    priority 100           优先级,数值越大优先级越高
    advert_int 1           通告间隔秒数,(心跳频率)  注意通告的组播地址224.0.0.18
    authentication {       认证类型,每个热备组保持一致
        auth_type PASS     认证类型
        auth_pass 1111     认证字串
    }
    virtual_ipaddress {
        192.168.31.2       指定的漂移地址(VIP),可以有多个啊
    }
}

Einic Yeo

Per Aspera Ad Astra

文章评论(0)

*

code