架構師教你Redis如何實現高可用架構

一、背景

公司的業務在大量的使用redis,訪問量大的業務我們有在使用codis集羣,redis 3.0集羣,說到redis 3.0集羣,我們線上已經跑了半年多了,集羣本身沒有出現過任務問題,但是由於我們這個業務是海外的,集羣建在aws的ec2上,由於ec2的網絡抖動或者ec2本身的原因,導致主從切換,目前aws的技術正在跟進,這個集羣目前的QPS 50w+,集羣本身已經做到了高可用和橫向擴展,但是,實際情況一些小的業務沒必要上集羣,單個實例就可以滿足業務需求,那麼我們就要想辦法如何保證單個實例的高可用,最近也在看相關的文檔,做一些測試,大家有在使用redis主從+lvs 漂VIP的方案,也有使用redis主從+哨兵 漂VIP的方案,甚至有在代碼邏輯做故障切換等等,各種各樣的方案都有,下面我介紹一下redis主從+哨兵 漂VIP的方案,後面我們打算線上大規模的使用這個方案。

二、環境

#redis

100.10.32.54:6400 主庫

100.10.32.55:6400 從庫

100.10.32.250 VIP

#sentinel

100.10.32.54:26400 sentinel 本地節點

100.10.32.55:26400 sentinel 本地節點

100.10.32.57:26400 sentinel 仲裁節點

三、部署

1、安裝

yum -y install redis

2、撰寫redis配置文件(100.10.32.54 和100.10.32.55)

vim /etc/redis_6400.conf

daemonize yes

pidfile "/var/run/redis_6400.pid"

port 6400

tcp-backlog 65535

bind 0.0.0.0

timeout 0

tcp-keepalive 0

loglevel notice

logfile "/var/log/redis/redis_6400.log"

maxmemory 8gb

maxmemory-policy allkeys-lru

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 "/data/redis/6400"

slave-serve-stale-data yes

slave-read-only yes

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

3、撰寫sentinel配置文件(100.10.32.54 、100.10.32.55 和100.10.32.57)

vim /etc/redis-sentinel6400.conf

daemonize yes

port 26400

dir "/data/redis/redis_sentinels"

pidfile "/var/run/redis/sentinel6400.pid"

logfile "/data/redis/redis_sentinels/sentinel6400.log"

sentinel monitor master6400 100.10.32.54 6400 2

sentinel down-after-milliseconds master6400 6000

sentinel failover-timeout master6400 18000

sentinel client-reconfig-script master6400 /opt/notify_master6400.sh ##仲裁節點無需添加這行配置,client-reconfig-script參數是在sentinel做failover的過程中調用腳本漂vip到新的master上

PS:

關於sentinel 的一些工作原理和參數說明,請參閱:http://redisdoc.com/topic/sentinel.html

4、撰寫漂VIP的腳本(100.10.32.54 、100.10.32.55)

vim /opt/notify_master6400.sh

#!/bin/bash

MASTER_IP=$6

LOCAL_IP='100.10.32.54' #從庫修改爲100.10.32.55

VIP='100.10.32.250'

NETMASK='24'

INTERFACE='eth0'

if [ ${MASTER_IP} = ${LOCAL_IP} ]; then

/sbin/ip addr add ${VIP}/${NETMASK} dev ${INTERFACE}

/sbin/arping -q -c 3 -A ${VIP} -I ${INTERFACE}

exit 0

else

/sbin/ip addr del ${VIP}/${NETMASK} dev ${INTERFACE}

exit 0

fi

exit 1

1

chmod +x /opt/notify_master6400.sh #賦予可執行權限

PS:

這裏大概說一下這個腳本的工作原理,sentinel在做failover的 過程中會傳出6個參數,分別是<master-name>、 <role>、 <state>、 <from-ip>、 <from-port>、 <to-ip> 、<to-port>,其中第6個參數from-ip也就是新的master的ip,對應腳本中的MASTER_IP,下面的if判斷大家應該都很瞭然了,如果MASTER_IP=LOCAL_IP,那就綁定VIP,反之刪除VIP。

5、啓動redis服務(100.10.32.54、100.10.32.55)

redis-server /etc/redis_6400.conf

6、初始化主從(100.10.32.55)

redis-cli -p 6400 slaveof 10.10.32.54 6400

7、綁定VIP到主庫(100.10.32.54)

/sbin/ip addr add 100.10.32.250/24 dev eth0

8、啓動sentinel服務(100.10.32.54、100.10.32.55、100.10.32.57)

redis-server /etc/redis-sentinel6400.conf --sentinel

至此,整個高可用方案已經搭建完成。

1.[root@localhost tmp]# redis-cli -h 100.10.32.54 -p 6400 info Replication

2.# Replication

3.role:master

4.connected_slaves:1

5.slave0:ip=100.10.32.55,port=6400,state=online,offset=72669,lag=1

6.master_repl_offset:72669

7.repl_backlog_active:1

8.repl_backlog_size:1048576

9.repl_backlog_first_byte_offset:2

10.repl_backlog_histlen:72668

1. [root@localhost tmp]# redis-cli -h 100.10.32.54 -p 26400 info Sentinel

2.# Sentinel

3.sentinel_masters:1

4.sentinel_tilt:0

5.sentinel_running_scripts:0

6.sentinel_scripts_queue_length:0

7.master0:name=master6400,status=ok,address=100.10.32.54:6400,slaves=1,sentinels=3

1.[root@localhost tmp]# ip a |grep eth0

2.2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000

3. inet 100.10.32.54/24 brd 100.10.32.255 scope global eth0

4. inet 100.10.32.250/24 scope global secondary eth0

四、測試

1、把主庫停掉

redis-cli -h 100.10.32.54 -p 6400 shutdown

2、看從庫是否提升爲主庫

[root@localhost tmp]# redis-cli -h 100.10.32.55 -p 6400 info Replication

# Replication

role:master

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

3、看VIP是否漂移到100.10.32.55上

[root@localhost tmp]# ip a |grep eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000

inet 100.10.32.55/24 brd 100.10.32.255 scope global eth0

inet 100.10.32.250/24 scope global secondary eth0

4、看Sentinel的監控狀態

[root@localhost tmp]# redis-cli -p 26400 info Sentinel

# Sentinel

sentinel_masters:1

sentinel_tilt:0

sentinel_running_scripts:0

sentinel_scripts_queue_length:0

master0:name=master6400,status=ok,address=100.10.32.55:6400,slaves=1,sentinels=3

如果你現在在JAVA這條路上掙扎,也想在IT行業拿高薪,可以參加我們的訓練營課程,選擇最適合自己的課程學習,技術大牛親授,7個月後,進入名企拿高薪。我們的課程內容有:Java工程化、高性能及分佈式、高性能、深入淺出。高架構。性能調優、Spring,MyBatis,Netty源碼分析和大數據等多個知識點。如果你想拿高薪的,想學習的,想就業前景好的,想跟別人競爭能取得優勢的,想進阿里面試但擔心面試不過的,你都可以來,q羣號爲:779792048

注:加羣要求

1、具有1-5工作經驗的,面對目前流行的技術不知從何下手,需要突破技術瓶頸的可以加。

2、在公司待久了,過得很安逸,但跳槽時面試碰壁。需要在短時間內進修、跳槽拿高薪的可以加。

3、如果沒有工作經驗,但基礎非常紮實,對java工作機制,常用設計思想,常用java開發框架掌握熟練的,可以加。

4、覺得自己很牛B,一般需求都能搞定。但是所學的知識點沒有系統化,很難在技術領域繼續突破的可以加。

5.阿里Java高級大牛直播講解知識點,分享知識,多年工作經驗的梳理和總結,帶着大家全面、科學地建立自己的技術體系和技術認知!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章