redis+Keepalived主從熱備切換例項
阿新 • • 發佈:2018-11-20
[[email protected]01~/]# 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 1 #延遲10秒待資料被對方同步完成之後再切換主從角色 echo -e "[keepalived master]\n\$(date +%F_%T)\nRun SLAVEOF NO ONE cmd ...\n" >>\${LOGFILE} \$REDISCLI -h \$(hostname) -p \${PORT} SLAVEOF NO ONE &>>\${LOGFILE} EOF cat >/etc/keepalived/scripts/redis_backup.sh<<-EOF #!/bin/bash REMOTE_HOSTNAME="\${1:-cache-redis-02}" PORT="\${2:-6380}" REDISCLI="/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" sleep 5 #延遲15秒待資料被對方同步完成之後再切換主從角色 echo -e "[keepalived backup]\n\$(date +%F_%T)\nRun SLAVEOF cmd ...\n" >>\${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)\n" >> \$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)\n" >> \$LOGFILE EOF chmod +x /etc/keepalived/scripts/*.sh
|
[[email protected]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 1 #延遲10秒待資料被對方同步完成之後再切換主從角色 echo -e "[keepalived master]\n\$(date +%F_%T)\nRun SLAVEOF NO ONE cmd ...\n" >>\${LOGFILE} \$REDISCLI -h \$(hostname) -p \${PORT} SLAVEOF NO ONE &>>\${LOGFILE} EOF cat >/etc/keepalived/scripts/redis_backup.sh<<-EOF #!/bin/bash REMOTE_HOSTNAME="\${1:-cache-redis-01}" PORT="\${2:-6380}" REDISCLI="/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" sleep 5 #延遲15秒待資料被對方同步完成之後再切換主從角色 echo -e "[keepalived backup]\n\$(date +%F_%T)\nRun SLAVEOF cmd ...\n" >>\${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)\n" >> \$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)\n" >> \$LOGFILE EOF chmod +x /etc/keepalived/scripts/*.sh
|