一,规划三台redis的ip:一主二从

redismaster01: 172.18.1.1       主
redisslave01: 172.18.1.2 从
redisslave02: 172.18.1.3 从

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

对应的源码可以访问这里获取: https://github.com/liuhongdi/

说明:作者:刘宏缔 邮箱: [email protected]

说明: centos8上redis5的安装,参考这一篇:

centos8安装redis
地址:https://www.cnblogs.com/architectforest/p/12325230.html

二,对三台redis机器分别进行配置

1,master01上

[root@redismaster01 /]# vi /usr/local/soft/redis5/conf/redis.conf

内容:

bind 172.18.1.1
masterauth 123456
requirepass 123456

重启redis服务:

[root@redismaster01 /]# systemctl stop redis
[root@redismaster01 /]# systemctl start redis

2,slave01上

[root@redisslave01 /]# vi /usr/local/soft/redis5/conf/redis.conf

内容

bind 172.18.1.2
slaveof 172.18.1.1 6379
masterauth 123456
requirepass 123456

重启redis服务

[root@redisslave01 /]# systemctl stop redis
[root@redisslave01 /]# systemctl start redis

3,slave02上

[root@redisslave02 /]# vi /usr/local/soft/redis5/conf/redis.conf

内容

bind 172.18.1.3
slaveof 172.18.1.1 6379
masterauth 123456
requirepass 123456

重启redis服务

[root@redisslave02 /]# systemctl stop redis
[root@redisslave02 /]# systemctl start redis

4,参数说明:

slaveof: 指定主redis的ip 和 port
masterauth: 主redis的密码
requirepass: 本地的密码

建议密码保持一致

三,从redis-cli查看主从状态

1,从master01上

[root@redismaster01 /]# /usr/local/soft/redis5/bin/redis-cli -h 172.18.1.1 -p 6379

查看主从同步:

172.18.1.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.18.1.2,port=6379,state=online,offset=938,lag=0
slave1:ip=172.18.1.3,port=6379,state=online,offset=938,lag=1
master_replid:f2800497a73845ad25276bf74cffce2138a6e216
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:952
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:952

2,从slave01上

[root@redisslave01 /]# /usr/local/soft/redis5/bin/redis-cli -h 172.18.1.2 -p 6379
172.18.1.2:6379> info replication
# Replication
role:slave
master_host:172.18.1.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_repl_offset:182
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:f2800497a73845ad25276bf74cffce2138a6e216
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:182
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:182

四,info replaction的内容详解:

1,主redis上:

role:master                 #实例的角色,此处是master
connected_slaves:2 #连接的slave实例个数,2个
slave0:ip=172.18.1.2,port=6379,state=online,offset=938,lag=0 #lag从库多少秒未向主库发送REPLCONF命令
slave1:ip=172.18.1.3,port=6379,state=online,offset=938,lag=1 #lag从库多少秒未向主库发送REPLCONF命令
master_replid:f2800497a73845ad25276bf74cffce2138a6e216
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:952 #主从同步偏移量,此值如果和上面的offset相同说明主从一致没延迟
second_repl_offset:-1
repl_backlog_active:1 #复制积压缓冲区是否开启
repl_backlog_size:1048576 #复制积压缓冲大小
repl_backlog_first_byte_offset:1 #复制缓冲区里偏移量的大小
repl_backlog_histlen:952

2,从redis上

# Replication
role:slave #实例的角色,是slave
master_host:172.18.1.1 #此节点对应的master的ip
master_port:6379 #此节点对应的master的port
master_link_status:up #slave端可查看它与master之间同步状态,当复制断开后表示down,连接时是up
master_last_io_seconds_ago:7 #主库多少秒未发送数据到从库?
master_sync_in_progress:0 #从服务器是否在与主服务器进行同步
slave_repl_offset:4855 #slave复制偏移量
slave_priority:100 #slave优先级
slave_read_only:1 #从库是否设置只读
connected_slaves:0 #连接的slave实例个数
master_replid:f2800497a73845ad25276bf74cffce2138a6e216
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:4855 #主从同步偏移量
second_repl_offset:-1
repl_backlog_active:1 #复制积压缓冲区是否开启
repl_backlog_size:1048576 #复制积压缓冲大小
repl_backlog_first_byte_offset:1 #复制缓冲区里偏移量的大小
repl_backlog_histlen:4855

说明:在从redis上查看同步时,

1,如果master_link_status是up,则表示到主redis的连接正常,否则先排查连接的问题
2,如果 slave_repl_offset 和 master_repl_offset 一致,表示同步是一致的

五,测试redis的主从是否生效

1,master01上

172.18.1.1:6379> set a 123
OK
172.18.1.1:6379> get a
"123"

2,slave01上

172.18.1.2:6379> get a
"123"

3,slave02上

172.18.1.3:6379> get a
"123"

六,规划 :三个哨兵服务器

官方建议至少三个且奇数个的 Sentinel 节点

172.18.1.4    哨兵1
172.18.1.5 哨兵2
172.18.1.6 哨兵3

七,每台机器上哨兵的安装:

在redis源码中编译完成后,要把redis-sentinel 复制到安装目录的bin目录下

把sentinel.conf配置文件也复制到安装目录的conf目录下

[root@redissentinel01 src]# cp /usr/local/source/redis-5.0.7/src/redis-sentinel /usr/local/soft/redis5/bin/
[root@redissentinel01 src]# cp /usr/local/source/redis-5.0.7/sentinel.conf /usr/local/soft/redis5/conf/

八,每台机器上哨兵的配置:

1,创建用到的临时工作目录

[root@redissentinel01 src]# cd /data/redis6379/
[root@redissentinel01 redis6379]# mkdir tmp

2,编辑哨兵的配置

[root@redissentinel01 src]# vi /usr/local/soft/redis5/conf/sentinel.conf

内容:实际操作时把括号和说明文字要去掉

port 26379    (默认,无需改动)
bind 0.0.0.0 (添加)
daemonize yes (默认是no,改成yes)
protected-mode no (默认,取消注释)
logfile "/data/redis6379/log/redis.log" (修改配置)
dir /data/redis6379/tmp (修改配置)
sentinel myid 372e722a7630342374ce6d8e0fee5f7a86e647e4
(此值每台机器上要区分开,可以只修改一个数字,
否则用info sentinel查看时sentinels的数量不正确
或者把这一行注释掉应该也可以)
sentinel monitor mymaster 172.18.1.1 6379 2 (修改配置)
sentinel auth-pass mymaster 123456 (修改配置)

3,配置的说明:

monitor  指定主redis的ip和port,最后的2是指:把这个主实例判断为失效至少需要2个Sentinel进程的同意
auth-pass 是指定主redis的密码

九,systemd管理redis-sentinel

1,编辑一个service文件

[root@redissentinel01 log]# vi /lib/systemd/system/redis-sentinel.service

内容:

[Unit]
Description=Redis
After=network.target [Service]
Type=forking
PIDFile=/var/run/redis-sentinel.pid
ExecStart=/usr/local/soft/redis5/bin/redis-sentinel /usr/local/soft/redis5/conf/sentinel.conf --sentinel
ExecStop=/bin/kill -s TERM $MAINPID [Install]
WantedBy=multi-user.target

2,测试启动redis-sentinel

[root@redissentinel01 log]# systemctl daemon-reload
[root@redissentinel01 log]# systemctl start redis-sentinel
[root@redissentinel01 log]# ps auxfww | grep redis-sentinel | grep -v grep
root 390 0.1 0.0 41076 3292 ? Ssl 05:39 0:00 /usr/local/soft/redis5/bin/redis-sentinel 0.0.0.0:26379 [sentinel]

十,sentinel常用命令:

查看mymaster下哪些sentinel

172.18.1.6:26379> sentinel sentinels mymaster

显示主节点列表及其状态

172.18.1.6:26379> sentinel masters

显示mymaster下有几个slave

172.18.1.6:26379> sentinel slaves mymaster

只查看mymaster这个主节点的信息

172.18.1.6:26379> sentinel master mymaster

查看sentinel信息的一个汇总

172.18.1.6:26379> info sentinel

十一,测试当master宕机时,哨兵能否起到切换master的作用

1,先从哨兵服务器上查看master信息

172.18.1.5:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=172.18.1.1:6379,slaves=3,sentinels=3

2,在master中杀死redis进程

[root@redismaster01 conf]# kill 1062
[root@redismaster01 conf]# kill 1062
bash: kill: (1062) - No such process

3,再次到哨兵服务器上查看:

172.18.1.5:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=172.18.1.2:6379,slaves=3,sentinels=3

可以看到:172.18.1.1上的redis进程被杀死后,172.18.1.2已经被选为master

4,登录到新选举出的master上查看

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.18.1.3,port=6379,state=online,offset=229680,lag=1
master_replid:3977c9f0425ab035dee705874b5b2f7cd8c1bcb4
master_replid2:41d7c9660b1195a58fc273319078a6f4551436b0
master_repl_offset:229950
second_repl_offset:193766
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:229950

说明:当前redis的role已经变成了master

5,回到旧的master上,再次启动

[root@redismaster01 conf]# systemctl start redis

用info replication查看时,角色已经变成slave

127.0.0.1:6379> info replication
# Replication
role:slave
master_host:172.18.1.2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:299988
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:3977c9f0425ab035dee705874b5b2f7cd8c1bcb4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:299988
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:267193
repl_backlog_histlen:32796

十二,查看centos版本

[root@localhost liuhongdi]# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)

05-23 03:57