keepalived

1 Operating Environment

  • Linux Version
cat /etc/issue
cat /etc/redhat-release
Ubuntu 16.04.4 LTS \n \l
  • Configuration Info
NodeHOST-IP
VIP192.168.0.10
MASTER192.168.0.20
BACKUP192.168.0.30

2 Install

3 Setting

3.1 Configuration Precautions

3.1.1 Common Command

ActionCommandRemarks
Self-startingsystemctl enable keepalived
startsystemctl start keepalived
stopsystemctl stop keepalived
restartsystemctl restart keepalived
check processps -ef | grep keepalived
check ipifconfig | grep eth0
check ipip -a

3.1.2 Configuration Precautions

  • keepavlied.conf
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.ori
vim /etc/keepalived/keepalived.conf
  • Configuration Precautions
ItemPrecautionsRemarks
virtual_router_id必须一致0- 255之间
router_id必须不同
state根据策略配置MASTER or BACKUP
prioritydiff0- 255之间,相差50以上

3.2 MASTER Node Setting

! Configuration File for keepalived

vrrp_script chk_fs {
    script "/etc/keepalived/chk_fs.sh"
    interval 10
    weight 2
}

global_defs {
   router_id xxx_master
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 188
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    
    track_script {
    	# run the script
        chk_fs
    }
	
    virtual_ipaddress {
       192.168.0.20/24 dev eth0 label eth0:1
    }
}

3.3 BACKUP Node Setting

! Configuration File for keepalived

vrrp_script chk_fs {
        script "/etc/keepalived/chk_fs.sh"
        interval 10
        weight 2
}

global_defs {
   router_id xxx_backup
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 188
    priority 20
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    
    track_script {
    	# run the script
        chk_fs
    }
    
    virtual_ipaddress {
       192.168.0.30/24 dev eth0 label eth0:1
    }
}

4 keepalived-script

  • killall
# ubuntu
apt-get install psmisc
# redhat or CentOS
yum install psmisc -y

4.1 Script

  • Context
cd /etc/keepalived
vi chk_fs.sh
  • keepalived-script
#! /bin/bash
# num=`ps -ef |grep freeswitch |grep -v grep | wc -l`
num =`netstat -tnlp |grep ":5066"|wc -l`
if [ $num -eq 0 ]; 
then
	# /usr/local/freeswitch/bin/freeswitch -nc
	# /app/docker.sh
	sleep 3
	if [ `netstat -tnlp |grep ":5066"|wc -l` -eq 0 ]; 
	then
	     killall keepalived
	fi
fi

4.2 Performance Testing

  • script
#!/bin/sh
echo "shell:hello world!"
PATH=/bin:/sbin:/usr/bin:/usr/sbin
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ]
then
     echo 'nginx server is died'
     killall keepalived
fi

echo "end: hello world!"
  • config
! Configuration File for keepalived

vrrp_script check_nginx_alive {
        script "/usr/bin/check_nginx_alive.sh"
        interval 3
        weight -10
}

global_defs {
   notification_email {
        wangjq@cmrh.com
   }
   notification_email_from wangjq@cmrh.com
   smtp_server mail.cmrh.com
   smtp_connect_timeout 30
   router_id CMRH_WEB
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 171
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass cmrh_web
    }

   track_script {
        ## run the script
        check_nginx_alive
    }

    virtual_ipaddress {
         100.69.216.171
    }
}

virtual_server 100.69.216.171 80 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 100.69.216.168 80 {
        weight 1
        TCP_CHECK{
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

5 keepalived HA

vim monitorvip.sh
bash monitorvip.sh
#! /bin/bash
var=` /sbin/ifconfig -a | grep "inet" | awk '{print $2}' | awk -F ':' '{print $2}' `;
vip="vip";
result=$(echo $var | grep "${vip}");
if [ "$result" != "" ]
then
    echo "$s1 include $s2"
else
    echo "$1 not include $s2"
fi

5.1 CTI

  • CTI
#!/bin/sh
while true
do
time=$(date "+%Y-%m-%d %H:%M:%S")
var=` /sbin/ifconfig -a | grep "inet" | awk '{print $2}' | awk -F ':' '{print $2}' `;
vip="100.70.88.148";
result=$(echo ${var} | grep "${vip}");
if [ "$result" != "" ]
then

	echo  "[""${time}""]": ${var} >>/var/rainny/cti/rainnyinfo/ctiwatchdog.log
	echo  "[""${time}""]": ${vip} >>/var/rainny/cti/rainnyinfo/ctiwatchdog.log
	echo  "[""${time}""]": result value: ${result} >>/var/rainny/cti/rainnyinfo/ctiwatchdog.log
	
	echo "[""${time}""]": Virtual IP is on this host. Running CTI Service!  >>/var/rainny/cti/rainnyinfo/ctiwatchdog.log
	sn=`ps -ef | grep rainnytech.exe | grep -v grep`
	if [ "${sn}" = "" ]
	then
		echo "[""${time}""]": Virtual IP is on this host. CTI Service is not running! >>/var/rainny/cti/rainnyinfo/ctiwatchdog.log
		cd /var/rainny/cti/rainnyinfo/
		echo "[""${time}""]": Restart CTI Service! >>/var/rainny/cti/rainnyinfo/ctiwatchdog.log
		nohup mono rainnytech.exe &
		echo "[""${time}""]": Restart CTI Service has been completed! >>/var/rainny/cti/rainnyinfo/ctiwatchdog.log
		echo "[""${time}""]": ctiserver is restart >>/var/rainny/cti/rainnyinfo/ctiwatchdog.log

	else
		echo "[""${time}""]":  Virtual IP is on this host. ctiserver is running >>/var/rainny/cti/rainnyinfo/ctiwatchdog.log

	fi
else
	echo  "[""${time}""]": ${var} >>/var/rainny/cti/rainnyinfo/ctiwatchdog.log
	echo  "[""${time}""]": ${vip} >>/var/rainny/cti/rainnyinfo/ctiwatchdog.log
	echo  "[""${time}""]": result value: ${result} >>/var/rainny/cti/rainnyinfo/ctiwatchdog.log
	
	echo "[""${time}""]": Virtual IP is not on this host. stop CTI Service!  >>/var/rainny/cti/rainnyinfo/ctiwatchdog.log
	pid=`ps -ef | grep "rainnytech.exe" | grep -v grep | awk '{print $2}'`
	if [ "${pid}" = "" ]
	then
		echo "[""${time}""]": ctiserver is not running, not need to restart! >>/var/rainny/cti/rainnyinfo/ctiwatchdog.log
	else
		echo "[""${time}""]": ctiserver is running, stop ctiserver! >>/var/rainny/cti/rainnyinfo/ctiwatchdog.log
		kill -9 "${pid}"
		echo "[""${time}""]": Stoping ctiserver has been completed! >>/var/rainny/cti/rainnyinfo/ctiwatchdog.log
	fi
fi
sleep 7
done

5.2 FS

#!/bin/sh
while true
do
time=$(date "+%Y-%m-%d %H:%M:%S")
var=` /sbin/ifconfig -a | grep "inet" | awk '{print $2}' | awk -F ':' '{print $2}' `;
vip="100.70.88.147";
result=$(echo ${var} | grep "${vip}");
if [ "$result" != "" ]
then
        echo  "[""${time}""]": ${var}
		echo  "[""${time}""]": ${vip}
		
		echo  "[""${time}""]": ${var} >>/var/rainny/cti/log/freeswitch.log
		echo  "[""${time}""]": ${vip} >>/var/rainny/cti/log/freeswitch.log
		echo  "[""${time}""]": result value: ${result} >>/var/rainny/cti/log/freeswitch.log
		
		echo  "[""${time}""]": vip is on this host!
        echo  "[""${time}""]": VIP is on this host, restart FreeSWITCH Service! >>/var/rainny/cti/log/freeswitch.log
        sn=`ps -ef | grep freeswitch | grep -v grep`
        if [ "${sn}" = "" ]
        then
			echo "[""${time}""]": VIP is on this host, FreeSWITCH is not running >>/var/rainny/cti/log/freeswitch.log
			cd /usr/local/freeswitch/bin
			echo "[""${time}""]": Restart FreeSWITCH >>/var/rainny/cti/log/freeswitch.log
			./freeswitch -nonat -nc
			echo "[""${time}""]": restart completed >>/var/rainny/cti/log/freeswitch.log
			echo "[""${time}""]": freeswitch is restart >>/var/rainny/cti/log/freeswitch.log
        else
			echo "[""${time}""]": freeswitch is running >>/var/rainny/cti/log/freeswitch.log
        fi
else
        echo  "[""${time}""]": ${var}
		echo  "[""${time}""]": ${vip}
		
		echo  "[""${time}""]": ${var} >>/var/rainny/cti/log/freeswitch.log
		echo  "[""${time}""]": ${vip} >>/var/rainny/cti/log/freeswitch.log
		echo  "[""${time}""]": result value: ${result} >>/var/rainny/cti/log/freeswitch.log
        
		echo "[""${time}""]": !
		echo "[""${time}""]": vip is not on this host!
        echo "[""${time}""]": VIP is not on this host, stop FreeSWITCH Service! >>/var/rainny/cti/log/freeswitch.log
        pid=`ps -ef | grep "freeswitch" | grep -v grep | awk '{print $2}'`
        if [ "${pid}" = "" ]
        then
			echo "[""${time}""]": FreeSWITCH Service is Not Running >>/var/rainny/cti/log/freeswitch.log
        else
			echo "[""${time}""]": Shutdown FreeSWITCH >>/var/rainny/cti/log/freeswitch.log
			kill -9 "${pid}"
			echo "[""${time}""]": FreeSWITCH Service has been closed >>/var/rainny/cti/log/freeswitch.log
        fi
fi
sleep 7
done

Reference

https://www.cnblogs.com/sssblog/p/10273148.html

https://www.cnblogs.com/wangkongming/p/4221503.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值