[root@cache-redis-01~/]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { 11270963592@qq.com } notification_email_from 18978038798@189.cn smtp_server smtp.189.cn smtp_connect_timeout 30 router_id redis_service01 vrrp_skip_check_adv_addr # vrrp_strict # can not use ping vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script chk_redis { script /etc/keepalived/scripts/redis_check.sh ###監控本地redis alive interval 2 ###監控時間間隔(秒) } vrrp_instance redis { state BACKUP interface eth0 virtual_router_id 55 priority 100 nopreempt ###不搶佔MASTER advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_redis ###執行上面定義的chk_redis } virtual_ipaddress { 172.16.1.63/24 dev eth0 label eth0:63 } notify_master /etc/keepalived/scripts/redis_master.sh # 當此節點切換爲master時執行的腳本 notify_backup "/etc/keepalived/scripts/redis_backup.sh cache-redis-02" # 當此節點切換爲
|
[root@cache-redis-02~/]# cat /etc/keepalived/keepalived.conf
|
跟從的腳本徹底同樣!!! |
mkdir -p /etc/keepalived/scripts
cat >/etc/keepalived/scripts/redis_master.sh<<-EOF #!/bin/bash PORT="\${1:-6380}" REDISCLI="/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" sleep 10 #延遲10秒待數據被對方同步完成以後再切換主從角色 echo -e "[keepalived master]\t\$(date +%F_%T)\nRun SLAVEOF NO ONE cmd ..." >>\${LOGFILE} \$REDISCLI -h \$(hostname) -p \${PORT} SLAVEOF NO ONE &>>\${LOGFILE} EOF cat >/etc/keepalived/scripts/redis_backup.sh<<-EOF #!/bin/bash REMOTE_HOSTNAME="\$1" PORT="\${2:-6380}" REDISCLI="/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" sleep 15 #延遲15秒待數據被對方同步完成以後再切換主從角色 echo -e "[keepalived backup]\t\$(date +%F_%T)\nRun SLAVEOF \${REMOTE_HOSTNAME} \${PORT} ..." >>\${LOGFILE} \$REDISCLI -h \$(hostname) -p \${PORT} SLAVEOF \${REMOTE_HOSTNAME} \${PORT} &>>\${LOGFILE} EOF cat >/etc/keepalived/scripts/redis_check.sh<<-EOF #!/bin/bash PORT="\${1:-6380}" LOGFILE=/var/log/keepalived-redis-state.log ALIVE=\$(/bin/redis-cli -h \$(hostname) -p \${PORT} PING) if [ "\$ALIVE" == "PONG" ]; then exit 0 else echo -e "[redis_check fault]\t\$(date +%F_%T)" >> \$LOGFILE exit 1 fi EOF cat >/etc/keepalived/scripts/redis_fault.sh<<-EOF #!/bin/bash systemctl stop keepalived.service EOF cat >/etc/keepalived/scripts/redis_stop.sh<<-EOF #!/bin/bash LOGFILE=/var/log/keepalived-redis-state.log echo -e "[keepalived stop]\t\$(date +%F_%T)" >> \$LOGFILE EOF chmod +x /etc/keepalived/scripts/*.sh
|