前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用Redis Sentinel实现Redis主从自动切换

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

作者头像
星哥玩云
发布2022-08-18 15:24:50
7070
发布2022-08-18 15:24:50
举报
文章被收录于专栏:开源部署开源部署

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档