Keepalived高可用介绍与配置详解(3)

主机

#编写监控脚本 [root@linuxmi ~]# mkdir /scripts [root@linuxmi ~]# cd /scripts/ [root@linuxmi scripts]# cat > check_n.sh <<EOF #!/bin/bash nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l) if [ $nginx_status -lt 1 ];then systemctl stop keepalived fi EOF [root@linuxmi scripts]# chmod +x check_n.sh [root@linuxmi scripts]# cat > notify.sh <<EOF #!/bin/bash VIP=$2 sendmail (){ subject="${VIP}'s server keepalived state is translate" content="`date +'%F %T'`: `hostname`'s state change to master" echo $content | mail -s "$subject" 1470044516@qq.com } case "$1" in master) nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l) if [ $nginx_status -lt 1 ];then systemctl start nginx fi sendmail ;; backup) nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l) if [ $nginx_status -gt 0 ];then systemctl stop nginx fi ;; *) echo "Usage:$0 master|backup VIP" ;; esac EOF [root@linuxmi scripts]# chmod +x notify.sh #修改主机配置 [root@linuxmi ~]# vim /etc/keepalived/ #按如下内容添加 global_defs { router_id lb01 } vrrp_script nginx_check { script "/scripts/check_n.sh" interval 1 weight -20 } vrrp_instance VI_1 { state BACKUP interface ens32 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass linuxmi123 } virtual_ipaddress { 192.168.83.250 } notify_master "/scripts/notify.sh master 192.168.83.250" notify_backup "/scripts/notify.sh backup 192.168.83.250" }

从机

[root@linuxmi ~]# mkdir /scripts [root@linuxmi ~]# cd /scripts/ [root@linuxmi scripts]# cat > notify.sh <<EOF #!/bin/bash VIP=$2 sendmail (){ subject="${VIP}'s server keepalived state is translate" content="`date +'%F %T'`: `hostname`'s state change to master" echo $content | mail -s "$subject" 1470044516@qq.com } case "$1" in master) nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l) if [ $nginx_status -lt 1 ];then systemctl start nginx fi sendmail ;; backup) nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l) if [ $nginx_status -gt 0 ];then systemctl stop nginx fi ;; *) echo "Usage:$0 master|backup VIP" ;; esac EOF [root@linuxmi scripts]# chmod +x notify.sh #修改配置文件 [root@linuxmi ~]# vim /etc/keepalived/ #按如下内容添加 vrrp_instance VI_1 { state BACKUP interface ens32 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass linuxmi123 } virtual_ipaddress { 192.168.83.250 } notify_master "/scripts/notify.sh master 192.168.83.250" notify_backup "/scripts/notify.sh backup 192.168.83.250" }

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/aa898f6a50a356c6539dc45ff6d39760.html