Redis哨兵模式
环境
角色 | IP | 端口 |
---|---|---|
master | 10.10.10.100 | 6379 |
slave1 | 10.10.10.101 | 6379 |
slave2 | 10.10.10.102 | 6379 |
sentinel | 10.10.10.100 | 26379 |
sentinel | 10.10.10.101 | 26379 |
sentinel | 10.10.10.102 | 26379 |
主从复制配置很简单,不做赘述
10.10.10.100:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.10.10.101,port=6379,state=online,offset=5695,lag=1
slave1:ip=10.10.10.102,port=6379,state=online,offset=5695,lag=0
master_replid:ed0b450585c5395c1eebce8eb8b5273df35463ca
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5695
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:5695
10.10.10.101:6379> info replication
# Replication
role:slave
master_host:10.10.10.100
master_port:6379
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:5737
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:ed0b450585c5395c1eebce8eb8b5273df35463ca
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5737
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:5737
10.10.10.102:6379> info replication
# Replication
role:slave
master_host:10.10.10.100
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:5807
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:ed0b450585c5395c1eebce8eb8b5273df35463ca
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5807
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1523
repl_backlog_histlen:4285
复制一份安装包里面的配置文件sentinel.conf,修改以下master配置即可
sentinel monitor mymaster 10.10.10.100 6379 2
##mymaster名字随便取,但是需要保持一致
## 2 表示获取的票数,有三台哨兵,2台及2台以上认为master挂掉了,才进行切换,这个值需要大于哨兵总数的一半
注意!!哨兵模式在配置的时候,如果三个哨兵启动在不同的机器,需要配置bind IPADDR ,否则哨兵将不起作用
# *** IMPORTANT ***
#
# By default Sentinel will not be reachable from interfaces different than
# localhost, either use the 'bind' directive to bind to a list of network
# interfaces, or disable protected mode with "protected-mode no" by
# adding it to this configuration file.
#
# Before doing that MAKE SURE the instance is protected from the outside
# world via firewalling or other means.
#
# For example you may use one of the following:
#
# bind 127.0.0.1 192.168.1.1
#
# protected-mode no
# port <sentinel-port>
# The port that this sentinel instance will run on
bind 10.10.10.100
port 26379
如果不配置bind,将会出现以下现象,会自动断开其他哨兵的连接
12642:X 31 May 14:03:12.133 # Sentinel ID is 93f88deff364d6f6842540a85b6ad066b6e1c72d
12642:X 31 May 14:03:12.133 # +monitor master mymaster 10.10.10.100 6379 quorum 2
12642:X 31 May 14:03:12.133 * +slave slave 10.10.10.101:6379 10.10.10.101 6379 @ mymaster 10.10.10.100 6379
12642:X 31 May 14:03:12.134 * +slave slave 10.10.10.102:6379 10.10.10.102 6379 @ mymaster 10.10.10.100 6379
12642:X 31 May 14:03:17.880 * +sentinel sentinel f99e627401f6e23d1242c18764b8d820a63ef1b5 10.10.10.101 26379 @ mymaster 10.10.10.100 6379
12642:X 31 May 14:03:21.432 * +sentinel sentinel d9463b90bd223c6308969dc3a7c7ae7299f4cc93 10.10.10.102 26379 @ mymaster 10.10.10.100 6379
12642:X 31 May 14:03:47.904 # +sdown sentinel f99e627401f6e23d1242c18764b8d820a63ef1b5 10.10.10.101 26379 @ mymaster 10.10.10.100 6379
12642:X 31 May 14:03:51.494 # +sdown sentinel d9463b90bd223c6308969dc3a7c7ae7299f4cc93 10.10.10.102 26379 @ mymaster 10.10.10.100 6379
配置成功后会在配置文件后面添加主从信息和哨兵节点信息
# Generated by CONFIG REWRITE
sentinel known-slave mymaster 10.10.10.102 6379
sentinel known-slave mymaster 10.10.10.101 6379
sentinel known-sentinel mymaster 10.10.10.102 26379 d9463b90bd223c6308969dc3a7c7ae7299f4cc93
sentinel known-sentinel mymaster 10.10.10.101 26379 f99e627401f6e23d1242c18764b8d820a63ef1b5
sentinel current-epoch 4
把master down掉,观察是否会进行切换,三台哨兵,会选举出来一台进行切换动作
12677:X 31 May 14:19:33.030 # +sdown master mymaster 10.10.10.100 6379
12677:X 31 May 14:19:33.097 # +odown master mymaster 10.10.10.100 6379 #quorum 3/2
12677:X 31 May 14:19:33.097 # +new-epoch 5
12677:X 31 May 14:19:33.097 # +try-failover master mymaster 10.10.10.100 6379
12677:X 31 May 14:19:33.098 # +vote-for-leader 93f88deff364d6f6842540a85b6ad066b6e1c72d 5
12677:X 31 May 14:19:33.100 # d9463b90bd223c6308969dc3a7c7ae7299f4cc93 voted for 93f88deff364d6f6842540a85b6ad066b6e1c72d 5
12677:X 31 May 14:19:33.100 # f99e627401f6e23d1242c18764b8d820a63ef1b5 voted for 93f88deff364d6f6842540a85b6ad066b6e1c72d 5
12677:X 31 May 14:19:33.154 # +elected-leader master mymaster 10.10.10.100 6379
12677:X 31 May 14:19:33.154 # +failover-state-select-slave master mymaster 10.10.10.100 6379
12677:X 31 May 14:19:33.238 # +selected-slave slave 10.10.10.102:6379 10.10.10.102 6379 @ mymaster 10.10.10.100 6379
12677:X 31 May 14:19:33.238 * +failover-state-send-slaveof-noone slave 10.10.10.102:6379 10.10.10.102 6379 @ mymaster 10.10.10.100 6379
12677:X 31 May 14:19:33.310 * +failover-state-wait-promotion slave 10.10.10.102:6379 10.10.10.102 6379 @ mymaster 10.10.10.100 6379
12677:X 31 May 14:19:33.835 # +promoted-slave slave 10.10.10.102:6379 10.10.10.102 6379 @ mymaster 10.10.10.100 6379
12677:X 31 May 14:19:33.835 # +failover-state-reconf-slaves master mymaster 10.10.10.100 6379
12677:X 31 May 14:19:33.924 * +slave-reconf-sent slave 10.10.10.101:6379 10.10.10.101 6379 @ mymaster 10.10.10.100 6379
12677:X 31 May 14:19:34.242 # -odown master mymaster 10.10.10.100 6379
12677:X 31 May 14:19:34.886 * +slave-reconf-inprog slave 10.10.10.101:6379 10.10.10.101 6379 @ mymaster 10.10.10.100 6379
12677:X 31 May 14:19:34.886 * +slave-reconf-done slave 10.10.10.101:6379 10.10.10.101 6379 @ mymaster 10.10.10.100 6379
12677:X 31 May 14:19:34.940 # +failover-end master mymaster 10.10.10.100 6379
12677:X 31 May 14:19:34.940 # +switch-master mymaster 10.10.10.100 6379 10.10.10.102 6379
12677:X 31 May 14:19:34.940 * +slave slave 10.10.10.101:6379 10.10.10.101 6379 @ mymaster 10.10.10.102 6379
12677:X 31 May 14:19:34.940 * +slave slave 10.10.10.100:6379 10.10.10.100 6379 @ mymaster 10.10.10.102 6379
12677:X 31 May 14:20:04.962 # +sdown slave 10.10.10.100:6379 10.10.10.100 6379 @ mymaster 10.10.10.102 6379
三台哨兵,挂了一台可以继续工作,挂了两台就不行了,因为票数是2,如果票数是1的话,只有一台哨兵也是可以工作的
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 289211569@qq.com