How to use shell to develop keepalived startup script

Time:2020-9-13

What is keepalived?

The keepalived software was originally designed for LVS load balancing software, which was used to manage and monitor the status of each service node in the LVS cluster system. Later, the VRRP function which can realize high availability was added. Therefore, in addition to managing LVS software, keepalived can also be used as a highly available solution software for other services (such as nginx, haproxy, mysql, etc.).

Keepalived software mainly realizes the high availability function through VRRP protocol. VRRP is the abbreviation of Virtual Router Redundancy Protocol (VRRP). The purpose of VRRP is to solve the single point failure problem of static routing. It can ensure that the whole network can run continuously when individual nodes are down.

Therefore, on the one hand, keepalived has the function of configuration management of LVS, at the same time, it also has the function of health check of nodes under LVS, on the other hand, it can also realize the high availability of system network services.

Keepalived official website http://www.keepalived.org

1.1 relevant documents of keepalived

The execution command of keepalived is / data / apps / keepalived / SBIN / keepalived
Keepalived configuration file / data / apps / keepalived / etc / keepalived/ keepalived.conf
The PID file of keepalived / data / apps / keepalived / run/ keepalived.pid

1.2 start stop mode of keepalived

Note: do not modify the / data / apps / keepalived / etc / sysconfig / keepalived file

Startup mode: keepalived – f configuration file (absolute path) – P PID file (absolute path)
Close mode: kill $(cat PID file (absolute path))

1.3 script content

Note: this script can only be started by root of Linux super user, because there are restrictions in the script


#!/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

summary

This article on the use of shell to develop keepalived startup scripts will be introduced here. For more relevant shell kept startup scripts, please search the previous articles of developeppaer or continue to browse the related articles below. I hope you can support developeppaer more in the future!