Redis 之 哨兵模式

一、哨兵模式的概念

哨兵模式是一種特殊的模式,首先Redis提供了哨兵的命令,哨兵是一個獨立的進程,作爲進程,它會獨立運行。其原理是哨兵通過發送命令,等待Redis服務器響應,從而監控運行的多個Redis實例。
Redis  之  哨兵模式

二、實驗環境

Redis  之  哨兵模式

三、安裝redis服務

1、指定外部安裝源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
2、安裝redis
yum -y install redis

四、修改master主庫的配置文件

1、vi /etc/redis.conf

#修改綁定IP地址爲本機IP
bind 10.3.152.78   

#以守護進程在後臺運行
daemonize yes

五、修改兩臺從庫的redis.conf配置文件

1、vi /etc/redis.conf

#分別修改綁定的對應主機IP地址
bind 10.3.151.34

#以守護進程在後臺運行
daemonize yes

#指定主庫IP地址與端口
slaveof 10.3.152.78 6379

六、啓動三臺服務器的redis服務

1、service redis start
2、在主庫上檢查redis同步狀態

[root@ops-site redis]# redis-cli -h 10.3.152.78
10.3.152.78:6379> info replication
# Replication
role:master       #  當前主機爲master角色
connected_slaves:2        #有兩個從庫連接過來
slave0:ip=10.3.151.86,port=6379,state=online,offset=228889,lag=1       #slave0信息
slave1:ip=10.3.151.34,port=6379,state=online,offset=228889,lag=1       #slave1信息
master_repl_offset:229163
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:229162

七、在主庫編輯redis-sentinel.conf 文件

bind 0.0.0.0
port 26379
dir /tmp
#重點 10.3.152.78 爲主庫,6379爲主庫端口,2爲當兩個哨兵同時成立
sentinel monitor mymaster 10.3.152.78 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 5000
logfile /var/log/redis/sentinel.log

八、把當前主庫的redis-sentinel.conf 配置文件複製到從庫中

九、啓動三臺服務器的redis-sentinel服務

service redis-sentinel start

十、查看當前sentinel狀態(可在任何一臺服務器上運行)

[root@ops-site ~]# redis-cli -h 10.3.152.78 -p 26379
10.3.152.78:26379> 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=mymaster,status=ok,address=10.3.152.78:6379,slaves=2,sentinels=3

十一、驗證檢查哨兵是否成功,主從是否自動切換

1、分別在主庫上停止redis服務,看是否會切換到別的服務器作爲master
2、啓動主庫的redis,看是否會自動從當前的主庫中同步數據。

十二、JAVA連接哨兵羣集模式

Redis  之  哨兵模式

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