最近公司項目要求Redis集群且高可用拌滋,在查詢了一系列文章藕溅,再結(jié)合項目實際情況,所以采用了這一套高可用集群方案
方案選型
Redis集群有三種方案實現(xiàn):
-
Redis官方集群方案 Redis Cluster
這種方案是redis官方提供的陆赋,采用slot的概念烦衣,一共分成16384個槽。對key的值進行散列勤众,分配到這16384個slot中的某一個中建峭。但是這個Redis集群,要保證16384個槽對應的node都正常工作决摧,如果某個node發(fā)生故障,那它負責的slots也就失效凑兰,整個集群將不能工作掌桩。這不符合我們的要求:高可用。
-
利用代理中間件實現(xiàn)大規(guī)模Redis集群
過于復雜姑食,并且我們redis的使用量不是很大波岛,只需要保證穩(wěn)定即可(高可用)。所以這個方案也沒有采納音半。
-
Redis Sentinel主從高可用方案
部署redis主從服務(1個master则拷,多個salve),然后通過redis官方的監(jiān)控工具Sentinel(哨兵)曹鸠,對每個節(jié)點進行監(jiān)控煌茬,實現(xiàn)自動故障遷移,即master死掉彻桃,將salve升級為master坛善。基本原理是:心跳機制+投票裁決邻眷。
環(huán)境準備
- 下載redis源碼包眠屎。地址
- 準備1臺服務器(按照部署情況,共有5個端口)
- 5個端口中肆饶,1個master(6379)改衩,2個slave(6380,6381),2個sentinel(哨兵)(63791, 63792)
sentinel配置(參數(shù)配置查看附錄1)
-
配置文件_63791(
sentinel_63791.conf
)# 端口 port 63791 daemonize yes # 很重要驯镊,見附錄2 protected-mode no # 日志文件 logfile "/opt/redis-cluster/logs/sentinel_63791.log" #master-1 # 監(jiān)控master # sentinel monitor master_name ip port quorum # quorum是一個數(shù)字葫督,指明當有多少個sentinel認為一個master失效時,master才算真正失效阿宅。 sentinel monitor master-1 10.211.55.10 6381 1 # 這個配置項指定了需要多少失效時間候衍,一個master才會被這個sentinel主觀地認為是不可用的。 單位是毫秒洒放,默認為30秒 sentinel down-after-milliseconds master-1 5000 sentinel failover-timeout master-1 18000 # 設(shè)置連接master和slave時的密碼,master和slave用戶名密碼要一致 sentinel auth-pass master-1 test123 sentinel parallel-syncs master-1 1
-
配置文件_63792(
sentinel_63792.conf
)port 63792 daemonize yes protected-mode no logfile "/opt/redis-cluster/logs/sentinel_63792.log" #master-1 sentinel monitor master-1 10.211.55.10 6381 1 sentinel down-after-milliseconds master-1 5000 sentinel failover-timeout master-1 18000 sentinel auth-pass master-1 test123 sentinel parallel-syncs master-1 1
Master節(jié)點配置
redis_master_6379.conf
配置:
port 6379
daemonize yes
requirepass test123
masterauth test123
兩個slave節(jié)點配置
-
redis_slave_6380.conf
port 6380 daemonize yes requirepass "test123" slaveof 10.211.55.10 6379 masterauth "sunhao123" bind 0.0.0.0
-
redis_slave_6381.conf
port 6381 daemonize yes requirepass "test123" slaveof 10.211.55.10 6379 masterauth "sunhao123" bind 0.0.0.0
啟動服務
按如下順序依次啟動服務:
redis-server /opt/redis-cluster/redis_master_6379.conf
redis-server /opt/redis-cluster/redis_slave_6380.conf
redis-server /opt/redis-cluster/redis_slave_6381.conf
redis-sentinel /opt/redis-cluster/sentinel_63791.conf
redis-sentinel /opt/redis-cluster/sentinel_63792.conf
查看各個節(jié)點的狀態(tài)
-
查看全部節(jié)點狀態(tài)
[root@CentOS-3 redis-cluster]# ps aux | grep redis root 1782 0.0 0.0 103252 824 pts/4 S+ 21:36 0:00 grep redis root 25331 0.1 0.3 133528 7648 ? Ssl 18:06 0:20 redis-server 0.0.0.0:6380 root 25336 0.1 0.5 135576 9720 ? Ssl 18:06 0:21 redis-server 0.0.0.0:6381 root 29303 0.2 0.4 133524 7700 ? Ssl 18:27 0:32 redis-sentinel *:63791 [sentinel] root 29308 0.2 0.4 133524 7704 ? Ssl 18:27 0:33 redis-sentinel *:63792 [sentinel] root 29486 0.1 0.3 133528 7648 ? Ssl 18:28 0:18 redis-server 0.0.0.0:6379
-
查看master狀態(tài)
[root@CentOS-3 redis-cluster]# redis-cli -h 10.211.55.10 -p 6379 10.211.55.10:6379> auth test123 OK 10.211.55.10:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=10.211.55.10,port=6380,state=online,offset=1568095,lag=1 slave1:ip=10.211.55.10,port=6381,state=online,offset=1568095,lag=1 master_repl_offset:1568095 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:519520 repl_backlog_histlen:1048576 10.211.55.10:6379>
-
查看slave的狀態(tài):
[root@CentOS-3 redis-cluster]# redis-cli -h 10.211.55.10 -p 6380 10.211.55.10:6380> auth test123 OK 10.211.55.10:6380> info replication # Replication role:slave master_host:10.211.55.10 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:1578354 slave_priority:100 slave_read_only:1 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 10.211.55.10:6380>
-
查看sentinel的狀態(tài):
[root@CentOS-3 redis-cluster]# redis-cli -h 10.211.55.10 -p 63791 10.211.55.10:63791> info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=master-1,status=ok,address=10.211.55.10:6379,slaves=2,sentinels=2 10.211.55.10:63791>
驗證redis sentinel的主從切換:
首先關(guān)閉master節(jié)點蛉鹿,即kill掉master進程
-
查看sentinel服務,發(fā)現(xiàn)端口6381升級為master節(jié)點往湿,這時sentinel完成故障自動切換妖异。
[root@CentOS-3 redis-cluster]# redis-cli -h 10.211.55.10 -p 63791 10.211.55.10:63791> info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=master-1,status=ok,address=10.211.55.10:6381,slaves=2,sentinels=2 10.211.55.10:63791>
-
啟動剛才被shutdown的6379服務并查看惋戏,發(fā)現(xiàn)它變成了slave服務。
[root@CentOS-3 redis-cluster]# redis-cli -h 10.211.55.10 -p 6379 10.211.55.10:6379> auth test123 OK 10.211.55.10:6379> info replication # Replication role:slave master_host:10.211.55.10 master_port:6381 master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_repl_offset:15074 slave_priority:100 slave_read_only:1 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 10.211.55.10:6379>
附錄
-
Redis搭建sentinel,無法主從自動切換,一直卡在-sdown sentinel
redis-sentinel有保護模式他膳,所以要將這個模塊關(guān)閉响逢!
protected-mode no
-
redis-sentinel配置項詳細說明
參考鏈接