本文最后更新于 257 天前,其中的信息可能已经过时,如有错误请发送邮件到wuxianglongblog@163.com
Redis的高可用架构多哨兵(sentinel)模式部署实战
一.环境准备,依旧基于单节点多实例的的基础环境进行配置
1.配置文件
[root@redis201.oldboyedu.com ~]# cat /oldboyedu/softwares/redis16370/redis.conf
port 16370
daemonize yes
pidfile "/oldboyedu/data/redis16370/redis.pid"
loglevel notice
logfile "/oldboyedu/data/redis16370/redis.log"
dbfilename "dump.rdb"
dir "/oldboyedu/data/redis16370"
requirepass "oldboyedu"
masterauth "oldboyedu"
bind 172.200.1.201 127.0.0.1
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes
appendfsync everysec
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# cat /oldboyedu/softwares/redis16371/redis.conf
port 16371
daemonize yes
pidfile "/oldboyedu/data/redis16371/redis.pid"
loglevel notice
logfile "/oldboyedu/data/redis16371/redis.log"
dbfilename "dump.rdb"
dir "/oldboyedu/data/redis16371"
bind 172.200.1.201 127.0.0.1
requirepass "oldboyedu"
masterauth "oldboyedu"
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes
appendfsync everysec
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# cat /oldboyedu/softwares/redis16372/redis.conf
port 16372
daemonize yes
pidfile "/oldboyedu/data/redis16372/redis.pid"
loglevel notice
logfile "/oldboyedu/data/redis16372/redis.log"
dbfilename "dump.rdb"
dir "/oldboyedu/data/redis16372"
bind 172.200.1.201 127.0.0.1
requirepass "oldboyedu"
masterauth "oldboyedu"
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes
appendfsync everysec
[root@redis201.oldboyedu.com ~]#
2.启动服务
[root@redis201.oldboyedu.com ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:16370 *:*
LISTEN 0 128 172.200.1.201:16370 *:*
LISTEN 0 128 127.0.0.1:16371 *:*
LISTEN 0 128 172.200.1.201:16371 *:*
LISTEN 0 128 127.0.0.1:16372 *:*
LISTEN 0 128 172.200.1.201:16372 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@redis201.oldboyedu.com ~]#
3.查看主从状态(注意哈,16372实例是master哟~因此在下面编写"sentinel"的配置文件是要注意!)
[root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION
# Replication
role:slave
master_host:172.200.1.201
master_port:16372
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:181851
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:129559
repl_backlog_histlen:30483
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16371 -a oldboyedu INFO REPLICATION
# Replication
role:slave
master_host:172.200.1.201
master_port:16372
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:181851
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu INFO REPLICATION
# Replication
role:master
connected_slaves:2
slave0:ip=172.200.1.201,port=16371,state=online,offset=181865,lag=0
slave1:ip=172.200.1.201,port=16370,state=online,offset=181865,lag=0
master_repl_offset:181865
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:159304
repl_backlog_histlen:22562
[root@redis201.oldboyedu.com ~]#
二.创建多sentinel实例使用的配置文件,数据,日志目录
[root@redis201.oldboyedu.com ~]# mkdir -pv /oldboyedu/softwares/redis2637{0..2}
mkdir: 已创建目录 "/oldboyedu/softwares/redis26370"
mkdir: 已创建目录 "/oldboyedu/softwares/redis26371"
mkdir: 已创建目录 "/oldboyedu/softwares/redis26372"
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# mkdir -pv /oldboyedu/data/redis2637{0..2}
mkdir: 已创建目录 "/oldboyedu/data/redis26370"
mkdir: 已创建目录 "/oldboyedu/data/redis26371"
mkdir: 已创建目录 "/oldboyedu/data/redis26372"
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# mkdir -pv /oldboyedu/logs/redis2637{0..2}
mkdir: 已创建目录 "/oldboyedu/logs/redis26370"
mkdir: 已创建目录 "/oldboyedu/logs/redis26371"
mkdir: 已创建目录 "/oldboyedu/logs/redis26372"
[root@redis201.oldboyedu.com ~]#
三.创建配置文件
1.编辑"sentinel26370"实例的配置文件
[root@redis201.oldboyedu.com ~]# cat > /oldboyedu/softwares/redis26370/sentinel.conf <<EOF
> bind 172.200.1.201 127.0.0.1
> port 26370
> dir /oldboyedu/data/redis26370
> sentinel monitor mymaster 172.200.1.201 16372 2
> sentinel down-after-milliseconds mymaster 5000
> sentinel auth-pass mymaster oldboyedu
> EOF
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# cat /oldboyedu/softwares/redis26370/sentinel.conf
bind 172.200.1.201 127.0.0.1
port 26370
dir /oldboyedu/data/redis26370
sentinel monitor mymaster 172.200.1.201 16372 2
sentinel down-after-milliseconds mymaster 5000
sentinel auth-pass mymaster oldboyedu
[root@redis201.oldboyedu.com ~]#
温馨提示:
注意"sentinel monitor mymaster 172.200.1.201 16372 2"中的2,表示最少需要2个sentinel实例"投票"决定master库是否已下线,仅有1票是无法进行自动主从切换的!因此3个sentinel实例只能允许任意1个sentinel实例挂掉!
2.编辑"sentinel26371"实例的配置文件
[root@redis201.oldboyedu.com ~]# cat > /oldboyedu/softwares/redis26371/sentinel.conf <<EOF
> bind 172.200.1.201 127.0.0.1
> port 26371
> dir /oldboyedu/data/redis26371
> sentinel monitor mymaster 172.200.1.201 16372 2
> sentinel down-after-milliseconds mymaster 5000
> sentinel auth-pass mymaster oldboyedu
> EOF
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# cat /oldboyedu/softwares/redis26371/sentinel.conf
bind 172.200.1.201 127.0.0.1
port 26371
dir /oldboyedu/data/redis26371
sentinel monitor mymaster 172.200.1.201 16372 2
sentinel down-after-milliseconds mymaster 5000
sentinel auth-pass mymaster oldboyedu
[root@redis201.oldboyedu.com ~]#
3.编辑"sentinel26372"实例的配置文件
[root@redis201.oldboyedu.com ~]# cat > /oldboyedu/softwares/redis26372/sentinel.conf <<EOF
> bind 172.200.1.201 127.0.0.1
> port 26372
> dir /oldboyedu/data/redis26372
> sentinel monitor mymaster 172.200.1.201 16372 2
> sentinel down-after-milliseconds mymaster 5000
> sentinel auth-pass mymaster oldboyedu
> EOF
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# cat /oldboyedu/softwares/redis26372/sentinel.conf
bind 172.200.1.201 127.0.0.1
port 26372
dir /oldboyedu/data/redis26372
sentinel monitor mymaster 172.200.1.201 16372 2
sentinel down-after-milliseconds mymaster 5000
sentinel auth-pass mymaster oldboyedu
[root@redis201.oldboyedu.com ~]#
四.启动sentinel进程并查看日志
1.启动"redis26370"实例进程
(1)启动"redis26370"实例进程
[root@redis201.oldboyedu.com ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:16370 *:*
LISTEN 0 128 172.200.1.201:16370 *:*
LISTEN 0 128 127.0.0.1:16371 *:*
LISTEN 0 128 172.200.1.201:16371 *:*
LISTEN 0 128 127.0.0.1:16372 *:*
LISTEN 0 128 172.200.1.201:16372 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-sentinel /oldboyedu/softwares/redis26370/sentinel.conf &> /oldboyedu/logs/redis26370/sentinel.log &
[1] 31924
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:16370 *:*
LISTEN 0 128 172.200.1.201:16370 *:*
LISTEN 0 128 127.0.0.1:16371 *:*
LISTEN 0 128 172.200.1.201:16371 *:*
LISTEN 0 128 127.0.0.1:16372 *:*
LISTEN 0 128 172.200.1.201:16372 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:26370 *:*
LISTEN 0 128 172.200.1.201:26370 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@redis201.oldboyedu.com ~]#
(2)查看日志
[root@redis201.oldboyedu.com ~]# tail -100f /oldboyedu/logs/redis26370/sentinel.log
31924:X 04 Mar 08:57:42.537 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.13 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26370
| `-._ `._ / _.-' | PID: 31924
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
31924:X 04 Mar 08:57:42.537 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
31924:X 04 Mar 08:57:42.537 # Sentinel ID is 00f3a2c0c4329119d2062dda209a9da906f74892
31924:X 04 Mar 08:57:42.538 # +monitor master mymaster 172.200.1.201 16372 quorum 2
31924:X 04 Mar 08:57:42.538 * +slave slave 172.200.1.201:16371 172.200.1.201 16371 @ mymaster 172.200.1.201 16372
31924:X 04 Mar 08:57:42.539 * +slave slave 172.200.1.201:16370 172.200.1.201 16370 @ mymaster 172.200.1.201 16372
31924:X 04 Mar 08:57:47.541 # +sdown sentinel 4b01fbf124e407d90ca0d0b615e9c558f79cc73b 172.200.1.201 26372 @ mymaster 172.200.1.201 16372
31924:X 04 Mar 08:57:47.541 # +sdown sentinel 9cb490bf09c47042b61944fc3322c31d6e63c247 172.200.1.201 26371 @ mymaster 172.200.1.201 16372
2.启动"redis26371"实例进程
(1)启动"redis26370"实例进程
[root@redis201.oldboyedu.com ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:16370 *:*
LISTEN 0 128 172.200.1.201:16370 *:*
LISTEN 0 128 127.0.0.1:16371 *:*
LISTEN 0 128 172.200.1.201:16371 *:*
LISTEN 0 128 127.0.0.1:16372 *:*
LISTEN 0 128 172.200.1.201:16372 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:26370 *:*
LISTEN 0 128 172.200.1.201:26370 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-sentinel /oldboyedu/softwares/redis26371/sentinel.conf &> /oldboyedu/logs/redis26371/sentinel.log &
[2] 32557
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:16370 *:*
LISTEN 0 128 172.200.1.201:16370 *:*
LISTEN 0 128 127.0.0.1:16371 *:*
LISTEN 0 128 172.200.1.201:16371 *:*
LISTEN 0 128 127.0.0.1:16372 *:*
LISTEN 0 128 172.200.1.201:16372 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:26370 *:*
LISTEN 0 128 172.200.1.201:26370 *:*
LISTEN 0 128 127.0.0.1:26371 *:*
LISTEN 0 128 172.200.1.201:26371 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@redis201.oldboyedu.com ~]#
(2)查看日志
[root@redis201.oldboyedu.com ~]# tail -100f /oldboyedu/logs/redis26371/sentinel.log
32557:X 04 Mar 08:59:24.144 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.13 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26371
| `-._ `._ / _.-' | PID: 32557
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
32557:X 04 Mar 08:59:24.144 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
32557:X 04 Mar 08:59:24.144 # Sentinel ID is 9cb490bf09c47042b61944fc3322c31d6e63c247
32557:X 04 Mar 08:59:24.144 # +monitor master mymaster 172.200.1.201 16372 quorum 2
32557:X 04 Mar 08:59:24.146 * +slave slave 172.200.1.201:16371 172.200.1.201 16371 @ mymaster 172.200.1.201 16372
32557:X 04 Mar 08:59:24.147 * +slave slave 172.200.1.201:16370 172.200.1.201 16370 @ mymaster 172.200.1.201 16372
32557:X 04 Mar 08:59:29.150 # +sdown sentinel 4b01fbf124e407d90ca0d0b615e9c558f79cc73b 172.200.1.201 26372 @ mymaster 172.200.1.201 16372
3.启动"redis26372"实例进程
(1)启动"redis26370"实例进程
[root@redis201.oldboyedu.com ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:16370 *:*
LISTEN 0 128 172.200.1.201:16370 *:*
LISTEN 0 128 127.0.0.1:16371 *:*
LISTEN 0 128 172.200.1.201:16371 *:*
LISTEN 0 128 127.0.0.1:16372 *:*
LISTEN 0 128 172.200.1.201:16372 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:26370 *:*
LISTEN 0 128 172.200.1.201:26370 *:*
LISTEN 0 128 127.0.0.1:26371 *:*
LISTEN 0 128 172.200.1.201:26371 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-sentinel /oldboyedu/softwares/redis26372/sentinel.conf &> /oldboyedu/logs/redis26372/sentinel.log &
[3] 32757
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:16370 *:*
LISTEN 0 128 172.200.1.201:16370 *:*
LISTEN 0 128 127.0.0.1:16371 *:*
LISTEN 0 128 172.200.1.201:16371 *:*
LISTEN 0 128 127.0.0.1:16372 *:*
LISTEN 0 128 172.200.1.201:16372 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:26370 *:*
LISTEN 0 128 172.200.1.201:26370 *:*
LISTEN 0 128 127.0.0.1:26371 *:*
LISTEN 0 128 172.200.1.201:26371 *:*
LISTEN 0 128 127.0.0.1:26372 *:*
LISTEN 0 128 172.200.1.201:26372 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@redis201.oldboyedu.com ~]#
(2)查看日志
[root@redis201.oldboyedu.com ~]# tail -100f /oldboyedu/logs/redis26372/sentinel.log
32757:X 04 Mar 09:00:34.562 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.13 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26372
| `-._ `._ / _.-' | PID: 32757
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
32757:X 04 Mar 09:00:34.562 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
32757:X 04 Mar 09:00:34.562 # Sentinel ID is 4b01fbf124e407d90ca0d0b615e9c558f79cc73b
32757:X 04 Mar 09:00:34.562 # +monitor master mymaster 172.200.1.201 16372 quorum 2
32757:X 04 Mar 09:00:34.564 * +slave slave 172.200.1.201:16371 172.200.1.201 16371 @ mymaster 172.200.1.201 16372
32757:X 04 Mar 09:00:34.566 * +slave slave 172.200.1.201:16370 172.200.1.201 16370 @ mymaster 172.200.1.201 16372
五.验证sentinel能够自动切换
1.手动停止主库运行,模拟主库宕机
[root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu INFO REPLICATION
# Replication
role:master
connected_slaves:2
slave0:ip=172.200.1.201,port=16371,state=online,offset=181949,lag=1
slave1:ip=172.200.1.201,port=16370,state=online,offset=181949,lag=0
master_repl_offset:181949
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:159304
repl_backlog_histlen:22646
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu SHUTDOWN
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu INFO REPLICATION
Could not connect to Redis at 127.0.0.1:16372: Connection refused
[root@redis201.oldboyedu.com ~]#
2.当16372实例对应的主库宕机后,会自动有其他salve来接管
[root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION
# Replication
role:master
connected_slaves:1
slave0:ip=172.200.1.201,port=16371,state=online,offset=7024,lag=1
master_repl_offset:7166
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:7165
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16371 -a oldboyedu INFO REPLICATION
# Replication
role:slave
master_host:172.200.1.201
master_port:16370
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:9310
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu SHUTDOWN
Could not connect to Redis at 127.0.0.1:16372: Connection refused
[root@redis201.oldboyedu.com ~]#
3.恢复16372实例,会自动加入集群并成为slave角色
[root@redis201.oldboyedu.com ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:16370 *:*
LISTEN 0 128 172.200.1.201:16370 *:*
LISTEN 0 128 127.0.0.1:16371 *:*
LISTEN 0 128 172.200.1.201:16371 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:26370 *:*
LISTEN 0 128 172.200.1.201:26370 *:*
LISTEN 0 128 127.0.0.1:26371 *:*
LISTEN 0 128 172.200.1.201:26371 *:*
LISTEN 0 128 127.0.0.1:26372 *:*
LISTEN 0 128 172.200.1.201:26372 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-server /oldboyedu/softwares/redis16372/redis.conf
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:16370 *:*
LISTEN 0 128 172.200.1.201:16370 *:*
LISTEN 0 128 127.0.0.1:16371 *:*
LISTEN 0 128 172.200.1.201:16371 *:*
LISTEN 0 128 127.0.0.1:16372 *:*
LISTEN 0 128 172.200.1.201:16372 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:26370 *:*
LISTEN 0 128 172.200.1.201:26370 *:*
LISTEN 0 128 127.0.0.1:26371 *:*
LISTEN 0 128 172.200.1.201:26371 *:*
LISTEN 0 128 127.0.0.1:26372 *:*
LISTEN 0 128 172.200.1.201:26372 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu INFO REPLICATION
# Replication
role:slave
master_host:172.200.1.201
master_port:16370
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:97984
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
4.再次查看各个Redis实例状态
[root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION
# Replication
role:master
connected_slaves:2
slave0:ip=172.200.1.201,port=16371,state=online,offset=107142,lag=0
slave1:ip=172.200.1.201,port=16372,state=online,offset=107142,lag=0
master_repl_offset:107142
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:107141
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16371 -a oldboyedu INFO REPLICATION
# Replication
role:slave
master_host:172.200.1.201
master_port:16370
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:107994
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu INFO REPLICATION
# Replication
role:slave
master_host:172.200.1.201
master_port:16370
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:108420
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
六.手动停掉任意一个sentinel实例,再次验证主从结构能否正常使用
1.将sentinel 26370实例停用
[root@redis201.oldboyedu.com ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:16370 *:*
LISTEN 0 128 172.200.1.201:16370 *:*
LISTEN 0 128 127.0.0.1:16371 *:*
LISTEN 0 128 172.200.1.201:16371 *:*
LISTEN 0 128 127.0.0.1:16372 *:*
LISTEN 0 128 172.200.1.201:16372 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:26370 *:*
LISTEN 0 128 172.200.1.201:26370 *:*
LISTEN 0 128 127.0.0.1:26371 *:*
LISTEN 0 128 172.200.1.201:26371 *:*
LISTEN 0 128 127.0.0.1:26372 *:*
LISTEN 0 128 172.200.1.201:26372 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# netstat -untalp | grep 26370 | grep LISTEN
tcp 0 0 127.0.0.1:26370 0.0.0.0:* LISTEN 31924/redis-sentine
tcp 0 0 172.200.1.201:26370 0.0.0.0:* LISTEN 31924/redis-sentine
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# kill -9 31924
[1] 已杀死 redis-sentinel /oldboyedu/softwares/redis26370/sentinel.conf &>/oldboyedu/logs/redis26370/sentinel.log
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:16370 *:*
LISTEN 0 128 172.200.1.201:16370 *:*
LISTEN 0 128 127.0.0.1:16371 *:*
LISTEN 0 128 172.200.1.201:16371 *:*
LISTEN 0 128 127.0.0.1:16372 *:*
LISTEN 0 128 172.200.1.201:16372 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:26371 *:*
LISTEN 0 128 172.200.1.201:26371 *:*
LISTEN 0 128 127.0.0.1:26372 *:*
LISTEN 0 128 172.200.1.201:26372 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@redis201.oldboyedu.com ~]#
2.发现依旧是可以正常切换主从的
[root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16371 -a oldboyedu INFO REPLICATION
# Replication
role:slave
master_host:172.200.1.201
master_port:16370
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:152478
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu INFO REPLICATION
# Replication
role:slave
master_host:172.200.1.201
master_port:16370
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:153046
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu SHUTDOWN
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION
Could not connect to Redis at 127.0.0.1:16370: Connection refused
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16371 -a oldboyedu INFO REPLICATION
# Replication
role:slave
master_host:172.200.1.201
master_port:16370
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:154480
master_link_down_since_seconds:5
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu INFO REPLICATION
# Replication
role:master
connected_slaves:1
slave0:ip=172.200.1.201,port=16371,state=online,offset=450,lag=0
master_repl_offset:450
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:449
[root@redis201.oldboyedu.com ~]#
3.记得恢复3个Redis实例哟~
[root@redis201.oldboyedu.com ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:16371 *:*
LISTEN 0 128 172.200.1.201:16371 *:*
LISTEN 0 128 127.0.0.1:16372 *:*
LISTEN 0 128 172.200.1.201:16372 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:26371 *:*
LISTEN 0 128 172.200.1.201:26371 *:*
LISTEN 0 128 127.0.0.1:26372 *:*
LISTEN 0 128 172.200.1.201:26372 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-server /oldboyedu/softwares/redis16370/redis.conf
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:16370 *:*
LISTEN 0 128 172.200.1.201:16370 *:*
LISTEN 0 128 127.0.0.1:16371 *:*
LISTEN 0 128 172.200.1.201:16371 *:*
LISTEN 0 128 127.0.0.1:16372 *:*
LISTEN 0 128 172.200.1.201:16372 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:26371 *:*
LISTEN 0 128 172.200.1.201:26371 *:*
LISTEN 0 128 127.0.0.1:26372 *:*
LISTEN 0 128 172.200.1.201:26372 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION # 仔细看这里,发现我们刚刚启动时(我估计是在2秒内),我这里并没有将其切换为slave状态,需要稍等一会~
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16371 -a oldboyedu INFO REPLICATION
# Replication
role:slave
master_host:172.200.1.201
master_port:16372
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:11661
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu INFO REPLICATION
# Replication
role:master
connected_slaves:2
slave0:ip=172.200.1.201,port=16371,state=online,offset=12229,lag=1
slave1:ip=172.200.1.201,port=16370,state=online,offset=12229,lag=0
master_repl_offset:12229
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:12228
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION # Duang~此时该节点重新被sentinel接管,又被论为slave节点。
# Replication
role:slave
master_host:172.200.1.201
master_port:16372
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:13961
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
六.手动停掉任意2个sentinel实例,发现不能正常切换主从啦~因为目前只有一个sentinel存活,尽管它能进行投票也只能投递1票
1.将sentinel 26372实例停用
[root@redis201.oldboyedu.com ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:16370 *:*
LISTEN 0 128 172.200.1.201:16370 *:*
LISTEN 0 128 127.0.0.1:16371 *:*
LISTEN 0 128 172.200.1.201:16371 *:*
LISTEN 0 128 127.0.0.1:16372 *:*
LISTEN 0 128 172.200.1.201:16372 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:26371 *:*
LISTEN 0 128 172.200.1.201:26371 *:*
LISTEN 0 128 127.0.0.1:26372 *:*
LISTEN 0 128 172.200.1.201:26372 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# netstat -untalp | grep 26372 | grep LISTEN
tcp 0 0 127.0.0.1:26372 0.0.0.0:* LISTEN 32757/redis-sentine
tcp 0 0 172.200.1.201:26372 0.0.0.0:* LISTEN 32757/redis-sentine
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# kill -9 32757
[3]+ 已杀死 redis-sentinel /oldboyedu/softwares/redis26372/sentinel.conf &>/oldboyedu/logs/redis26372/sentinel.log
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:16370 *:*
LISTEN 0 128 172.200.1.201:16370 *:*
LISTEN 0 128 127.0.0.1:16371 *:*
LISTEN 0 128 172.200.1.201:16371 *:*
LISTEN 0 128 127.0.0.1:16372 *:*
LISTEN 0 128 172.200.1.201:16372 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:26371 *:*
LISTEN 0 128 172.200.1.201:26371 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]#
2.发现不能正常切换主从啦~因为目前只有一个sentinel存活,尽管它能进行投票也只能投递1票,无法我们在配置文件中定义的"sentinel monitor mymaster 172.200.1.201 16372 2"中的"2"!
[root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION
# Replication
role:slave
master_host:172.200.1.201
master_port:16372
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:48575
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16371 -a oldboyedu INFO REPLICATION
# Replication
role:slave
master_host:172.200.1.201
master_port:16372
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:48731
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu INFO REPLICATION
# Replication
role:master
connected_slaves:2
slave0:ip=172.200.1.201,port=16371,state=online,offset=49015,lag=0
slave1:ip=172.200.1.201,port=16370,state=online,offset=48873,lag=1
master_repl_offset:49015
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:49014
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu SHUTDOWN
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION
# Replication
role:slave
master_host:172.200.1.201
master_port:16372
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:49597
master_link_down_since_seconds:3
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16370 -a oldboyedu INFO REPLICATION
# Replication
role:slave
master_host:172.200.1.201
master_port:16372
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:49597
master_link_down_since_seconds:6
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16371 -a oldboyedu INFO REPLICATION
# Replication
role:slave
master_host:172.200.1.201
master_port:16372
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:49597
master_link_down_since_seconds:10
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@redis201.oldboyedu.com ~]#
[root@redis201.oldboyedu.com ~]# redis-cli -p 16372 -a oldboyedu INFO REPLICATION
Could not connect to Redis at 127.0.0.1:16372: Connection refused
[root@redis201.oldboyedu.com ~]#
七.小彩蛋
replicaof 10.0.0.108 8108 # 推荐使用
slaveof 10.0.0.108 8108 # 早期版本支持,目前redis 6.2.5版本依旧是支持的,说不定在将来的版本会被移除
温馨提示:
如上所示,我们可以直接通过修改redis的配置文件,直接指定初始化的reids主库信息!