Redis 搭建主从加哨兵

哨兵(sentinel)是 Redis 的高可用性(High Availability)的解决方案:

由一个或多个 sentinel 实例组成 sentinel 集群可以监视一个或多个主服务器和多个从服务器。

当主服务器进入下线状态时,sentinel 可以将该主服务器下的某一从服务器升级为主服务器继续提供服 务,从而保证 redis 的高可用性。

image.png

Redis 主从搭建

{{Redis 服务搭建主从复制}}

Redis Sentinel 集群搭建

Redis-Sentinel 搭建三台,必须搭建奇数台,因为要投票选举,且保证高可用,三台算是蛮合适的了

我们是在 Redis 主从搭建的基础上进行的,目前已有 Redis-master 、redis-slave1 和 redis-slave2

先看下自己安装的 redis 下有没有 sentinel.conf,如果没有则先复制过来一份

cp /root/redis-5.0.5/sentinel.conf /usr/redis-ms/redis-sentinel1/bin

复制一份 redis-master 为 redis-sentinel1

cp -r redis-master redis-sentinel1
cd /usr/redis-ms/redis-sentinel1/bin
vim sentinel.conf

修改 sentinel.conf 的内容,设置后台进程 yes ,有密码就指定密码,其他默认就好

# 哨兵sentinel实例运行的端口 默认26379 
port 26379
# 将`daemonize`由`no`改为`yes` 
daemonize yes
# sentinel monitor <master-name> <ip> <redis-port> <quorum> 
# 其中quorum是投票数,当有2票认为master挂掉,就选主切换,改成自己的ip端口
sentinel monitor mymaster 127.0.0.1 6379 2
# sentinel auth-pass <master-name> <password> 
# 设置redis主从服务器的密码且必须redis主从密码一致,没有则注释掉
sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
# sentinel down-after-milliseconds <master-name> <milliseconds> 
#指定多少毫秒之后 主节点没有应答哨兵sentinel 此时 哨兵主观上认为主节点下线 默认30秒
sentinel down-after-milliseconds mymaster 30000
# sentinel parallel-syncs <master-name> <numslaves> 
# 这个配置项指定了在发生failover主备切换时最多可以有多少个slave同时对新的master进行 同步默认1
sentinel parallel-syncs mymaster 1
# sentinel failover-timeout <master-name> <milliseconds>
# 故障转移的超时时间,当进行failover时,配置所有slaves指向新的master所需的最大时间。默认三分钟
sentinel failover-timeout mymaster 180000

复制一份 redis-sentinel1 为 redis-sentinel 2

修改其端口号

cp redis-sentinel1 redis-sentinel2
cd /usr/redis-ms/redis-sentinel2/bin
vim sentinel.conf 
port 26382

复制一份 redis-sentinel1 为 redis-sentinel 3

修改其端口号

cp redis-sentinel1 redis-sentinel3
cd /usr/redis-ms/redis-sentinel3/bin
vim sentinel.conf 
port 26383

Redis 主从和 sentinel 集群启动

分别启动三个 redis :redis-master、redis-slave1、redis-slave2

三个 sentinel : redis-sentinel1、redis-sentinel2、redis-sentinel3

#启动三个redis
cd /usr/redis-ms/redis-master/bin
./redis-server redis.conf
cd /usr/redis-ms/redis-slave1/bin
./redis-server redis.conf
cd /usr/redis-ms/redis-slave2/bin
./redis-server redis.conf
#启动三个sentinel
cd /usr/redis-ms/redis-sentinel1/bin
./redis-sentinel sentinel.conf
cd /usr/redis-ms/redis-sentinel2/bin
./redis-sentinel sentinel.conf
cd /usr/redis-ms/redis-sentinel3/bin
./redis-sentinel sentinel.conf

查看启动情况

➜  bin ps -ef | grep redis
root       7593      1  0 11:02 ?        00:00:05 ./redis-sentinel *:26383 [sentinel]
root       7665      1  0 11:29 ?        00:00:00 ./redis-server *:6379
root       7674      1  0 11:30 ?        00:00:00 ./redis-server *:6381
root       7685      1  0 11:30 ?        00:00:00 ./redis-server *:6382
root       7697      1  0 11:30 ?        00:00:00 ./redis-sentinel *:26382 [sentinel]
root       7707      1  0 11:30 ?        00:00:00 ./redis-sentinel *:26381 [sentinel]

Redis 主从加哨兵测试

测试主从复制情况

redis-master

#启动三个redis
#master
cd /usr/redis-ms/redis-master/bin
./redis-cli
#测试主从复制 ok
127.0.0.1:6379> set sunday sky
OK
127.0.0.1:6379> get sunday
"sky"
#查看master信息
127.0.0.1:6379> info
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=121740,lag=0
slave1:ip=127.0.0.1,port=6382,state=online,offset=121474,lag=1
master_replid:1ad13cc44397824972b9de8429aadab2a90f6e05
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:121740
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:121740

redis-slave1

#slave1
cd /usr/redis-ms/redis-slave1/bin
./redis-cli -p 6381
#测试主从复制 ok
127.0.0.1:6381> get sunday
"sky"
127.0.0.1:6381> set book library
(error) READONLY You can not write against a read only replica
#查看salve1信息
127.0.0.1:6381> info
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:145981
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:1ad13cc44397824972b9de8429aadab2a90f6e05
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:145981
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:145981

redis-slave2

cd /usr/redis-ms/redis-slave2/bin
./redis-cli -p 6382
#测试主从复制 ok
127.0.0.1:6382> get sunday
"sky"
127.0.0.1:6382> set book library
(error) READONLY You can not write against a read only replica.
#查看master信息
127.0.0.1:6382> info
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:169690
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:1ad13cc44397824972b9de8429aadab2a90f6e05
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:169690
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:570
repl_backlog_histlen:169121

测试哨片工作情况

关闭主服务 redis-master

ps -ef | grep redis
root       7593      1  0 11:02 ?        00:00:09 ./redis-sentinel *:26383 [sentinel]
root       7665      1  0 11:29 ?        00:00:02 ./redis-server *:6379
root       7674      1  0 11:30 ?        00:00:02 ./redis-server *:6381
root       7685      1  0 11:30 ?        00:00:02 ./redis-server *:6382
root       7697      1  0 11:30 ?        00:00:03 ./redis-sentinel *:26382 [sentinel]
root       7707      1  0 11:30 ?        00:00:03 ./redis-sentinel *:26381 [sentinel]
root       7744   7400  0 11:32 pts/4    00:00:00 ./redis-cli
root       7822   7755  0 11:37 pts/1    00:00:00 ./redis-cli -p 6381
root       7825   7754  0 11:38 pts/0    00:00:00 ./redis-cli -p 6382
kill -9 7665

再次测试 redis-slave1 和 redis-slave2

redis-master

127.0.0.1:6379> set saturday 6
Could not connect to Redis at 127.0.0.1:6379: Connection refused
#此时主服务器变成了redis-slave2,再次重启原主服务测试发现主服务器已经变成了从服务器
➜  bin ./redis-server redis.conf
7846:C 07 Aug 2021 11:57:57.291 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7846:C 07 Aug 2021 11:57:57.291 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=7846, just started
7846:C 07 Aug 2021 11:57:57.291 # Configuration loaded
➜  bin ./redis-cli
127.0.0.1:6379> set sunday 7
(error) READONLY You can not write against a read only replica.
127.0.0.1:6379> info
# Replication
role:slave
master_host:127.0.0.1
master_port:6382
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:599430
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:48a0b03ad4717e90283a7761d8324ab4b1c640ee
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:599430
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:326716
repl_backlog_histlen:272715

redis-slave1

127.0.0.1:6381> set saturday 6
(error) READONLY You can not write against a read only replica.
127.0.0.1:6381> get saturday
"6"
#此时主服务器变成了redis-slave2
127.0.0.1:6381> info 
# Replication
role:slave
master_host:127.0.0.1
master_port:6382
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:621529
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:48a0b03ad4717e90283a7761d8324ab4b1c640ee
master_replid2:1ad13cc44397824972b9de8429aadab2a90f6e05
master_repl_offset:621529
second_repl_offset:226608
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:621529

redis-slave2

127.0.0.1:6382> set saturday 6
OK
127.0.0.1:6382> get saturday
"6"
#此时主服务器变成了redis-slave2
127.0.0.1:6382> info
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=630363,lag=0
slave1:ip=127.0.0.1,port=6379,state=online,offset=630230,lag=1
master_replid:48a0b03ad4717e90283a7761d8324ab4b1c640ee
master_replid2:1ad13cc44397824972b9de8429aadab2a90f6e05
master_repl_offset:630363
second_repl_offset:226608
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:570
repl_backlog_histlen:629794

查看下 sentinel.conf 的配置文件

vim sentinel.conf
#可以看到监控的主服务器已经发生了变化了
sentinel monitor mymaster 127.0.0.1 6382 2

成功实现了主从复制 + 哨兵实现主从切换高可用

Redis 哨兵实现主从原理

分别查看 redis-master 的 redis.conf

cd /usr/redis-ms/redis-master/bin
vim redis.conf
# Generated by CONFIG REWRITE
replicaof 127.0.0.1 6382
#可以看到redis-master添加了这一行指定主服务器的代码

分别查看 redis-slave1 的 redis.conf

cd /usr/redis-ms/redis-slave1/bin
vim redis.conf
# Redis configuration file example.
replicaof 127.0.0.1 6382
#可以看到redis-slave1指定的主服务器变成了6382

分别查看 redis-slave2 的 redis.conf

cd /usr/redis-ms/redis-slave2/bin
vim redis.conf
# replicaof <masterip> <masterport>
#已查找到文件结尾,再从开头继续查找
#replicaof已经被哨兵删掉了

哨兵实现主从切换是通过修改主从 redis 服务器的 redis.conf 的 replicaof 来实现的

且其本身的 sentinel.conf 也会更改,监控新的主服务器地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值