Centos7搭建Redis主从复制及sentinel监控

Centos7搭建Redis主从复制及sentinel监控

  • 主从复制
  • sentinel监控,实现故障转移failover

主从复制

本文介绍如何搭建Redis一主二从环境,需要先准备3个Redis单实例节点环境。
单实例节点环境搭建请参考:Centos7安装Redis单节点 https://blog.csdn.net/iceliooo/article/details/102892062

IP 角色:端口
192.168.56.105 Master:6379
192.168.56.106 replica:6379
192.168.56.107 replica:6379

修改106及107节点redis.conf配置,添加如下配置信息:

286 # replicaof  
287 replicaof 192.168.56.105 6379

重启105,106, 107 节点Redis服务,查看主从复制状态:
105Master节点:

[root@k8s-master src]# redis-server ../redis.conf
23141:C 04 Nov 2019 14:37:31.847 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23141:C 04 Nov 2019 14:37:31.847 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=23141, just started
23141:C 04 Nov 2019 14:37:31.847 # Configuration loaded
[root@k8s-master src]# redis-cli
127.0.0.1:6379> info replication
NOAUTH Authentication required.
127.0.0.1:6379> auth ******
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:c3bda75e828e43caee07f27dc191f98e661b82c2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

106 replica节点:

[root@k8s-node1 src]# redis-server ../redis.conf
13197:C 04 Nov 2019 14:38:40.254 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
13197:C 04 Nov 2019 14:38:40.254 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=13197, just started
13197:C 04 Nov 2019 14:38:40.254 # Configuration loaded
[root@k8s-node1 src]# redis-cli 
127.0.0.1:6379> info replication
NOAUTH Authentication required.
127.0.0.1:6379> auth ******
OK
127.0.0.1:6379> info repilication
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.56.105
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1572849566
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:c1b5f8a15f72d059bc1d523a12c4a4380391c35d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

发现主从配置失败,主要是因为Master节点配置了访问密码,所以需要在replica节点配置Master节点的访问秘密:

294 # masterauth 
295 masterauth ******

重启Master及replica节点,主从配置成功:

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.56.106,port=6379,state=online,offset=254,lag=1
slave1:ip=192.168.56.107,port=6379,state=online,offset=254,lag=1
master_replid:c4b26a9a991c54e3110d3cf9479d997937532002
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:254
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:254

127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.56.105
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:56
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:c4b26a9a991c54e3110d3cf9479d997937532002
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:56
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:56

Master节点执行命令,replica节点成功接收

127.0.0.1:6379> set iceliooo male
OK
127.0.0.1:6379>

从节点默认为自读

127.0.0.1:6379> get iceliooo
"male"
127.0.0.1:6379> set iceliooo male
(error) READONLY You can't write against a read only replica.
127.0.0.1:6379> 

sentinel监控,实现故障转移failover

主从复制解决了数据备份问题,提升了性能,但Master节点不可用,将出现单点故障,需要手动切换Master节点,费时费力,还会造成服务不可用问题。
下面介绍如何通过sentinel实现Master自动切换。
sentinel监控主从集群中所有Redis服务节点,sentinel本身也会存在单点故障问题,sentinel通过相互监控来解决。sentinel本身没有主从之分。

Redis主从及Sentinel部署分布

IP 角色:端口
192.168.56.105 Master:6379/sentinel:26379
192.168.56.106 replica:6379 /sentinel:26379
192.168.56.107 replica:6379/sentinel:26379

修改sentinel.conf配置文件

[root@k8s-master redis-5.0.5]# cp sentinel.conf sentinel.conf.bak
[root@k8s-master redis-5.0.5]# vi sentinel.conf

主要修改如下配置,其它使用默认值:

daemonize yes
port 16379
protected-mode no
dir "/usr/local/software/redis-5.0.5/sentinel-temp"
sentinel monitor redis-master 192.168.56.105 6379 2

启动3台服务器的redis服务和sentinel服务

[root@k8s-master redis-5.0.5]# cd src/
[root@k8s-master src]# redis-sentinel ../sentinel.conf
2489:X 04 Nov 2019 17:12:28.225 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2489:X 04 Nov 2019 17:12:28.225 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=2489, just started
2489:X 04 Nov 2019 17:12:28.225 # Configuration loaded
[root@k8s-master src]# ps -ef|grep redis
root      2490     1  0 17:12 ?        00:00:00 redis-sentinel *:16379 [sentinel]
root      3064 11611  0 17:13 pts/1    00:00:00 grep --color=auto redis
root     29488     1  0 14:50 ?        00:00:09 redis-server *:6379
[root@k8s-master src]# 

模拟Master故障:

106上查看集群状态:

127.0.0.1:6379> auth centos
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.56.105
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:12465
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:c4b26a9a991c54e3110d3cf9479d997937532002
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:12465
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:12465
127.0.0.1:6379> 

关闭Master节点:

127.0.0.1:6379> SHUTDOWN
not connected> 

再次查看106上集群状态,30秒之后发现master节点切换失败:

127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.56.105
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:12605
master_link_down_since_seconds:35
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:c4b26a9a991c54e3110d3cf9479d997937532002
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:12605
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:12605
127.0.0.1:6379> 

失败原因主要是没有在sentinel.conf配置文件中配置Redis集群节点的访问密码(这里要求集群中每个节点的访问密码必须一致

121 sentinel monitor mymaster 192.168.56.107 6379 2
122 sentinel auth-pass mymaster ******

这里还需要注意,“sentinel auth-pass mymaster ******” 必须配置在“sentinel monitor mymaster 192.168.56.107 6379 2” 之后,否则会出现如下异常:

[root@k8s-master src]# redis-sentinel ../sentinel.conf

*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 104
>>> 'sentinel auth-pass mymaster ******'
No such master with specified name.

重新启动各个节点的redis-sentinel服务及redis服务,106上查看集群状态:

127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.56.105
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:1596
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:09787ba9f25f74e8caac6c07808997c7d3d4a421
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1596
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1596

关闭Master节点,模拟Master节点故障:

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.56.106,port=6379,state=online,offset=6786,lag=0
slave1:ip=192.168.56.107,port=6379,state=online,offset=6786,lag=1
master_replid:09787ba9f25f74e8caac6c07808997c7d3d4a421
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:6786
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:6786
127.0.0.1:6379> SHUTDOWN
not connected> 

30秒后,106上查看集群状态,切换成功,107成为新的Master节点:

127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.56.107
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:11842
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:569347a188791772e9f7ff3fa11048d0e6a86b01
master_replid2:09787ba9f25f74e8caac6c07808997c7d3d4a421
master_repl_offset:11842
second_repl_offset:8517
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:11842

启动105redis节点,模拟原Master节点恢复,新Master中恢复到两个replica节点:

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.56.106,port=6379,state=online,offset=478823,lag=1
slave1:ip=192.168.56.105,port=6379,state=online,offset=478966,lag=0
master_replid:569347a188791772e9f7ff3fa11048d0e6a86b01
master_replid2:09787ba9f25f74e8caac6c07808997c7d3d4a421
master_repl_offset:478966
second_repl_offset:8517
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:478966

你可能感兴趣的:(Linux,Centos7,Redis)