1. 程式人生 > >redis+Keepalived主從熱備切換例項

redis+Keepalived主從熱備切換例項

 

 

 

 

 

[[email protected]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" ###監控指令碼 interval 2 ###監控時間 } vrrp_instance redis { state BACKUP
interface eth0 virtual_router_id 55 priority 100 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_redis ###執行上面定義的chk_redis } virtual_ipaddress { 172.16.1.63/24 } notify_master
/etc/keepalived/scripts/redis_master.sh notify_backup /etc/keepalived/scripts/redis_backup.sh notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh }

 

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    
! 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_service02 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" ###監控指令碼 interval 2 ###監控時間 } vrrp_instance redis { state BACKUP interface eth0 virtual_router_id 55 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_redis ###執行上面定義的chk_redis } virtual_ipaddress { 172.16.1.63/24 } notify_master /etc/keepalived/scripts/redis_master.sh notify_backup /etc/keepalived/scripts/redis_backup.sh notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh }

 

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