结合keepalived实现redis群集高可用故障自动切换
? ?系統架構圖:
我們所要實現的目的很簡單,
104,107 為keepalive和redis主從架構,其余服務器比如105、106均為redis從庫并且掛在vip 192.168.56.180下面。
主keepalive負責主要日常工作,從keepalive擔任備機角色,一旦主keepalive掛掉,從keepalive服務器立即使從redis轉變角色切換成master狀態開始接管任務提供服務,實現業務的無縫切換,當掛掉的服務器修好上線后繼續擔任主的角色,從庫會自動切換到slave狀態并且不影響掛載在vip下的從redis的數據同步,一滿足高并發架構的需求。
keepalive在ubuntu的安裝很簡單
| 1 2 3 4 | apt-get?install?libssl-dev apt-get?install?openssl apt-get?install?libpopt-dev apt-get?install?keepalived |
redis的安裝也很簡單:
網上很多方法,也可參考我的另一篇文章:?redis的shell安裝腳本,實現在linux下本機主從架構
網上有一篇郭冬的一篇文章給了我很大啟發故拿來參考:通過Keepalived實現Redis Failover自動故障切換,
下面我們看192.168.56.104主keepalived的配置
/etc/keepalived/keepalived.conf
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | global_defs { ????????notification_email { ????????????????409011500@qq.com ????????} ????????notification_email_from? 409011500@qq.com ????????????????smtp_server 127.0.0.1? (如果本機配置的話) ????????????????smtp_connect_timeout 30 ????????????????router_id redis-ha } vrrp_script chk_redis { ????????????????script?"/home/lhb/sh/redis_check.sh"???###監控腳本 ????????????????interval 2????????????????????????????????????????###監控時間 } vrrp_instance VI_1 { ????????state MASTER????????????????????????????###設置為MASTER ????????interface eth0??????????????????????????###監控網卡 ????????virtual_router_id 52 ????????priority 101????????????????????????????###權重值 ????????authentication { ?????????????????????auth_type PASS?????????????###加密 ?????????????????????auth_pass redis????????????###密碼 ????????} ????????track_script { ????????????????chk_redis???????????????????????###執行上面定義的chk_redis ????????} ????????virtual_ipaddress { ?????????????192.168.56.180????????????????????????###VIP ????????} ????????notify_master?/home/lhb/sh/redis_master.sh ????????notify_backup?/home/lhb/sh/redis_backup.sh } |
/home/lhb/sh/redis_master.sh
| 1 2 3 4 5 6 7 8 9 10 11 | #!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/usr/local/redis/log/keepalived-redis-state.log" echo?"[master]"?>> $LOGFILE date?>> $LOGFILE echo?"Being master...."?>> $LOGFILE 2>&1 echo?"Run SLAVEOF cmd ..."?>> $LOGFILE $REDISCLI SLAVEOF 192.168.56.107 6379 >> $LOGFILE? 2>&1 sleep?10?#延遲10秒以后待數據同步完成后再取消同步狀態 echo?"Run SLAVEOF NO ONE cmd ..."?>> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1 |
/home/lhb/sh/redis_backup.sh
| 1 2 3 4 5 6 7 8 9 | #!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/usr/local/redis/log/keepalived-redis-state.log" echo?"[backup]"?>> $LOGFILE date?>> $LOGFILE echo?"Being slave...."?>> $LOGFILE 2>&1 sleep?15?#延遲15秒待數據被對方同步完成之后再切換主從角色 echo?"Run SLAVEOF cmd ..."?>> $LOGFILE $REDISCLI SLAVEOF 192.168.56.107 6379 >> $LOGFILE? 2>&1 |
/usr/local/redis/etc/redis.conf
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | daemonize?yes pidfile?/var/run/redis.pid port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile?"/usr/local/redis/log/redis.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error?yes rdbcompression?yes rdbchecksum?yes dbfilename dump.rdb dir?/usr/local/redis/data slave-serve-stale-data?yes slave-read-only no repl-disable-tcp-nodelay no slave-priority 100 appendonly?yes appendfilename?"appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 notify-keyspace-events?"" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 activerehashing?yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync?yes |
192.168.56.107從keepalived的配置
/etc/keepalived/keepalived.conf
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | global_defs { ????????notification_email { ????????????????409011500@qq.com ????????} ????????notification_email_from 409011500@qq.com ????????????????smtp_server 127.0.0.1 ????????????????smtp_connect_timeout 30 ????????????????router_id redis-ha } vrrp_script chk_redis { ????????????????script?"/home/lhb/sh/redis_check.sh"???###監控腳本 ????????????????interval 2????????????????????????????????????????###監控時間 } vrrp_instance VI_1 { ????????state BACKUP????????????????????????????????###設置為BACKUP ????????interface eth0??????????????????????????????###監控網卡 ????????virtual_router_id 52 ????????priority 100????????????????????????????????###比MASTRE權重值低 ????????authentication { ?????????????????????auth_type PASS ?????????????????????auth_pass redis????????????????###密碼與MASTRE相同 ????????} ????????track_script { ????????????????chk_redis???????????????????????###執行上面定義的chk_redis ????????} ????????virtual_ipaddress { ?????????????192.168.56.180?????????????????????????###VIP ????????} ????????notify_master?/home/lhb/sh/redis_master.sh ????????notify_backup?/home/lhb/sh/redis_backup.sh } |
/home/lhb/sh/redis_master.sh
| 1 2 3 4 5 6 7 8 9 10 11 | #!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/usr/local/redis/log/keepalived-redis-state.log" echo?"[master]"?>> $LOGFILE date?>> $LOGFILE echo?"Being master...."?>> $LOGFILE 2>&1 echo?"Run SLAVEOF cmd ..."?>> $LOGFILE $REDISCLI SLAVEOF 192.168.56.104 6379 >> $LOGFILE? 2>&1 sleep?10?#延遲10秒以后待數據同步完成后再取消同步狀態 echo?"Run SLAVEOF NO ONE cmd ..."?>> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1 |
/home/lhb/sh/redis_backup.sh
| 1 2 3 4 5 6 7 8 9 | #!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/usr/local/redis/log/keepalived-redis-state.log" echo?"[backup]"?>> $LOGFILE date?>> $LOGFILE echo?"Being slave...."?>> $LOGFILE 2>&1 sleep?15?#延遲15秒待數據被對方同步完成之后再切換主從角色 echo?"Run SLAVEOF cmd ..."?>> $LOGFILE $REDISCLI SLAVEOF 192.168.56.104 6379 >> $LOGFILE? 2>&1 |
/home/lhb/sh/redis_check.sh
| 1 2 3 4 5 6 7 8 9 | #!/bin/bash ALIVE=`/usr/local/redis/bin/redis-cli?PING` if?[?"$ALIVE"?==?"PONG"?];?then ??echo?$ALIVE ??exit?0 else ??echo?$ALIVE ??exit?1 fi |
/usr/local/redis/etc/redis_slave.conf
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | daemonize?yes pidfile?/var/run/redis_salve.pid port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile?"/usr/local/redis/log/redis_slave.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error?yes rdbcompression?yes rdbchecksum?yes dbfilename dump_salve.rdb dir?/usr/local/redis/data slave-serve-stale-data?yes slave-read-only no repl-disable-tcp-nodelay no slave-priority 100 appendonly?yes appendfilename?"appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 notify-keyspace-events?"" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 activerehashing?yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync?yes SLAVEOF 192.168.56.104 6379 |
192.168.56.105、192.168.56.106 redis配置文件相同:
/usr/local/redis/etc/redis_salve.conf
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | daemonize?yes pidfile?/var/run/redis_salve.pid port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile?"/usr/local/redis/log/redis_slave.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error?yes rdbcompression?yes rdbchecksum?yes dbfilename dump_salve.rdb dir?/usr/local/redis/data slave-serve-stale-data?yes slave-read-only no repl-disable-tcp-nodelay no slave-priority 100 appendonly no appendfilename?"appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 notify-keyspace-events?"" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 activerehashing?yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync?yes SLAVEOF 192.168.56.180 6379 |
好了,下面我們就來展示一下是否是我們期待的那樣?
啟動192.168.56.104,192.168.56.107上的redis,keepalvied,以及192.168.56.105,192.168.56.106上的redis
在192.168.56.104 我們看到以下結果,已經獲取vip:192.168.56.180
在192.168.56.107 我們看到以下結果,并沒有獲得vip192.168.56.180
在192.168.56.105,192.168.56.106我們看到相同結果redis掛載在vip:192.168.56.180下
這時我們把192.168.56.104下的redis給關閉掉,看一下結果:vip已經釋放
然后到192.168.56.107下看一下信息: 已經獲取vip,并且redis已經從salve切換到master,并且從庫105、106狀態均為online
192.168.56.105、192.168.56.106 redis結果:一直掛載在vip下面并且link_status處于up狀態
由此可見,當主redis掛掉后,備機上的redis立即切換為master,并且不影響業務正常運行。
我們然后把主上redis從新啟動,看到下面結果: 主服務器獲取vip
redis信息:我們可以看到192.168.56.107備機已經出現在master的slave列表中
然后切換到:192.168.56.107看一下信息: 已經釋放vip,并且redis已經從master切換到slave狀態,并且指向的master是192.168.56.104
在看192.168.56.105、192.168.56.106 redis信息,亦然指向192.168.56.180
看到這里,已經滿足我們系統架構最初的所有設想功能。有感興趣的同學可以一起交流。謝謝
本文轉自birdinroom 51CTO博客,原文鏈接:http://blog.51cto.com/birdinroom/1401663,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的结合keepalived实现redis群集高可用故障自动切换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017.11.7 Python 制作E
- 下一篇: hadoop jetty的应用