Keepalived系列—服务器安装与配置

时间:2022-03-26 00:51:44

一、安装

1、首先安装依赖包

[root@bogon /]# yum install -y libnl*
[root@bogon /]# yum install -y libnfnetlink-devel

2、下载并解压Keepalived

[root@bogon src]# pwd
/usr/local/src
[root@bogon src]# wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz
[root@bogon src]# tar xvf keepalived-1.3.5.tar.gz

3、编译安装

[root@bogon src]# cd keepalived-1.3.5
[root@bogon keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived
[root@bogon keepalived-1.3.5]# make
[root@bogon keepalived-1.3.5]# make install

二、配置

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

1、去 keepalived 源码目录下拷贝启动脚本到/etc/init.d/下

[root@bogon keepalived-1.3.5]# pwd
/usr/local/src/keepalived-1.3.5
[root@bogon keepalived-1.3.5]# cp /usr/local/src/keepalived-1.3.5/keepalived/etc/init.d/keepalived /etc/init.d/keepalived
[root@bogon keepalived-1.3.5]# ll /etc/init.d/ | grep keepalived
-rwxr-xr-x 1 root root 1308 82 14:15 keepalived

2、修改/etc/init.d/keepalived启动脚本

需要修改的数据有以下几项:

  • # config: /etc/keepalived/keepalived.conf 文件的位置;
  • # Source configuration file (we set KEEPALIVED_OPTIONS there) 文件的位置。. /etc/sysconfig/keepalived

修改为实际安装的文件路径,我的安装路径如下:

  • # config: /usr/local/keepalived/etc/keepalived/keepalived.conf
  • . /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
[root@bogon keepalived-1.3.5]# vim /etc/init.d/keepalived
#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /usr/local/keepalived/run/keepalived.pid
# config: /usr/local/keepalived/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

2、修改其权限并开机启动

  1. 修改权限:chmod 755 /etc/init.d/keepalived
  2. 加为系统服务:chkconfig –add keepalived
  3. 开机启动:chkconfig keepalived on
  4. 查看开机启动的服务:chkconfig –list

3、备注:keepalived服务控制

  • systemctl enable keepalived.service #设置开机自动启动
  • systemctl disable keepalived.service #取消开机自动启动
  • systemctl start keepalived.service #启动服务
  • systemctl restart keepalived.service #重启服务
  • systemctl stop keepalived.service #停止服务
  • systemctl status keepalived.service #查看服务状态

附、安装过程中的问题

1、修改启动脚本 /etc/init.d/keepalived 后,未生效

对于此问题一般的解决方法是执行如下命令,重新加载脚本。

[root@bogon sbin]# systemctl daemon-reload

以上方法大多时候会生效,但也有例外。此时,可以试试如下方法:

删除路径 /usr/lib/systemd/system/ 下相应的启动脚本

[root@bogon /]# chkconfig --del keepalived
[root@bogon /]# cd /usr/lib/systemd/system/
[root@bogon system]# ls | grep keepalived
keepalived.service
[root@bogon system]# rm keepalived.service
rm:是否删除普通文件 "keepalived.service"y

然后重新注册为系统服务:

[root@bogon system]# chkconfig --add keepalived

重新注册后,修改后的配置文件就会生效。

2、a configured resource limit was exceeded.

执行 systemctl start keepalived.service 后出现如下错误:

[root@bogon sbin]# systemctl start keepalived.service
Job for keepalived.service failed because a configured resource limit was exceeded. See "systemctl status keepalived.service" and "journalctl -xe" for details.

这个问题在网上找了半天也没找可以解决的方法,但是把 问题1 解决之后 问题2 就没再出现

3、安装时出现如下问题

Keepalived系列—服务器安装与配置

缺少依赖包,安装一下就好:

[root@bogon /]# yum install -y libnfnetlink-devel