redis的sentinel檢測實現自動主從切換

1.環境準備

三臺主機實現:確保三臺服務器redis的版本一致,以host1爲主服務,其他爲從
host1 192.168.1.9  master
host2 192.168.1.106 slave1
host3 192.168.1.110 slave2

2.配置host1的哨兵配置文件sentinel.conf

[root@localhost ~]# vim /app/redis/etc/sentinel.conf
bind 192.168.1.9
port 6379
daemonize yes
logfile "sentinel_26379.log"
dir "/app/redis/log"
sentinel monitor mymaster 192.168.1.9 6379 2  #當有2個及以上的哨兵斷定master宕機時會選舉新的master
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000 #主觀下線的時間
sentinel parallel-syncs mymaster 1           #向新 master 同步數據的 slave 數量,數字越 小總同步時間越長
sentinel failover - timeout mymaster 180000 #所有slave指向新的master所需的超時時間
sentinel deny-scripts-reconfig yes

3.配置host2的哨兵配置文件sentinel.conf,只需修改綁定的ip其他與主相同

[root@localhost ~]# vim /app/redis/etc/sentinel.conf
bind 192.168.1.106  
……
……

4.配置host3的哨兵配置文件sentinel.conf,只需修改綁定的ip其他與主相同

[root@localhost ~]# vim /app/redis/etc/sentinel.conf
bind 192.168.1.110  
……
……

5.在命令行將host2和host3的主服務指向host1

host2:
[root@localhost ~]# redis-cli
127.0.0.1:6379> auth 12345
OK
127.0.0.1:6379> slaveof 192.168.1.9 6379
127.0.0.1:6379> config set masterauth 123456
127.0.0.1:6379> info replication
#Replication
role:master
connected_slaves:1
slave:ip=192.168.1.106,port=6379,state=online,offset=1541332,lag=1
……
……

host3:
[root@localhost ~]# redis-cli
127.0.0.1:6379> auth 12345
OK
127.0.0.1:6379> slaveof 192.168.1.9 6379
127.0.0.1:6379> config set masterauth 123456
127.0.0.1:6379> info replication
#Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.106,port=6379,state=online,offset=1541332,lag=1
slave1:ip=192.168.1.110,port=6379,state=online,offset=1541332,lag=1
……
……

6.三臺服務同時開啓哨兵服務

[root@localhost ~]# redis-server /app/redis/etc/redis.conf

7.將host1主服掛掉模擬宕機測試,測試是否其中的salve有一臺會被自動提升爲主服務

hsot1服務器:

[root@localhost ~]# ss -tnlp
State       Recv-Q Send-Q                                Local Address:Port                               Peer Address:Port              
LISTEN      0      511                                         192.168.1.9:26379                                   *:*        
users:(("redis-sentinel",pid=2413,fd=6))
LISTEN      0      511                                         192.168.1.9:6379                                     *:*  
users:(("redis-server",pid=2369,fd=7))
LISTEN      0      511                                                         127.0.0.1:6379                         *:*                  
 users:(("redis-server",pid=2369,fd=6))

root@localhost ~]# kill -9 2413

host2服務器:可以看到此服務被提升爲主服務
127.0.0.1:6379> info replication
#Keyspace
db0:keys=7,expires=0,avg_ttl=0
127.0.0.1:6379> info replication
#Replication
role:master  #角色切換爲主
connected_slaves:1  #當親從服務的連接個數
slave0:ip=192.168.1.110,port=6379,state=online,offset=1728348,lag=1
master_replid:7c8eb18c7c05b8f2d9f29028d016f9c40e8c2ce0
master_replid2:a8efce354ba8249ff264dcba60ac21030253b829
master_repl_offset:1728348
second_repl_offset:63848
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:679773
repl_backlog_histlen:1048576

host2服務器:此時的主指定爲host2服務器
127.0.0.1:6379> info replication
#Replication
role:slave
master_host:192.168.1.106   #主服務爲host2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:1758995
slave_priority:100
slave_read_only:1
……
……

8.當host1服務恢復正常,重啓,會被自動切換爲從服務

1)重啓之前先修改redis.conf配置文件,添加連接主服務的認證密碼即可
[root@localhost ~]# vim /app/redis/etc/redis.conf
 masterauth 123456
2)在重啓host1的redis服務
[root@localhost ~]# redis-server /app/redis/etc/redis.conf
3)查看其所處的狀態,可以看到被切換的從服務
127.0.0.1:6379> info replication
#Replication
role:slave
master_host:192.168.1.106  #主服務指向host2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:1829345
slave_priority:100
slave_read_only:1
……
……

9.在查看host2被提升爲新的主後的連接狀態

127.0.0.1:6379> info replication
#Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.110,port=6379,state=online,offset=1854211,lag=0
slave1:ip=192.168.1.9,port=6379,state=online,offset=1854072,lag=1
master_replid:7c8eb18c7c05b8f2d9f29028d016f9c40e8c2ce0
master_replid2:a8efce354ba8249ff264dcba60ac21030253b829
master_repl_offset:1854211
second_repl_offset:63848
……
……
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章