阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

利用Redis Sentinel实现Redis主从自动切换

138次阅读
没有评论

共计 3737 个字符,预计需要花费 10 分钟才能阅读完成。

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
……
……

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计3737字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中