探究keepalived的安装

CentOS 6通过yum安装keepalived

/etc/keepalived
/etc/keepalived/keepalived.conf
/etc/rc.d/init.d/keepalived
/etc/sysconfig/keepalived
/usr/bin/genhash  --这个不知道是干嘛用的
/usr/libexec/keepalived --这个文件夹里边啥也没有
/usr/sbin/keepalived
/usr/share/doc/keepalived-1.2.13
/usr/share/doc/keepalived-1.2.13/AUTHOR
/usr/share/doc/keepalived-1.2.13/CONTRIBUTORS
/usr/share/doc/keepalived-1.2.13/COPYING
/usr/share/doc/keepalived-1.2.13/ChangeLog
/usr/share/doc/keepalived-1.2.13/NOTE_vrrp_vmac.txt
/usr/share/doc/keepalived-1.2.13/README
/usr/share/doc/keepalived-1.2.13/TODO
/usr/share/doc/keepalived-1.2.13/VERSION
/usr/share/doc/keepalived-1.2.13/keepalived.conf.SYNOPSIS
/usr/share/doc/keepalived-1.2.13/samples
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.HTTP_GET.port
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.IPv6
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.SMTP_CHECK
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.SSL_GET
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.fwmark
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.inhibit
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.misc_check
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.misc_check_arg
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.quorum
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.sample
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.status_code
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.track_interface
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.virtual_server_group
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.virtualhost
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.vrrp
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.vrrp.localcheck
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.vrrp.lvs_syncd
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.vrrp.routes
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.vrrp.scripts
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.vrrp.static_ipaddress
/usr/share/doc/keepalived-1.2.13/samples/keepalived.conf.vrrp.sync
/usr/share/doc/keepalived-1.2.13/samples/sample.misccheck.smbcheck.sh
/usr/share/man/man1/genhash.1.gz
/usr/share/man/man5/keepalived.conf.5.gz
/usr/share/man/man8/keepalived.8.gz
/usr/share/snmp/mibs/KEEPALIVED-MIB.txt

man目录下是一些参数的说明可以执行man keepalived看到。doc目录下的samples里边有一些配置文件的例子。

主要看这几个

  • /etc/keepalived/keepalived.conf
  • /etc/rc.d/init.d/keepalived
  • /etc/sysconfig/keepalived
  • /usr/sbin/keepalived

我把keepalived进行make install后,得到的文件为

  • /usr/local/keepalived/bin/genhash 不知道这个文件是干嘛用的
  • /usr/local/keepalived/etc/keepalived/keepalived.conf
  • /usr/local/keepalived/etc/keepalived/samples文件夹
  • /usr/local/keepalived/etc/sysconfig/keepalived
  • /usr/local/keepalived/sbin/keepalived
  • /usr/local/keepalived/share/man文件夹
  • /usr/local/keepalived/share/snamp文件夹

主要看这几个

  • /usr/local/keepalived/etc/keepalived/keepalived.conf
  • /usr/local/keepalived/etc/sysconfig/keepalived
  • /usr/local/keepalived/sbin/keepalived

发现和yum安装的比较少了一个/etc/rc.d/init.d/keepalived这个文件是用来加入系统服务用的。

我下载了keepalived之后解压,可以在此目录下找到这个文件

keepalived-1.3.4\keepalived\etc\init.d\keepalived

打开这个文件

#!/bin/sh
#
# 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)
. /etc/sysconfig/keepalived

RETVAL=0

prog="keepalived"

start() {
    echo -n $"Starting $prog: "
    daemon keepalived ${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

看注释

# config: /etc/keepalived/keepalived.conf

所以要把/usr/local/keepalived/etc/keepalived/keepalived.conf复制到/etc/keepalived/目录下。

可以看到他执行了. /etc/rc.d/init.d/functions这个文件,在这个里边有这么句话

# Set up a default search path.
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export PATH

也就是说会从这几个目录下寻找。所以要把/usr/local/keepalived/sbin/keepalived复制到/usr/sbin/目录下

并且keepalived-1.3.4\keepalived\etc\init.d\keepalived文件里边用的daemon函数也是在functions文件中定义的。

再往下看可以看到执行了. /etc/sysconfig/keepalived这个文件,所以要把/usr/local/keepalived/etc/sysconfig/keepalived拷贝到/etc/sysconfig/目录下,并且注释说定义了一个变量在里边,

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

再往下面看

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

start方法里边用到了KEEPALIVED_OPTIONS这个变量

打开/usr/local/keepalived/etc/sysconfig/keepalived

# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp               -P    Only run with VRRP subsystem.
# --check              -C    Only run with Health-checker subsystem.
# --dont-release-vrrp  -V    Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs  -I    Dont remove IPVS topology on daemon stop.
# --dump-conf          -d    Dump the configuration data.
# --log-detail         -D    Detailed log messages.
# --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)
#

KEEPALIVED_OPTIONS="-D"

发现默认情况下,这个变量存的就是-D,他的意思就是打印详细日志信息。注释中还提到了一些其他的选项可以根据自己的需求自定义设置。

总结源码安装

之前先要准备安装环境

yum -y install kernel-devel*
yum -y install openssl-*
yum -y install popt-devel
yum -y install lrzsz
yum -y install openssh-clients
yum -y install libnl libnl-devel popt

这些应该够,如果不够看执行./configure的时候缺啥再安装啥。

将keepalived-1.3.4.tar.gz上传到服务器/usr/local下

cd /usr/local
tar -xvf keepalived-1.3.4.tar.gz
cd keepalived-1.3.4

执行配置命令

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

编译make

安装make install

拷贝文件

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

创建keepalived文件夹

mkdir -p /etc/keepalived

将配置文件拷贝到此目录

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

然后在keepalived的解压目录中找到keepalived-1.3.4\keepalived\etc\init.d\keepalived将其拷贝到/etc/rc.d/init.d/目录下(拷贝到/etc/init.d/目下也是可以的,因为init.d会链接到rc.d/init.d)

添加执行权限

chmod +x keepalived

然后加入系统服务,加入了之后就可以使用service keepalived start这样的命令了

chkconfig --add keepalived #添加时必须保证/etc/rc.d/init.d/keepalived存在

之后查看是否加入了系统服务

chkconfig --list

加入开机启动

chkconfig keepalived on

CentOS7

在CentOS 7中通过yum安装,安装的东西有

/etc/keepalived
/etc/keepalived/keepalived.conf
/etc/sysconfig/keepalived
/usr/bin/genhash  --不知道是干嘛的
/usr/lib/systemd/system/keepalived.service
/usr/libexec/keepalived  --是个文件夹,里边什么都没有
/usr/sbin/keepalived
/usr/share/doc/keepalived-1.3.5
/usr/share/doc/keepalived-1.3.5/AUTHOR
/usr/share/doc/keepalived-1.3.5/CONTRIBUTORS
/usr/share/doc/keepalived-1.3.5/COPYING
/usr/share/doc/keepalived-1.3.5/ChangeLog
/usr/share/doc/keepalived-1.3.5/NOTE_vrrp_vmac.txt
/usr/share/doc/keepalived-1.3.5/README
/usr/share/doc/keepalived-1.3.5/TODO
/usr/share/doc/keepalived-1.3.5/keepalived.conf.SYNOPSIS
/usr/share/doc/keepalived-1.3.5/samples
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.HTTP_GET.port
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.IPv6
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.SMTP_CHECK
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.SSL_GET
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.fwmark
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.inhibit
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.misc_check
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.misc_check_arg
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.quorum
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.sample
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.status_code
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.track_interface
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.virtual_server_group
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.virtualhost
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.vrrp
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.vrrp.localcheck
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.vrrp.lvs_syncd
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.vrrp.routes
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.vrrp.rules
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.vrrp.scripts
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.vrrp.static_ipaddress
/usr/share/doc/keepalived-1.3.5/samples/keepalived.conf.vrrp.sync
/usr/share/doc/keepalived-1.3.5/samples/sample.misccheck.smbcheck.sh
/usr/share/man/man1/genhash.1.gz
/usr/share/man/man5/keepalived.conf.5.gz
/usr/share/man/man8/keepalived.8.gz
/usr/share/snmp/mibs/KEEPALIVED-MIB.txt
/usr/share/snmp/mibs/VRRP-MIB.txt
/usr/share/snmp/mibs/VRRPv3-MIB.txt

主要看这几个

  • /etc/keepalived/keepalived.conf
  • /etc/sysconfig/keepalived
  • /usr/sbin/keepalived
  • /usr/lib/systemd/system/keepalived.service

在回头看CentOS6的yum安装

  • /etc/keepalived/keepalived.conf
  • /etc/sysconfig/keepalived
  • /usr/sbin/keepalived
  • /etc/rc.d/init.d/keepalived

会发现别的都一样就是设置系统服务的文件不一样了,keepalived.service可以在keepalived的解压包的keepalived-1.3.4\keepalived\keepalived.service目录下找到。

打开keepalived.service

[Unit]
Description=LVS and VRRP High Availability Monitor
After=syslog.target network-online.target

[Service]
Type=forking
PIDFile=/var/run/keepalived.pid
KillMode=process
EnvironmentFile=-/etc/sysconfig/keepalived
ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

先主要看

#在程序执行前先执行这个文件
EnvironmentFile=-/etc/sysconfig/keepalived
ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS

总结CentOS7中的源码安装

和CentOS6中的源码安装基本一样,只是加入系统服务的时候有区别。

把keepalived-1.3.4\keepalived\keepalived.service复制到/usr/lib/systemd/system/目录下,这样就算加入系统服务了,就可使用systemctl status keepalived了。

设置开机自启,执行systemctl enable keepalived

你可能感兴趣的:(探究keepalived的安装)