欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

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

程序员文章站 2022-03-18 09:57:16
keepalived是什么?keepalived软件起初是专为lvs负载均衡软件设计的,用来管理并监控lvs集群系统中各个服务节点的状态,后来又加入了可以实现高可用的vrrp功能。因此,keepali...

keepalived是什么?

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

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

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

         keepalived官网

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启动脚本内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!