Redis的哨兵(sentinel)配置和Python程序应用示例(2)

Master机器查看进程:
root      84286  57342  0 Oct18 pts/2    00:00:00 redis-cli -h 192.168.221.160 -p 26379
root      84302      1  0 Oct18 ?        00:03:41 redis-server *:26379                           
redis    84328      1  0 Oct18 ?        00:03:01 /usr/sbin/redis-server 192.168.221.160:6379
root      84391  83553  0 Oct18 pts/3    00:00:00 redis-cli -h 192.168.221.160
root      86746  86651  0 09:35 pts/6    00:00:00 grep redis

Slave机器查看进程:
redis    53505      1  0 Oct18 ?        00:02:17 /usr/sbin/redis-server 192.168.221.161:6379
root      53510  52922  0 Oct18 pts/0    00:00:00 redis-cli -h 192.168.221.161
root      53542      1  0 Oct18 ?        00:02:53 redis-server *:26379                           
root      54767  54723  0 09:34 pts/1    00:00:00 grep redis

查看master状态:
[root@MidApp ~]# redis-cli -h 192.168.221.160 
192.168.221.160:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.221.161,port=6379,state=online,offset=9243499,lag=0
master_repl_offset:9243644
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:8195069
repl_backlog_histlen:1048576

查看slave状态:
[root@DB ~]# redis-cli -h 192.168.221.161 -p 6379
192.168.221.161:6379> info replication
# Replication
role:slave
master_host:192.168.221.160
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:9200304
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

查看sentinel状态:
[root@DB ~]# redis-cli -h 192.168.221.160 -p 26379
192.168.221.160:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=192.168.221.160:6379,slaves=1,sentinels=2

六、Redis-Sentinel主从切换测试
首先手动关闭主redis(192.168.221.160),查看sentinel日志,可以看到192.168.221.161已经变成了主redis,自动完成了切换:
[84302] 18 Oct 15:22:31.539 * +failover-state-wait-promotion slave 192.168.221.161:6379 192.168.221.161 6379 @ mymaster 192.168.221.160 6379
[84302] 18 Oct 15:22:32.434 # +promoted-slave slave 192.168.221.161:6379 192.168.221.161 6379 @ mymaster 192.168.221.160 6379
[84302] 18 Oct 15:22:32.434 # +failover-state-reconf-slaves master mymaster 192.168.221.160 6379
[84302] 18 Oct 15:22:32.498 # +failover-end master mymaster 192.168.221.160 6379
[84302] 18 Oct 15:22:32.499 # +switch-master mymaster 192.168.221.160 6379 192.168.221.161 6379
[84302] 18 Oct 15:22:32.500 * +slave slave 192.168.221.160:6379 192.168.221.160 6379 @ mymaster 192.168.221.161 6379
[84302] 18 Oct 15:22:37.552 # +sdown slave 192.168.221.160:6379 192.168.221.160 6379 @ mymaster 192.168.221.161 6379

把刚才关闭的redis再次启动之后检查redis主从状态:
[root@DB ~]# redis-cli -h 192.168.221.160 -p 6379
192.168.221.160:6379> info replication
# Replication
role:slave
master_host:192.168.221.161
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:9200304
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

[root@MidApp ~]# redis-cli -h 192.168.221.161 
192.168.221.161:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.221.160,port=6379,state=online,offset=9243499,lag=0
master_repl_offset:9243644
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:8195069
repl_backlog_histlen:1048576

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/aa82575f9e9f5fc5b83e7ad2de6c2c91.html