欢迎来到代码驿站!

Shell

当前位置:首页 > 脚本语言 > Shell

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

时间:2023-03-14 14:13:32|栏目:Shell|点击:

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判断文件,目录是否存在或者具有权限的代码

栏    目:Shell

下一篇:没有了

本文标题:如何利用shell开发keepalived启动脚本

本文地址:http://www.codeinn.net/misctech/227408.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有