利用Redis Sentinel实现Redis主从自动切换
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
……
……