首页 > 编程语言 > 如何利用shell开发keepalived启动脚本
2020
09-24

如何利用shell开发keepalived启动脚本

keepalived是什么?

Keepalived软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能。因此,Keepalived除了能够管理LVS软件外,还可以作为其他服务(例如:Nginx、Haproxy、MySQL等)的高可用解决方案软件。

  Keepalived软件主要是通过VRRP协议实现高可用功能的。VRRP是Virtual Router RedundancyProtocol(虚拟路由器冗余协议)的缩写,VRRP出现的目的就是为了解决静态路由单点故障问题的,它能够保证当个别节点宕机时,整个网络可以不间断地运行。

  所以,Keepalived 一方面具有配置管理LVS的功能,同时还具有对LVS下面节点进行健康检查的功能,另一方面也可实现系统网络服务的高可用功能。

         keepalived官网http://www.keepalived.org

1.1 keepalived的相关的文件

keepalived的执行命令:/data/apps/keepalived/sbin/keepalived
keepalived的配置文件:/data/apps/keepalived/etc/keepalived/keepalived.conf
keepalived的Pid文件:/data/apps/keepalived/run/keepalived.pid

1.2 keepalived的启停止方式

注意:不要去修改/data/apps/keepalived/etc/sysconfig/keepalived文件

启动方式:keepalived -f 配置文件(绝对路径)  -p PID文件(绝对路径)
关闭方式:kill $( cat PID文件(绝对路径) )

1.3 脚本内容

注意:该脚本只能Linux的超级用户root才能启动,因为脚本中有进行限制

#!/bin/bash
## Define variables
RETVAL=0
Conf="/data/apps/keepalived/etc/keepalived/keepalived.conf"
Exce="/data/apps/keepalived/sbin/keepalived"
Pid="/data/apps/keepalived/run/keepalived.pid"
# Determine the user to execute
if [ "$UID" -ne "$RETVAL" ];then
 echo "Must be root to run scripts"
 exit 1
fi
# Load local functions library
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
# Define functions
start(){
 if [ ! -f "$Pid" ];then
 $Exce -f $Conf -p $Pid >/dev/null 2>&1
 RETVAL=$?
 if [ $RETVAL -eq 0 ];then
  action "Start keepalived service" /bin/true
    else
  action "Start keepalived service" /bin/false
 fi
 else
 echo "keepalived service is running"
 fi
 return $RETVAL
}
stop(){
 if [ -f "$Pid" ];then
   kill $(cat $Pid) >/dev/null 2>&1
 RETVAL=$?
 if [ $RETVAL -eq 0 ];then
  action "Stop keepalived service" /bin/true
    else
  action "Stop keepalived service" /bin/false
 fi
 else
 echo "keepalived service is not running"
 fi
 return $RETVAL
}
status(){
 if [ -f "$Pid" ];then
   echo "keepalived service is running"
 else
 echo "keepalived service is not running"
 fi
 return $RETVAL
}
# case local functions
case "$1" in
 start)
 start
 RETVAL=$?
 ;;
 stop)
 stop
 RETVAL=$?
 ;;
 status)
 status
 RETVAL=$?
 ;;
 restart)
 stop
 sleep 2
 start
 RETVAL=$?
 ;;
 *)
 echo "USAGE:$0{status|start|stop|restart}"
   exit 1 
esac
# Scripts return values
exit $RETVAL

总结

到此这篇关于使用shell开发keepalived启动脚本的文章就介绍到这了,更多相关shell keepalived启动脚本内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧