文章目录
- 一、概念架构简述
- 1. Redis Sentinel简述
- 2. Redis Sentinel优点
- 3. Redis Sentinel缺点
- 二、redis 3节点
- 2.1. 101节点配置
- 2.2. 102节点配置
- 2.3. 103节点配置
- 三、哨兵搭建实现
- 3.1. 101节点配置
- 3.2. 102节点配置
- 3.3. 103节点配置
- 3.4. 启动哨兵
- 3.5. sentinel 监控
- 3.6. 哨兵验证
一、概念架构简述
1. Redis Sentinel简述
Redis Sentinel是分布式系统中监控Redis主从服务器,并从主服务器下显示自动故障转移的模式。
其中4个特性为监控、提醒、自动故障转移、配置提供者。
2. Redis Sentinel优点
- 哨兵 是基于主从模式的,所有主从的优点,哨兵模式都有。
- 主从可以自动切换,系统更健壮,可用性高。
- Sentinel会不断的检查你的主服务器是否运作正常。当被监控的某一个Redis 出现问题是,Sentinel可以通过API向管理员或者其他应用程序发送通知。
3. Redis Sentinel缺点
- 主从切换需要时间,丢失数据
- 还是没有解决主节点写的压力
- 主节点的写能力,存储能力受到单机的限制;动态扩展困难复杂,对于集群,容量- 达到上限时再现扩容会变得很复杂。
二、redis 3节点
节点分布
服务器 | 端口 |
---|---|
192.168.45.101 | 6379 |
192.168.45.101 | 26379 |
192.168.45.102 | 6379 |
192.168.45.102 | 26379 |
192.168.45.103 | 6379 |
192.168.45.103 | 26379 |
2.1. 101节点配置
- 创建目录
mkdir /usr/local/redis/conf/ -p
mkdir /usr/local/redis/log/ -p
mkdir /usr/local/redis/data/ -p
- 配置
vim /usr/local/redis/conf/redis-6379.conf
# 放行访问IP的限制 多个用空格隔开
# 0.0.0.0 代表无限制
bind 0.0.0.0# 后台启动开启
daemonize yes# 日志存储目录以及日志文件名
logfile "/usr/local/redis/log/redis.log"# rdb数据文件名
dbfilename "dump.rdb"# aof模式开启
appendonly no# aof数据文件名
appendfilename "appendonly.aof"#rdb数据文件和aof数据文件存储目录
dir "/usr/local/redis/data"# 设置密码
requirepass "123456"# 从节点访问主节点的密码(必须和requirepass 一致)
masterauth "123456"#从节点只读模式
replica-read-only yes#关闭保护模式,可以外部访问
protected-mode no# 端口
port 6379
- 启动redis
/app/redis-6.2.6/src/redis-server /usr/local/redis/conf/redis-6379.conf
2.2. 102节点配置
创建目录
mkdir /usr/local/redis/conf/ -p
mkdir /usr/local/redis/log/ -p
mkdir /usr/local/redis/data/ -p
- 配置
vim /usr/local/redis/conf/redis-6379.conf
# 放行访问IP的限制 多个用空格隔开
# 0.0.0.0 代表无限制
bind 0.0.0.0# 后台启动开启
daemonize yes# 日志存储目录以及日志文件名
logfile "/usr/local/redis/log/redis.log"# rdb数据文件名
dbfilename "dump.rdb"# aof模式开启
appendonly no# aof数据文件名
appendfilename "appendonly.aof"#rdb数据文件和aof数据文件存储目录
dir "/usr/local/redis/data"# 设置密码
requirepass "123456"# 从节点访问主节点的密码(必须和requirepass 一致)
masterauth "123456"#从节点只读模式
replica-read-only yes#关闭保护模式,可以外部访问
protected-mode no# 端口
port 6379
- 启动redis
/app/redis-6.2.6/src/redis-server /usr/local/redis/conf/redis-6379.conf
2.3. 103节点配置
- 创建目录
mkdir /usr/local/redis/conf/ -p
mkdir /usr/local/redis/log/ -p
mkdir /usr/local/redis/data/ -p
- 配置
vim /usr/local/redis/conf/redis-6379.conf
# 放行访问IP的限制 多个用空格隔开
# 0.0.0.0 代表无限制
bind 0.0.0.0# 后台启动开启
daemonize yes# 日志存储目录以及日志文件名
logfile "/usr/local/redis/log/redis.log"# rdb数据文件名
dbfilename "dump.rdb"# aof模式开启
appendonly no# aof数据文件名
appendfilename "appendonly.aof"#rdb数据文件和aof数据文件存储目录
dir "/usr/local/redis/data"# 设置密码
requirepass "123456"# 从节点访问主节点的密码(必须和requirepass 一致)
masterauth "123456"#从节点只读模式
replica-read-only yes#关闭保护模式,可以外部访问
protected-mode no# 端口
port 6379
- 启动redis
/app/redis-6.2.6/src/redis-server /usr/local/redis/conf/redis-6379.conf
三、哨兵搭建实现
3.1. 101节点配置
- 创建目录
mkdir /usr/local/redis/conf/ -p
mkdir /usr/local/redis/log/ -p
- 配置
vim /usr/local/redis/conf/sentiel.conf
# 放行所有ip限制
bind 0.0.0.0# 进程端口号
port 26379# 后台启动
daemonize yes# 日志记录文件
logfile "/usr/local/redis/log/sentinel.log"# 进程编号记录文件
pidfile "/var/run/redis-sentinel.pid"# 提示Sentinel 去监视一个名为 mymaster 的主服务器 仲裁 =环境3/2+1
sentinel monitor mymaster 192.168.45.101 6379 2# 访问主节点的密码
sentinel auth-pass mymaster 123456# Sentinel 认为服务器已经断线所需要的毫秒数 默认30秒 这里演示10秒
sentinel down-after-milliseconds mymaster 10000# 若Sentinel 在改配置值内未能完成 failover(故障转移) 操作,则认为本次 failover 失败180秒 3分钟
sentinel failover-timeout mymaster 180000
3.2. 102节点配置
- 创建目录
mkdir /usr/local/redis/conf/ -p
mkdir /usr/local/redis/log/ -p
- 配置
vim /usr/local/redis/conf/sentiel.conf
# 放行所有ip限制
bind 0.0.0.0# 进程端口号
port 26379# 后台启动
daemonize yes# 日志记录文件
logfile "/usr/local/redis/log/sentinel.log"# 进程编号记录文件
pidfile "/var/run/redis-sentinel.pid"# 提示Sentinel 去监视一个名为 mymaster 的主服务器 仲裁 =环境3/2+1
sentinel monitor mymaster 192.168.45.101 6379 2# 访问主节点的密码
sentinel auth-pass mymaster 123456# Sentinel 认为服务器已经断线所需要的毫秒数 默认30秒 这里演示10秒
sentinel down-after-milliseconds mymaster 10000# 若Sentinel 在改配置值内未能完成 failover(故障转移) 操作,则认为本次 failover 失败180秒 3分钟
sentinel failover-timeout mymaster 180000
3.3. 103节点配置
- 创建目录
mkdir /usr/local/redis/conf/ -p
mkdir /usr/local/redis/log/ -p
- 配置
vim /usr/local/redis/conf/sentiel.conf
# 放行所有ip限制
bind 0.0.0.0# 进程端口号
port 26379# 后台启动
daemonize yes# 日志记录文件
logfile "/usr/local/redis/log/sentinel.log"# 进程编号记录文件
pidfile "/var/run/redis-sentinel.pid"# 提示Sentinel 去监视一个名为 mymaster 的主服务器 仲裁 =环境3/2+1
sentinel monitor mymaster 192.168.45.101 6379 2# 访问主节点的密码
sentinel auth-pass mymaster 123456# Sentinel 认为服务器已经断线所需要的毫秒数 默认30秒 这里演示10秒
sentinel down-after-milliseconds mymaster 10000# 若Sentinel 在改配置值内未能完成 failover(故障转移) 操作,则认为本次 failover 失败180秒 3分钟
sentinel failover-timeout mymaster 180000
3.4. 启动哨兵
依次启动3个节点 sentinel
/app/redis-6.2.6/src/redis-sentinel /usr/local/redis/conf/sentinel.conf
3.5. sentinel 监控
- sentinel _01
[root@VM-24-10-centos ~]# tail -f /usr/local/redis/log/sentinel.log
22858:X 23 Oct 2022 14:54:00.271 # Configuration loaded
22858:X 23 Oct 2022 14:54:00.272 * monotonic clock: POSIX clock_gettime
22858:X 23 Oct 2022 14:54:00.272 * Running mode=sentinel, port=26380.
22858:X 23 Oct 2022 14:54:00.273 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/coris set to the lower value of 128.
22858:X 23 Oct 2022 14:54:00.283 # Sentinel ID is 24381e0b70d72fdbb5489ee58a5db7e521dd7a54
22858:X 23 Oct 2022 14:54:00.283 # +monitor master mymaster 192.168.45.80 6380 quorum 2
22858:X 23 Oct 2022 14:54:00.286 * +slave slave 192.168.45.102:6379 192.168.45.102 6379 @ mymaster 192.168.45.101 6379
22858:X 23 Oct 2022 14:54:00.298 * +slave slave 192.168.45.103:6379 192.168.45.103 6379 @ mymaster 192.168.45.101 6379
22858:X 23 Oct 2022 14:54:02.286 * +sentinel sentinel acd408cb9683b06e5786ecded9af3cb77b3bc33c 10.0.24.10 26379 @ mymas26.101 6379
22858:X 23 Oct 2022 14:54:03.994 * +sentinel sentinel 1da437262d0fd32e484a63d9b0e4155db857923b 10.0.24.10 26379 @ mymas26.101 6379
- sentinel _02
[root@VM-24-10-centos ~]# tail -f /usr/local/redis/log/sentinel_02.log
22864:X 23 Oct 2022 14:54:00.277 # Configuration loaded
22864:X 23 Oct 2022 14:54:00.278 * monotonic clock: POSIX clock_gettime
22864:X 23 Oct 2022 14:54:00.279 * Running mode=sentinel, port=26381.
22864:X 23 Oct 2022 14:54:00.279 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/coris set to the lower value of 128.
22864:X 23 Oct 2022 14:54:00.289 # Sentinel ID is acd408cb9683b06e5786ecded9af3cb77b3bc33c
22864:X 23 Oct 2022 14:54:00.289 # +monitor master mymaster 192.168.45.80 6380 quorum 2
22864:X 23 Oct 2022 14:54:00.292 * +slave slave 192.168.45.80:6381 192.168.45.80 6381 @ mymaster 192.168.45.80 6380
22864:X 23 Oct 2022 14:54:00.300 * +slave slave 192.168.45.80:6382 192.168.45.80 6382 @ mymaster 192.168.45.80 6380
22864:X 23 Oct 2022 14:54:02.309 * +sentinel sentinel 24381e0b70d72fdbb5489ee58a5db7e521dd7a54 10.0.24.10 26380 @ mymas26.80 6380
22864:X 23 Oct 2022 14:54:03.994 * +sentinel sentinel 1da437262d0fd32e484a63d9b0e4155db857923b 10.0.24.10 26382 @ mymas26.80 6380
- sentinel _03
[root@VM-24-10-centos src]# tail -f /usr/local/redis/log/sentinel_03.log
22937:X 23 Oct 2022 14:54:01.929 # Configuration loaded
22937:X 23 Oct 2022 14:54:01.929 * monotonic clock: POSIX clock_gettime
22937:X 23 Oct 2022 14:54:01.930 * Running mode=sentinel, port=26382.
22937:X 23 Oct 2022 14:54:01.930 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
22937:X 23 Oct 2022 14:54:01.940 # Sentinel ID is 1da437262d0fd32e484a63d9b0e4155db857923b
22937:X 23 Oct 2022 14:54:01.940 # +monitor master mymaster 192.168.45.80 6380 quorum 2
22937:X 23 Oct 2022 14:54:01.942 * +slave slave 192.168.45.80:6381 192.168.45.80 6381 @ mymaster 192.168.45.80 6380
22937:X 23 Oct 2022 14:54:01.950 * +slave slave 192.168.45.80:6382 192.168.45.80 6382 @ mymaster 192.168.45.80 6380
22937:X 23 Oct 2022 14:54:02.286 * +sentinel sentinel acd408cb9683b06e5786ecded9af3cb77b3bc33c 10.0.24.10 26381 @ mymaster 192.168.45.80 6380
22937:X 23 Oct 2022 14:54:02.309 * +sentinel sentinel 24381e0b70d72fdbb5489ee58a5db7e521dd7a54 10.0.24.10 26380 @ mymaster 192.168.45.80 6380
3.6. 哨兵验证
- 登录101节点
/app/redis-6.2.6/src/redis-cli -p 6379 -a 123456
info replication
[root@localhost redis01]# /app/redis-6.2.6/src/redis-cli -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.45.76,port=6381,state=online,offset=2889706,lag=1
slave1:ip=192.168.45.76,port=6382,state=online,offset=2889706,lag=1
master_failover_state:no-failover
master_replid:0ee70287bead3e408a25ba421d918d7560f8fee3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2889972
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1841397
repl_backlog_histlen:1048576
127.0.0.1:6380>
- 登录102节点
/app/redis-6.2.6/src/redis-cli -p 6379 -a 123456
info replication
[root@localhost redis01]# /app/redis-6.2.6/src/redis-cli -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6381> info replication
# Replication
role:slave
master_host:192.168.45.76
master_port:6380
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:2925735
slave_repl_offset:2925735
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:0ee70287bead3e408a25ba421d918d7560f8fee3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2925735
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1877160
repl_backlog_histlen:1048576
127.0.0.1:6381> exit
- 登录103节点
/app/redis-6.2.6/src/redis-cli -p 6379 -a 123456
info replication
[root@localhost redis01]# /app/redis-6.2.6/src/redis-cli -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6382> info replication
# Replication
role:slave
master_host:192.168.45.76
master_port:6380
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:2971130
slave_repl_offset:2971130
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:0ee70287bead3e408a25ba421d918d7560f8fee3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2971130
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1922555
repl_backlog_histlen:1048576
127.0.0.1:6382>